Apply corrections to Google Calendar events on any update to enforce golden rules. Made with Google Apps Script.
Related to Google Calendar Synchronization.
- 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 Correction
- 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() {
runCorrection('Work', '2023-01-01', event => {
event.colorId = event.transparency === 'transparent' ? '10' : '11'
return event
})
}
Or with comments:
// This function is called by the trigger
// You should modify the name, start date and correction function to your needs
function onCalendarUpdate() {
// Run the correction with some options
runCorrection(
// The name of the calendar
'Work',
// The start date (YYYY-MM-DD, corrections are applied from this date)
'2023-01-01',
// Correction function, event as input
event => {
// In this example, "busy" events are painted red, "free" events green
event.colorId = event.transparency === 'transparent' ? '10' : '11'
// Do not forget to return the corrected event
return event
}
)
}
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 correct all events from the start date.
At the first run, all events after the start date are corrected. With any other run, only modified events after the start date are corrected.
Create a trigger for the onCalendarUpdate
function, triggered by calendar updates and with the respected calendar id.
Now, on every calendar update, the events are corrected automatically if required.
Copy the onCalendarUpdate
function, for example as onWorkCalendarUpdate
or onFamilyCalendarUpdate
.
onWorkCalendarUpdate() {
runCorrection('Work', '2023-01-01', event => {
...
return event
})
}
onFamilyCalendarUpdate() {
runCorrection('Family', '2023-01-01', event => {
...
return event
})
}
Create a trigger per calendar, select each time a different on...CalendarUpdate
function and insert a different calendar ID.
Events created by the Google Calendar Synchronization can be excluded from correction:
runCorrection('Family', '2023-01-01', event => {
if (event.extendedProperties?.private?.sourceCalendarId) return event
...
})
After any modification to the onCalendarUpdate
function, you should run the function resetScript
to reset the script and allow correction of all events from the start date again accordingly.
- Initial release
onCalendarUpdate
function removed from theCode.gs
file.clasp.json
file removed from the repository
- consider hidden calendars
- 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 Correction"
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