Skip to content

Instantly share code, notes, and snippets.

@vbresan
Forked from nizioleque/Code.gs
Created March 23, 2024 06:12
Show Gist options
  • Save vbresan/b4f04ca4048e700662ec95af493f31e5 to your computer and use it in GitHub Desktop.
Save vbresan/b4f04ca4048e700662ec95af493f31e5 to your computer and use it in GitHub Desktop.
Google Calendar events bulk edit (with Google Apps Script)

Google Calendar events bulk edit

This is a simple Google Apps Script which lets you modify Google Calendar events in bulk.

These instructions assume that you have basic knowledge of JavaScript. Documentation of the Google Calendar API here.

Usage instructions

  1. Create a new Google Apps Script project
  2. Find the "Services" section on the left sidebar. Click the "+" button and add Google Calendar API (v3)
  3. Copy the contents of Code.gs into Code.gs in your project
  4. Customize the script as explained below and run it!
  5. On the first run you'll be asked to log in with your Google account to connect to the Calendar API. Don't worry about the warning you'll see, this is normal.

Customization

In order to customize the function, change the following constants and functions in the top section of Code.gs:

  1. Test - enables test mode. If enabled, TestFn will run instead of ModifyFn. This is to let you test your modifications on one event instead of applying them on all events at once.
  2. TestFn - the function used in test mode. It runs once with the (filtered) list of events as its parameter. In this example if applies the modifications (described in ModifyFn) to the first event on the list and logs the event before and after the changes.
  3. CalendarId - the ID of the calendar you want to modify. Find it by going to Google Calendar settings for this calendar and scrolling down until you find "Calendar ID". You can also pass "default" to use your account's default calendar.
  4. FilterFn - filtering function which lets you select the events that you want to modify.
  5. ModifyFn - the function that applies your modifications on every event (when test mode is disabled). In this example it changes the event's time to 11:40-12:10.

By default the modifications are only applied to events in the future. Feel free to change that by customizing the timeMin and timeMax fields in the events query (lines 37-41).

function start() { main(); }
// CONFIGURE BELOW ------------------------------------------------
const Test = true;
const TestFn = events => {
const testEvent = events[0];
console.log(testEvent);
const response = updateEvent(testEvent);
console.log(response);
}
const CalendarId =
"<YOUR CALENDAR'S ID>";
const FilterFn = event => {
return event.summary.includes("Project meeting")
}
const ModifyFn = event => {
const startTime = new Date(event.start.dateTime);
startTime.setHours(11, 40)
const endTime = new Date(event.end.dateTime);
endTime.setHours(12, 10)
event.start.dateTime = startTime.toISOString();
event.end.dateTime = endTime.toISOString();
}
// CONFIGURE ABOVE ------------------------------------------------
function main() {
const startTime = new Date();
const events = Calendar.Events.list(CalendarId, {
timeMin: startTime.toISOString(),
singleEvents: true,
orderBy: "startTime",
});
const eventsFiltered = events.items.filter(FilterFn);
console.log(`Events found: ${eventsFiltered.length}`);
if (Test) {
TestFn(eventsFiltered);
return;
}
for (const event of eventsFiltered) {
updateEvent(event);
}
}
function updateEvent(event) {
ModifyFn(event);
return Calendar.Events.update(event, CalendarId, event.id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment