Create Calendar events from sheet rows without doubles

TL;DR — Sync sheet rows to Google Calendar safely: store the event id on the row, create each event once, update it when the title or time changes, and skip everything else. Plus a soft NitroGAS note for createCalendarEventIfMissing.

The first version of every “sheet → calendar” script works beautifully. The second run puts every session on the calendar twice. The third run, three times. Then someone moves a session to Thursday in the sheet and the calendar still says Tuesday.

The fix is boring and reliable: give each row a memory. Store the event id on the row, create only when there isn’t one, and update only when something that matters changed.

TL;DR

  • Add an EventId column (and a small SyncedSig column) to the sheet
  • No event id → create the event, write the id back immediately
  • Event id present → compare title/start/end with what was last synced; update only if different
  • Unchanged rows are skipped without a single Calendar call
  • Soft helper name: createCalendarEventIfMissing (snippet + NitroGAS catalog)

Using the Apps Script editor a lot? NitroGAS drops free themes & snippets right into script.google.com — optional Co-Pilot when you want a boost.

Sheet shape

A Title | B Start | C End | D Guest | E EventId | F SyncedSig | G SyncNote

Start and End are real date-time cells. EventId, SyncedSig and SyncNote belong to the script; protect them or at least colour them grey so humans leave them alone.

What not to do

  • Don’t search the calendar by title to “find the existing event”; titles change and repeat
  • Don’t delete and recreate events on every run; guests get cancellation + invite spam, and any notes on the event vanish
  • Don’t write all event ids at the end of the run; if it times out halfway, the next run doubles everything it already created
  • Don’t build times from text like "11/12 3pm"; use date cells

The sync

var CAL_SHEET = 'Sessions'; function syncSessionsToCalendar() { var ss = SpreadsheetApp.getActive(); var sheet = ss.getSheetByName(CAL_SHEET); var calendarId = PropertiesService.getScriptProperties().getProperty('sessionsCalendarId'); // blank = default calendar var values = sheet.getDataRange().getValues(); values.shift(); // headers: Title | Start | End | Guest | EventId | SyncedSig | SyncNote var created = 0, updated = 0, skipped = 0; values.forEach(function (r, idx) { var rowNum = idx + 2; var title = String(r[0] || '').trim(); var start = r[1], end = r[2], guest = String(r[3] || '').trim(); var eventId = String(r[4] || ''), lastSig = String(r[5] || ''); if (!title || !(start instanceof Date) || !(end instanceof Date)) return; // incomplete row // A fingerprint of the fields we sync. Same fingerprint = nothing to do. var sig = [title, start.getTime(), end.getTime()].join('|'); if (eventId && sig === lastSig) { skipped++; return; } try { var res = createCalendarEventIfMissing({ calendarId: calendarId || undefined, eventId: eventId || undefined, title: title, start: start, end: end, guests: guest ? [guest] : [] }); if (res.created) { // Write the id and fingerprint right away: no doubles even if the run dies next row sheet.getRange(rowNum, 5, 1, 3).setValues([[res.eventId, sig, 'Created']]); created++; return; } // Existing event, but the row changed: update in place (keeps guests' RSVPs) var ev = res.event; if (ev.getTitle() !== title) ev.setTitle(title); if (ev.getStartTime().getTime() !== start.getTime() || ev.getEndTime().getTime() !== end.getTime()) { ev.setTime(start, end); } sheet.getRange(rowNum, 6, 1, 2).setValues([[sig, 'Updated']]); updated++; } catch (err) { sheet.getRange(rowNum, 7).setValue('Error: ' + err.message); } }); console.log('Calendar sync: %s created, %s updated, %s unchanged', created, updated, skipped); }

createCalendarEventIfMissing does the create-or-return part. Pass the stored eventId and it hands back the existing event, or creates a fresh one if there’s no id (or the old event was deleted). Either way you get an eventId to store.

Why a fingerprint column?

Without SyncedSig, every run would have to fetch every event just to discover nothing changed. That’s slow and burns Calendar quota. The fingerprint (title|startMs|endMs) lets unchanged rows skip with zero API calls. When someone edits the title or moves the time, the fingerprint no longer matches and that row, and only that row, gets updated.

Want guests or description changes to sync too? Add them to the fingerprint and handle them in the update branch.

Deleted events

If someone deletes the event in Calendar, getEventById returns nothing and the helper creates a new one on the next run that touches the row. If deleted should mean “cancelled,” add a Status column and skip (or remove) cancelled rows deliberately rather than relying on what’s in the calendar.

Run it

A menu item for “sync now,” plus a time-driven trigger for the background:

function onOpen() { SpreadsheetApp.getUi().createMenu('Calendar') .addItem('Sync sessions now', 'syncSessionsToCalendar') .addToUi(); } function installCalendarSync() { deleteTriggersByHandler('syncSessionsToCalendar'); ScriptApp.newTrigger('syncSessionsToCalendar').timeBased().everyHours(1).create(); }

Test against a throwaway calendar first (put its id in sessionsCalendarId), and leave guests blank until you trust it. Invites are real emails.

Failure modes worth naming

  • Wrong calendar: event ids only resolve in the calendar they were created in. Changing sessionsCalendarId later means new events everywhere; clear EventId deliberately if you migrate.
  • Script owner lacks access to a shared calendar: getCalendarById returns null and the helper throws. Share the calendar with edit rights.
  • Rows sorted or moved: fine. The id lives on the row and travels with it.
  • Copying a row: the copy carries the same EventId, and both rows now fight over one event. Clear EventId/SyncedSig on duplicated rows.
  • All-day sessions: this sketch uses timed events. All-day needs createAllDayEvent and date-only comparisons.

Soft Co-Pilot note

NitroGAS keeps createCalendarEventIfMissing a search away in the editor. Paste your header row and say which columns are title, start and end, and Co-Pilot can draft the sync with the event-id and fingerprint columns. You still test on a throwaway calendar. Free core; Co-Pilot is a pass for when you want the draft.

Closing checklist

  • EventId + SyncedSig columns, owned by the script
  • Event id written immediately after create
  • Unchanged rows skipped via fingerprint (no Calendar calls)
  • Changed rows updated in place, not recreated
  • Tested on a throwaway calendar with guests off

Happy Coding!