Synchronize Google Calendar events between one or multiple calendars. Made with Google Apps Script.
Related to Google Calendar Corrections.
- Backup all Google Calendars to be able to restore them if something went wrong
- Open Google Apps Script and create a new project
Google Calendar Synchronization
- Replace
Code.gs
file content with this code - Click at the
+
next toServices
, addGoogle Calendar API v3
asCalendar
Click at the +
next to Files
to add a new script file, you can name it onCalendarUpdate
.
Now you can copy and paste the following example code:
function onCalendarUpdate() {
runOneWaySync('Work', 'Family', 7, 21, (targetEvent, sourceEvent) => {
if (sourceEvent.summary === 'Holiday') targetEvent.summary = 'Family Time'
if (sourceEvent.summary === 'Secret') targetEvent.status = 'cancelled'
targetEvent.colorId = '0'
targetEvent.description = sourceEvent.description
return targetEvent
})
}
Or with comments:
// This function is called by the trigger
// You should modify the options and correction function to your needs
function onCalendarUpdate() {
// Run the correction with some options
runOneWaySync(
// The name of the source calendar
'Work',
// The name of the target calendar
'Family',
// Previous days
7,
// Next days
21,
// Correction function, event as input
(targetEvent, sourceEvent) => {
// All Work events with title "Holiday" are saved to Family as "Family Time"
if (sourceEvent.summary === 'Holiday') targetEvent.summary = 'Family Time'
// All Work events with title "Secret" are not synchronized to "Family"
if (sourceEvent.summary === 'Secret') targetEvent.status = 'cancelled'
// All events keep the default calendar color
targetEvent.colorId = '0'
// Add the description to the target event
// By default, only the start and end date and the summary
// are synchronized to avoid any unintended data exposure
targetEvent.description = sourceEvent.description
// Do not forget to return the target event
return targetEvent
}
)
}
Further reading for the correction function: Google API Documentation and color IDs
Finally, save the changes and run the onCalendarUpdate
function manually.
On the first run, you have to grant permissions (calendar access) to the script.
Run the function onCalendarUpdate()
to start the synchonization.
At the first run, all events are synchronized. With any other run, only modified events are synchronized.
Create two triggers for the onCalendarUpdate
function, triggered by calendar updates:
- one for the source calencar ID
- one for the target calendar ID
Now, on every calendar update in the source calendar, the changes are synchronized to the target calendar.
On every change to synchronized events in the target calendar, the changes are overwritten from the source calendar again.
Copy the onCalendarUpdate
function, for example as onWorkCalendarUpdate
or onFamilyCalendarUpdate
.
onWorkCalendarUpdate() {
runOneWaySync('Work', 'Family', 7, 21, (targetEvent, sourceEvent) => {
// Exclude synchronized events
if (sourceEvent.extendedProperties?.private?.sourceCalendarId) targetEvent.status = 'cancelled'
...
return targetEvent
})
}
onFamilyCalendarUpdate() {
runOneWaySync('Family', 'Work', 7, 21, (targetEvent, sourceEvent) => {
// Exclude synchronized events
if (sourceEvent.extendedProperties?.private?.sourceCalendarId) targetEvent.status = 'cancelled'
...
return targetEvent
})
}
Create two triggers per on...CalendarUpdate
function and insert the source and target calendar ID respectively.
Inside the onCalendarUpdate
function, copy the runOneWaySync
function call.
Change the target calendar respectively.
function onCalendarUpdate() {
runOneWaySync('Work', 'Family', 7, 21, (targetEvent, sourceEvent) => {
...
return targetEvent
})
runOneWaySync('Work', 'Personal', 7, 21, (targetEvent, sourceEvent) => {
...
return targetEvent
})
}
Create a third trigger for the second target calendar id.
To clean any calendar from all synchronized events, you can call the function cleanCalendar
:
function cleanup() {
cleanCalendar('Work')
}
- Initial release
onCalendarUpdate
function removed from theCode.gs
file.clasp.json
file removed from the repository
- Simplified algorithm to avoid issues
cleanCalendar
function added
resetScript
function removed
- synchronize modified events only
- consider hidden calendars
- do not log skipped events
resetScript
function added
- Node.js and NPM installed
- Command Line Apps Script Projects installed globally
- Clone this repository
- Run
clasp login
to login to Google if not done before - Run
clasp create --type standalone --rootDir lib --title "Google Calendar Synchronization"
to create a new Apps Script Project - Run
mv lib/.clasp.json .clasp.json
to move the CLASP config file to the project root
- Run
clasp push
to replace the remote files with the local ones - Run
clasp open
to open the project in the Cloud IDE - Run
clasp pull
to replace the local files with the remote ones - Run
node buildscript.js
to build theCode.gs
file