Synchronize Google Calendar events between two or more 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
Calendar Synchronization
. - Click at the
+
next toServices
, addGoogle Calendar API
v3
asCalendar
. - Replace the
Code.gs
file content with this code and save.
The following examples are based on assumed calendars Work
and Family
.
-
Click the
+
next toFiles
to add a new script fileonStart
:function onStart() { sync('Work', 'Family') }
-
Select file
Code.gs
, run the functionstart()
and grant the requested rights.Now, any change to the
Work
calendar is synchronized to theFamily
calendar. -
To stop the synchronization, select file
Code.gs
and runstop()
. -
To remove all synchronized events, select file
Code.gs
and runclean()
.
Multiple source calendars can be synchronized to the same target calendar.
function onStart() {
sync('Work', 'Family')
sync('Second Work', 'Family')
}
One source calendar can be synchronized to multiple target calendars.
function onStart() {
sync('Work', 'Family')
sync('Work', 'Second Family')
}
The synchronization time range can be specified.
By default, the past 7
and next 28
days are synchronized.
sync('Work', 'Family', 7, 28)
There are a couple of helper function available to support the correction function.
For past days:
startOfWeek(offset = 0)
startOfMonth(offset = 0)
startOfQuarter(offset = 0)
startOfHalfyear(offset = 0)
startOfYear(offset = 0)
For next days:
endOfWeek(offset = 0)
endOfMonth(offset = 0)
endOfQuarter(offset = 0)
endOfHalfyear(offset = 0)
endOfYear(offset = 0)
Example - timerange is from the beginning of this week until the end of next month:
sync('Work', 'Family', startOfWeek(), endOfMonth(1))
To avoid any unwanted data exposure, by default, only the start date, end date, recurrence rule and Busy
as default summary are synchronized.
The correction function can be specified to modify or filter target events.
function correctionFunction(targetEvent, sourceEvent) {
// correction of the target event ...
return targetEvent
}
sync('Work', 'Family', 7, 28, correctionFunction)
function correctionFunction(targetEvent, sourceEvent) {
// Keep properties
targetEvent.summary = sourceEvent.summary
targetEvent.location = sourceEvent.location
// Modify properties
if (sourceEvent.attendees) targetEvent.colorId = '6'
// Exclude events
if (sourceEvent.transparency === 'transparent') targetEvent.status = 'cancelled'
// Do not forget to return the target event
return targetEvent
}
Read more about event properties and colors.
There are a couple of helper function available to support the correction function.
isSynchronizedEvent(sourceEvent) // true if synchronized from another calendar
isRecurringEvent(sourceEvent) // true if recurring event series or instance
isOOOEvent(sourceEvent) // true if out of office event
isAlldayEvent(sourceEvent) // true if allday event
isOnWeekend(sourceEvent) // true if on Saturday or Sunday
isBusyEvent(sourceEvent) // true if status is busy
isOpenByMe(sourceEvent) // true if needs action by me
isAcceptedByMe(sourceEvent) // true if accepted by me
isTentativeByMe(sourceEvent) // true if responded tentative by me
isDeclinedByMe(sourceEvent) // true if declined by me
By default, all source events are synchronized - also the ones which are synchronized from another calendar. You might want to exclude these events from your synchronization.
Example - this will result in a growing number of events:
function onStart() {
sync('Work', 'Family')
sync('Family', 'Work')
}
Solution - exclude synchronized events:
function correctionFunction(targetEvent, sourceEvent) {
if (isSynchronizedEvent(sourceEvent)) targetEvent.status = 'cancelled'
return targetEvent
}
function onStart() {
sync('Work', 'Family', 7, 28, correctionFunction)
sync('Family', 'Work', 7, 28, correctionFunction)
}
The synchronization interval can be specified.
By default, the next synchronization is triggered after 1
minute.
This value can be increased, if the Google Calender API quota is an issue.
function onStart() {
setSyncInterval(1)
sync('Work', 'Family')
}
A fallback trigger is starting the synchronization again, if the synchronziation script is not completed within the maximum execution time, given by Google Scripts.
By default, the maximum execution time is 6
minutes.
This value can be increased to make use of the increased Google Workspace limit of 30
minutes.
function onStart() {
setMaxExecutionTime(30)
sync('Work', 'Family')
}
To update the script version, replace the Code.gs
file content with this code.
- To stop the synchronization, select file
Code.gs
and runstop()
. - To remove all synchronized events, select file
Code.gs
and runclean()
. - Remove the Google Apps Script project.
Feel free to open an issue for bugs, feature requests or any other question.