Last active
November 16, 2023 07:02
-
-
Save erickoledadevrel/5a8423c38c1f5f6ca6ec50e326ca3ad5 to your computer and use it in GitHub Desktop.
Moving events from one calendar to another, using Google Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); | |
} |
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
I get an error reading line 7 of the code: Cannot read properties of undefined (reading 'getId'). Any fix to this?