Sync Google Calendars for Free


3 December 2024

My calendar chaos!

I often work for multiple clients at once on the basis of outcomes delivered. Recently I was helping a government agency to adopt OKRs. It was a high-touch engagement involving the design and facilitation of dozens of workshops for hundreds of people across 30+ teams, writing internal guidance, building and deploying some custom tooling, and nurturing an internal group of OKR Ambassadors over more than half a year. To simplify scheduling and collaboration the client gave me an account on their Google Workspace.

Sounds great, right? Using their Google Workspace account ensured data protection and compliance and made me more effective. I couldn’t have taken on such a large and complex engagement without it but managing two separate Google Calendars quickly became a logistical nightmare. Between juggling my personal life and time with other clients, I’d occasionally double-book myself—or worse, miss appointments entirely.

I quickly realised I needed a better way to sync both calendars. I looked briefly at some commercial solutions and then I remembered the power & simplicity of Google Apps Script (GAS).

I wondered if I could build something simple myself or with help from generative AI!

Getting help from GenAI

I opened ChatGPT and described what I wanted via this prompt:

Please write a google workspace script which will "sync" two google calendars I own by creating an event called "HOLD" in each calendar where an event exists in the other calendar. When events are removed from the source calendar, the "hold" events should be removed from the other calendar. The script should run every 20 minutes during business hours.

Immediately, I had a mostly working script!

I kept chatting to do a bit more refinement; I wanted to disable superfluous reminders on the HOLD events and access the free/busy flag on events directly via the Calendar Event API. It turns out there’s no way to access this directly but I got the script working well enough within 15 minutes. It syncs in both directions by loading events from both calendars by adding a “HOLD” placeholder in one calendar for accepted events in the other.

It does this automatically every hour, automatically cleaning up the placeholders if the original event gets canceled, deleted, or declined and prevents unnecessary reminders from being created on “HOLD” events.

I was really impressed with the cleanliness of the code, the reusable functions, and the code for creating and removing the trigger.

You can read my entire conversation with ChatGPT.

And the cost? Nothing. Zero. Only the time it took me to chat with the AI and set it up.

It’s been happily keeping both calendars in harmony for 6 months with zero issues and zero intervention on my part.

Now I get HOLD events in each calendar when I'm committed in the other!

How you can use it

Are you also juggling multiple Google calendars across workspaces that need to be kept in sync? Give the script below a whirl and let me know how you get on!

  1. Open Google Apps Script and create a new project.
  2. Copy and paste the script (below) into the editor.
  3. Replace the placeholder calendar IDs with your own.
  4. Run it once (you’ll need to authorize it) to test it.
  5. Call the createTrigger() function to set up automatic syncing.

Happy AI Coding!

This script has taken a huge administrative weight off my shoulders and the experience of being able to build simple apps like this is a game changer and adds a tremendous value to my already favourite productivity suite. I can’t wait to build more.

I hope it helps you, too. I’d love to hear from you how you’re using it as well as other experiences you’ve had with GAS and ChatGPT to build simple utilities like this.

 1//sync two calendars which I own by creating a 'HOLD' event in the corresponding calendar (or deleting as appropriate)
 2
 3function syncCalendars() {
 4  // Replace with your Calendar IDs (typically your email address)
 5  var calendarId1 = 'XXXXXXX';
 6  var calendarId2 = 'XXXXXXX';
 7
 8 // Define the name for the placeholder event
 9  var holdEventName = 'xHOLDx';  // Change this to whatever name you prefer
10  var daysAheadToSync = 45;
11  
12  // Get the calendar objects
13  var calendar1 = CalendarApp.getCalendarById(calendarId1);
14  var calendar2 = CalendarApp.getCalendarById(calendarId2);
15  
16  // Define the sync time window
17  var now = new Date();
18  var startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate());
19  var endTime = new Date(startTime);
20  endTime.setDate(endTime.getDate() + daysAheadToSync);
21
22  // Sync from Calendar 1 to Calendar 2
23  syncFromSourceToTarget(calendar1, calendar2, startTime, endTime, holdEventName);
24  
25  // Sync from Calendar 2 to Calendar 1
26  syncFromSourceToTarget(calendar2, calendar1, startTime, endTime, holdEventName);
27
28
29  // Remove reminders from newly created and existing HOLD events on both calendars
30  removeRemindersFromHoldEventsOnCalendar(calendar1, startTime, endTime, holdEventName);
31  removeRemindersFromHoldEventsOnCalendar(calendar2, startTime, endTime, holdEventName);
32}
33
34function syncFromSourceToTarget(sourceCalendar, targetCalendar, startTime, endTime, holdEventName) {
35  var sourceEvents = sourceCalendar.getEvents(startTime, endTime);
36  var targetEvents = targetCalendar.getEvents(startTime, endTime, {search: holdEventName});
37  
38  // Remove HOLD events in target calendar if the source event is no longer there
39  targetEvents.forEach(function(targetEvent) {
40    var relatedEvent = sourceEvents.find(function(event) {
41      return event.getStartTime().getTime() === targetEvent.getStartTime().getTime() &&
42             event.getEndTime().getTime() === targetEvent.getEndTime().getTime();
43    });
44    if (!relatedEvent) {
45      Logger.log('Found event to delete: ' + targetEvent.getTitle() + ' starting at ' + targetEvent.getStartTime());
46      targetEvent.deleteEvent();
47    }
48  });
49  
50  // Add HOLD events in target calendar for events in source calendar
51  sourceEvents.forEach(function(sourceEvent) {
52    if (sourceEvent.getTitle() !== holdEventName) { 
53      var relatedEvent = targetEvents.find(function(event) {
54        return event.getStartTime().getTime() === sourceEvent.getStartTime().getTime() &&
55               event.getEndTime().getTime() === sourceEvent.getEndTime().getTime();
56      });
57      if (!relatedEvent) {
58        Logger.log('Found event to create: ' + sourceEvent.getTitle() + ' starting at ' + sourceEvent.getStartTime());
59        targetCalendar.createEvent(holdEventName, sourceEvent.getStartTime(), sourceEvent.getEndTime(),sourceEvent.getEndTime())
60              .removeAllReminders(); // no reminders on hold events
61      }
62    }
63  });
64}
65
66function removeRemindersFromHoldEventsOnCalendar(calendar, startTime, endTime, holdEventName) {
67  var events = calendar.getEvents(startTime, endTime, {search: holdEventName});
68  events.forEach(function(event) {
69    if (event.getTitle() === holdEventName) {
70      Logger.log('Removing reminders from HOLD event: ' + event.getTitle() + ' starting at ' + event.getStartTime());
71      event.removeAllReminders();
72    }
73  });
74}
75
76function createTrigger() {
77  ScriptApp.newTrigger('syncCalendars')
78    .timeBased()
79    .everyHours(1)
80    .create();
81}
82
83function deleteTriggers() {
84  var triggers = ScriptApp.getProjectTriggers();
85  triggers.forEach(function(trigger) {
86    ScriptApp.deleteTrigger(trigger);
87  });
88}