Skip to content

Instantly share code, notes, and snippets.

@erickoledadevrel
Last active November 16, 2023 07:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erickoledadevrel/5a8423c38c1f5f6ca6ec50e326ca3ad5 to your computer and use it in GitHub Desktop.
Save erickoledadevrel/5a8423c38c1f5f6ca6ec50e326ca3ad5 to your computer and use it in GitHub Desktop.
Moving events from one calendar to another, using Google Apps Script
/**
* Move events with a given title from your primary calendar to another calendar.
* You must enable the Calendar Advanced Service:
* https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services
*/
function moveEvents(eventTitle, fromCalendarName, toCalendarName) {
var fromCalendarId = CalendarApp.getCalendarsByName(fromCalendarName)[0].getId();
var toCalendarId = CalendarApp.getCalendarsByName(toCalendarName)[0].getId();
var now = new Date();
var oneYearAgo = new Date(now);
oneYearAgo.setYear(oneYearAgo.getYear() - 1);
var pageToken = null;
do {
var results = Calendar.Events.list(fromCalendarId, {
q: eventTitle,
pageToken: pageToken
});
results.items.filter(function(event) {
return event.summary == eventTitle;
}).forEach(function(event) {
Calendar.Events.move(fromCalendarId, event.id, toCalendarId);
console.log('Moved event ' + event.id);
});
pageToken = results.nextPageToken;
} while (pageToken)
}
function test() {
moveEvents('Gym', 'Medical', 'Exercise');
}
@alexbartosik
Copy link

I get an error reading line 7 of the code: Cannot read properties of undefined (reading 'getId'). Any fix to this?

@erickoledadevrel
Copy link
Author

I get an error reading line 7 of the code: Cannot read properties of undefined (reading 'getId'). Any fix to this?

I think that means that it couldn't find a calendar with that name.

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