Why your sheet creates duplicate Google Calendar™ events, and how to stop it

A common first script for Google Sheets™ loops over the rows and calls createEvent() for each one. It works the first time. Run it again, or let a trigger run it, and every event appears twice. Run it a third time and you have three. This page explains why, gives a free Apps Script fix, and lists the cases that fix still doesn't cover.

Why it happens

The script has no memory. Nothing in the sheet records which event a row already created, so on every run it creates a new one. Matching by title and date ("does an event with this name exist that day?") helps a little, but breaks as soon as you rename an event or move it to another day: the old event stays and a new one appears.

The fix: store each event's ID in the row

When the script creates an event, write the event's ID into a spare column. On the next run, if the row has an ID, update that event instead of creating a new one. This version also takes a lock, so two runs that overlap (for example a trigger firing while you run it by hand) can't both create the same event.

/**
 * Sync rows to Google Calendar without duplicates.
 * Tab "Events": A Title | B Start | C End | D Event ID (leave D empty; the script fills it).
 * Format column D as plain text (Format > Number > Plain text).
 */
function syncRowsToCalendar() {
  const lock = LockService.getDocumentLock();
  if (!lock.tryLock(30000)) return;            // another run is still going
  try {
    const sheet = SpreadsheetApp.getActive().getSheetByName('Events');
    const cal = CalendarApp.getDefaultCalendar();
    const lastRow = sheet.getLastRow();
    if (lastRow < 2) return;
    const rows = sheet.getRange(2, 1, lastRow - 1, 4).getValues();
    rows.forEach(([title, start, end, id], i) => {
      if (!title || !(start instanceof Date) || !(end instanceof Date)) return;
      const event = id ? cal.getEventById(id) : null;
      if (event) {
        event.setTitle(title);                   // row already has an event: update it
        event.setTime(start, end);
      } else {
        const created = cal.createEvent(title, start, end);
        sheet.getRange(i + 2, 4).setValue(created.getId());
        SpreadsheetApp.flush();                  // save the ID right away
      }
    });
  } finally {
    lock.releaseLock();
  }
}

To use it: in your sheet choose Extensions → Apps Script, paste the code, save, and run syncRowsToCalendar once to authorise it. To run it automatically, add a time-driven trigger (Triggers → Add trigger). Start and End must be date-time cells.

What this script still doesn't handle

The no-code option

Calendupe is a Google Sheets add-on that does the "remember each row's event" part for you, in both directions:

Already have duplicates from an old script? Calendupe only manages events it created or that you exported into a sheet, so it won't remove those for you. The easiest clean start is a new calendar: create one in Google Calendar, pick it as Calendupe's default calendar, sync, and then delete the old calendar once you have checked the new one.

Next: set up Calendupe in five minutes.

Get Calendupe on the Marketplace All guides

Calendupe is free during launch, with every feature included. Questions? Email support@calendupe.com.