Giter Site home page Giter Site logo

tanaikech / managing-a-lot-of-google-calendar-events-using-batch-requests-with-google-apps-script Goto Github PK

View Code? Open in Web Editor NEW
9.0 4.0 0.0 4 KB

These are the sample scripts for managing a lot of Google Calendar Events using the batch requests with Google Apps Script.

License: MIT License

JavaScript 100.00%
google-apps-script google-calendar google-calendar-api batch-request

managing-a-lot-of-google-calendar-events-using-batch-requests-with-google-apps-script's Introduction

Managing A Lot Of Google Calendar Events using Batch Requests with Google Apps Script

MIT License

Overview

These are the sample scripts for managing a lot of Google Calendar Events using the batch requests with Google Apps Script.

Description

When we want to manage the events of Google Calendar, we have 2 ways. One is the use of Calendar service. Another is the use of Calendar API. In the case of them, when we want to manage a lot of calendar events, unfortunately, both ways have no batch requests. Namely, for example, when a lot of events are deleted, deleteEvent() and Events: delete are required to be used in the loop. In this case, the process cost will be high. On the other hand, Calendar API can use the batch requests. But in this case, in order to use this batch requests with Google Apps Script, it is required to create the request body of multipart/mixed by each user. Because there are no methods for automatically requests the batch requests. From this situation, here, I would like to introduce the simple sample scripts for creating, updating and deleting the events of Google Calendar using the batch requests with Google Apps Script.

Usage

The sample scripts use BatchRequest of the Google Apps Script library.

1. Install BatchRequest

You can see how to install BatchRequest at here.

2. Sample script

Creating events

In this sample script, 30 events are created from 20200601 to 20200630.

function createEvents() {
  const calendarId = "###"; // Please set the Calendar ID.
  const numberOfEvents = 30;
  let requests = [];
  for (let i = 0; i < numberOfEvents; i++) {
    requests.push({
      method: "POST",
      endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
      requestBody: {
        start: {
          dateTime: new Date(2020, 5, i + 1, 12).toISOString(),
          timeZone: Session.getScriptTimeZone(),
        },
        end: {
          dateTime: new Date(2020, 5, i + 1, 13).toISOString(),
          timeZone: Session.getScriptTimeZone(),
        },
        summary: `sample event ${i + 1}`,
        description: `sample description ${i + 1}`,
      },
    });
  }
  const result = BatchRequest.EDo({
    batchPath: "batch/calendar/v3",
    requests: requests,
  });
  console.log(result);
}

Updating events

In this sample script, the title and description of all events from 20200601 to 20200630 are updated.

function updateEvents() {
  const calendarId = "###"; // Please set the Calendar ID.
  const start = new Date(2020, 5, 1);
  const end = new Date(2020, 5, 30);
  const requests = CalendarApp.getCalendarById(calendarId)
    .getEvents(start, end)
    .map((e, i) => ({
      method: "PUT",
      endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${e
        .getId()
        .replace("@google.com", "")}`,
      requestBody: {
        start: { dateTime: e.getStartTime().toISOString() },
        end: { dateTime: e.getEndTime().toISOString() },
        description: `sample description ${i}`,
        summary: `sample event ${i}`,
      },
    }));
  const result = BatchRequest.EDo({
    batchPath: "batch/calendar/v3",
    requests: requests,
  });
  console.log(result);
}

IMPORTANT: In this script, Events: update is used. When I tested this using Events: patch, I confirmed the unstable working. So I used Events: update. But this might be resolved in the future update.

Deleting events

In this sample script, all events from 20200601 to 20200630 are deleted. So when you use this, please be careful this.

function deleteEvents() {
  const calendarId = "###"; // Please set the Calendar ID.
  const start = new Date(2020, 5, 1);
  const end = new Date(2020, 5, 30);
  const requests = CalendarApp.getCalendarById(calendarId)
    .getEvents(start, end)
    .map((e, i) => ({
      method: "DELETE",
      endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${e
        .getId()
        .replace("@google.com", "")}`,
    }));
  const result = BatchRequest.EDo({
    useFetchAll: true,
    batchPath: "batch/calendar/v3",
    requests: requests,
  });
}

IMPORTANT

  • At the batch requests, 100 API requests can be run with the asynchronous process as one API call.

    • When the library of BatchRequest is used, all requests can be processed in the library, even when the number of your requests is more than 100.
  • When you manage a lot of Calendar Events, please be careful of Quotas for Google Services. Ref

References:


Licence

MIT

Author

Tanaike

If you have any questions or comments, feel free to contact me.

Update History

  • v1.0.0 (June 12, 2020)

    1. Initial release.

TOP

managing-a-lot-of-google-calendar-events-using-batch-requests-with-google-apps-script's People

Contributors

tanaikech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

managing-a-lot-of-google-calendar-events-using-batch-requests-with-google-apps-script's Issues

Domain Wide Delegation

Is it possible to create the events on behalf of another user in the domain using this method?

How can the access token be passed into the request?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.