Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created October 11, 2022 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/33544a8f56cb5b4c2f96ceb29d6b2855 to your computer and use it in GitHub Desktop.
Save tanaikech/33544a8f56cb5b4c2f96ceb29d6b2855 to your computer and use it in GitHub Desktop.
Creating and Deleting Multiple Events in Google Calendar by Batch Requests using Calendar API with Node.js

Creating and Deleting Multiple Events in Google Calendar by Batch Requests using Calendar API with Node.js

These are the sample scripts for creating and deleting multiple events in Google Calendar by batch requests using Calendar API with Node.js.

In the current stage, unfortunately, googleapis for Node.js cannot request batch requests. Ref So, when multiple events are created and deleted in Google Calendar using Node.js, it is required to run the script in a loop. In this case, the process cost becomes high. Ref In this post, I would like to introduce creating and deleting multiple events in Google Calendar using batch request with Node.js.

Prepare

In these sample scripts, a Node.js module of node-gbatchrequests is used. So, please install it.

Creating multiple events

In this sample script, the access token is retrieved from "auth" of const calendar = google.calendar({ version: "v3", auth }) is used. This script is from Quickstart for Node.js.

When you use this script, please be careful about the scopes. In this case, please include https://www.googleapis.com/auth/calendar in the scopes.

// Here, the access token is retrieved from "auth" of this script is used.
// const calendar = google.calendar({ version: "v3", auth });

const calendarId = "###"; // Please set your Calendar ID.

// Please set multiple events you want to create.
const events = [
  {
    summary: "sample event 1",
    description: "sample description 1",
    start: {
      dateTime: "2022-10-01T09:00:00Z",
      timeZone: "Asia/Tokyo",
    },
    end: {
      dateTime: "2022-10-01T10:00:00Z",
      timeZone: "Asia/Tokyo",
    },
  },
  {
    summary: "sample event 2",
    description: "sample description 2",
    start: {
      dateTime: "2022-10-02T09:00:00Z",
      timeZone: "Asia/Tokyo",
    },
    end: {
      dateTime: "2022-10-02T10:00:00Z",
      timeZone: "Asia/Tokyo",
    },
  },
  ,
  ,
  ,
];
const obj = {
  accessToken: auth.credentials.access_token,
  api: { name: "calendar", version: "v3" },
  requests: events.map((e) => ({
    method: "POST",
    endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
    requestBody: e,
  })),
};
RunBatch(obj)
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

Deleting multiple events

In this sample script, the access token is retrieved from "auth" of const calendar = google.calendar({ version: "v3", auth }) is used. This script is from Quickstart for Node.js.

When you use this script, please be careful about the scopes. In this case, please include https://www.googleapis.com/auth/calendar in the scopes.

// Here, the access token is retrieved from "auth" of this script is used.
// const calendar = google.calendar({ version: "v3", auth });

const calendarId = "###"; // Please set your Calendar ID.

// Please set event IDs you want to delete.
const eventsIds = ["###eventId1###", "###eventId2###", , ,];
const obj = {
  accessToken: auth.credentials.access_token, // Please set your access token.
  api: { name: "calendar", version: "v3" },
  requests: eventsIds.map((eventId) => ({
    method: "DELETE",
    endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${eventId}`,
  })),
};
RunBatch(obj)
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment