Sync Google Calendars for Free


Updated: 1 March 2025
Update: I was hitting API rate limits so I made a few tweaks including adding a "backoff" function and not synching events where I haven't accepted all-day holidays which end with "day". Latest code is below.

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!
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.

  1var DEBUG_MODE = false; // Set to true for debug mode (only logs events without modifying them)
  2
  3var calendarId1;
  4var calendarId2;
  5
  6function setup() {
  7  calendarId1 = 'xxx@xxx.com';
  8  calendarId2 = 'xxx@xxxxx.com';
  9}
 10
 11function debug(message) {
 12  if (DEBUG_MODE) {
 13    Logger.log('[DEBUG] ' + message);
 14  }
 15}
 16
 17function syncCalendars() {
 18  setup();
 19  var holdEventName = 'xHOLDx';
 20  var daysAheadToSync = 10;
 21
 22  var calendar1 = CalendarApp.getCalendarById(calendarId1);
 23  var calendar2 = CalendarApp.getCalendarById(calendarId2);
 24
 25  var now = new Date();
 26  var startTime = new Date(now.getFullYear(), now.getMonth(), now.getDate());
 27  var endTime = new Date(startTime);
 28  endTime.setDate(endTime.getDate() + daysAheadToSync);
 29
 30  syncFromSourceToTarget(calendar1, calendar2, startTime, endTime, holdEventName);
 31  syncFromSourceToTarget(calendar2, calendar1, 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  var deletionDelayHours = 2;
 38
 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
 45    var now = new Date();
 46    var eventStart = targetEvent.getStartTime();
 47    if (!relatedEvent && (eventStart - now) / 3600000 > deletionDelayHours) {
 48      debug('Would delete: ' + targetEvent.getTitle() + ' starting at ' + targetEvent.getStartTime());
 49      if (!DEBUG_MODE) {
 50        retryWithBackoff(function () {
 51          targetEvent.deleteEvent();
 52        }, 3);
 53      }
 54    }
 55  });
 56
 57  sourceEvents.forEach(function (sourceEvent) {
 58    var eventTitle = sourceEvent.getTitle();
 59    if (eventTitle !== holdEventName) {
 60      var isAllDay = sourceEvent.isAllDayEvent();
 61      var containsDay = /day/i.test(eventTitle);
 62      if (!(isAllDay && containsDay)) {
 63        var myStatus = sourceEvent.getMyStatus();
 64        if (myStatus === CalendarApp.GuestStatus.YES || myStatus === CalendarApp.GuestStatus.OWNER) {
 65          var relatedEvent = targetEvents.find(function (event) {
 66            return event.getStartTime().getTime() === sourceEvent.getStartTime().getTime() &&
 67                   event.getEndTime().getTime() === sourceEvent.getEndTime().getTime();
 68          });
 69
 70          if (!relatedEvent) {
 71            debug('Would create: ' + eventTitle + ' starting at ' + sourceEvent.getStartTime());
 72            if (!DEBUG_MODE) {
 73              retryWithBackoff(function () {
 74                var newEvent;
 75                if (sourceEvent.isRecurringEvent()) {
 76                  var series = sourceEvent.getEventSeries();
 77                  newEvent = targetCalendar.createEventSeries(holdEventName, sourceEvent.getStartTime(), sourceEvent.getEndTime(), series.getRecurrence());
 78                } else {
 79                  newEvent = targetCalendar.createEvent(holdEventName, sourceEvent.getStartTime(), sourceEvent.getEndTime());
 80                }
 81                newEvent.removeAllReminders();
 82              }, 3);
 83            }
 84          }
 85        }
 86      } else {
 87        debug('Skipping all-day event ending with "day": ' + eventTitle);
 88      }
 89    }
 90  });
 91}
 92
 93function createTrigger() {
 94  ScriptApp.newTrigger('syncCalendars')
 95    .timeBased()
 96    .everyHours(2)
 97    .create();
 98}
 99
100function deleteTriggers() {
101  var triggers = ScriptApp.getProjectTriggers();
102  triggers.forEach(function (trigger) {
103    ScriptApp.deleteTrigger(trigger);
104  });
105}
106
107function retryWithBackoff(fn, retries) {
108  for (var i = 0; i < retries; i++) {
109    try {
110      return fn();
111    } catch (e) {
112      Logger.log('Error: ' + e.message + ' (Retrying in ' + (i + 1) + 's)');
113      Utilities.sleep((i + 1) * 1000);
114    }
115  }
116}