Create calendar events
CalendarApp.getDefaultCalendar().createEvent(title, startDate, endDate, {description, location, guests, sendInvites}) adds an event and returns it; createAllDayEvent(title, date) makes an all-day one. Read dates from the sheet as Date objects, build the end time with new Date(start.getTime() + minutes * 60000), and save event.getId() in the sheet so a rerun updates instead of duplicating.
Syntax
calendar.createEvent(title, startTime, endTime, options) | calendar.createAllDayEvent(title, date, options)| Part | What it is |
|---|---|
CalendarApp.getDefaultCalendar() | Your primary calendar; getCalendarById(id) for another, getCalendarsByName(name) |
startTime, endTime | Date objects; the end must be after the start |
guests | Comma-separated email string; sendInvites: true emails them |
event.getId() | Id to find it again with calendar.getEventById(id) |
Examples
const cal = CalendarApp.getDefaultCalendar();
const start = new Date(2026, 10, 12, 14, 0);
const end = new Date(2026, 10, 12, 15, 0);
const ev = cal.createEvent("Budget review", start, end, {location: "Room 2", description: "Bring the Q4 file"});
Logger.log(ev.getId());Result: An event on 12 November 2026 from 14:00 to 15:00; its id is logged
Month index is 0-based: 10 means November.
const sheet = SpreadsheetApp.getActiveSheet();
const rows = sheet.getRange(2, 1, sheet.getLastRow() - 1, 5).getValues();
rows.forEach((r, i) => {
const [title, start, minutes, guests, id] = r;
if (id) return;
const end = new Date(start.getTime() + minutes * 60000);
const ev = cal.createEvent(title, start, end, {guests: guests, sendInvites: true});
sheet.getRange(i + 2, 5).setValue(ev.getId());
});Result: One event per row without an id; column E stores each new event id
cal.createAllDayEvent("Office closed", new Date(2026, 11, 25));Result: An all-day event on 25 December 2026
Common mistakes
- Event start time must be before event end time: the end Date is earlier or equal; check the minutes column is a number, not text.
- The event lands on the wrong hour: the sheet date is interpreted in the spreadsheet time zone and the script in the project time zone; set both to the same zone in settings.
- Rerunning the script creates duplicate events: store the event id in the sheet and skip rows that have one, or update with getEventById.
- Guests are not invited: pass sendInvites: true; without it the guests are added silently.
Practice Create calendar events
These drills check your answer on live data, free, no account needed.
- Add days to a date`isoDate` is a calendar date like `"2024-01-30"`. Return the date `n` days later (earlier when `n` is negative) in the same `YYYY-MM-DD` form. Work in UTC so the answer does not depend on the time zone of the computer, and get leap years right.
- Days between two dates`a` and `b` are calendar dates like `"2024-01-31"`. Return the number of days from `a` to `b`: positive when `b` is later, negative when it is earlier, 0 for the same date. The answer must be right in every time zone, including across daylight saving changes.
- Format a durationFormat a whole number of seconds like `"1d 2h 3m 4s"`, using days, hours, minutes and seconds, largest first. Leave out units that are zero. Zero seconds is `"0s"`.
Questions
How do I create an event in a shared calendar?
Use CalendarApp.getCalendarById("name@group.calendar.google.com") with the id from the calendar's settings; you need write access to it.
How do I update or delete an event from the script?
const ev = cal.getEventById(id); ev.setTime(newStart, newEnd); ev.setTitle(...); ev.deleteEvent();
Related
- Dates in Apps ScriptHandle dates in Google Apps Script: sheet dates arrive as Date objects, create dates with new Date(y, m, d), add days, days between, compare, time zone.
- Utilities.formatDateFormat dates in Google Apps Script with Utilities.formatDate: the time zone argument, Java pattern letters like yyyy MM dd HH mm, month names, MM versus mm.
- Loop through rowsLoop through rows in Google Sheets with Apps Script: read the data once with getValues, loop the 2D array with for, for...of or forEach, then write back.
- Time-driven triggersSchedule Google Apps Script with time-driven triggers: create them in the Triggers panel or with ScriptApp.newTrigger, run daily, hourly or weekly, delete.