Skip to content

Snippets · 11 functions

Google Calendar snippets

Each snippet is a complete function you can run on its own to try one Calendar task, from reading a shared calendar to coloring one event.

How to use these

Set up before you run

Copy a snippet into a script file, read its setup and notes, change the placeholder values, and save. For functions meant to run manually, choose the function name in the toolbar and press Run. Results appear in the Execution log unless the description says otherwise.

1

Get the default calendar and log its name

Reads the signed-in user's own calendar, the one you see first when you open Google Calendar.

Code.gs
function logDefaultCalendarName() {
  const calendar = CalendarApp.getDefaultCalendar();
  Logger.log('Default calendar: ' + calendar.getName());
}

2

List today's events

Logs the title of every event on the default calendar for the current day.

Code.gs
function logTodaysEvents() {
  const calendar = CalendarApp.getDefaultCalendar();
  const events = calendar.getEventsForDay(new Date());
  events.forEach((event) => Logger.log(event.getTitle()));
  Logger.log('Events today: ' + events.length);
}

3

List events between two dates

Logs every event on the default calendar for the next 7 days, useful for a weekly digest.

Code.gs
function logEventsInDateRange() {
  const calendar = CalendarApp.getDefaultCalendar();
  const startDate = new Date();
  const endDate = new Date();
  endDate.setDate(endDate.getDate() + 7);
  const events = calendar.getEvents(startDate, endDate);
  events.forEach((event) => Logger.log(event.getTitle()));
  Logger.log('Events in range: ' + events.length);
}

Good to know. A long date range can return a lot of events, so keep the range as short as the task needs.

4

Create a timed event

Creates a one hour event on the default calendar starting at 2 PM tomorrow.

Code.gs
function createTimedEvent() {
  const calendar = CalendarApp.getDefaultCalendar();
  const startTime = new Date();
  startTime.setDate(startTime.getDate() + 1);
  startTime.setHours(14, 0, 0, 0);
  const endTime = new Date(startTime.getTime() + 60 * 60 * 1000);
  const event = calendar.createEvent('Project check-in', startTime, endTime);
  Logger.log('Created event: ' + event.getId());
}

5

Create an all-day event

Creates a one-day, all-day event a week from today, useful for deadlines and reminders.

Code.gs
function createAllDayReminder() {
  const calendar = CalendarApp.getDefaultCalendar();
  const eventDate = new Date();
  eventDate.setDate(eventDate.getDate() + 7);
  const event = calendar.createAllDayEvent('Quarterly report due', eventDate);
  Logger.log('Created all-day event: ' + event.getId());
}

6

Create an event with a description and location

Passes an options object to createEvent to fill in details beyond the title and time.

Code.gs
function createEventWithDetails() {
  const calendar = CalendarApp.getDefaultCalendar();
  const startTime = new Date();
  startTime.setDate(startTime.getDate() + 2);
  startTime.setHours(10, 0, 0, 0);
  const endTime = new Date(startTime.getTime() + 30 * 60 * 1000);
  const event = calendar.createEvent('Client call', startTime, endTime, {
    description: 'Review the proposal before the call.',
    location: 'Conference Room B'
  });
  Logger.log('Created event: ' + event.getTitle());
}

7

Add a guest to an event

Looks up one event by ID and adds a guest to it.

Code.gs
function addGuestToEvent() {
  const calendar = CalendarApp.getDefaultCalendar();
  const event = calendar.getEventById('REPLACE_WITH_EVENT_ID');
  if (!event) throw new Error('No event found for that ID.');
  event.addGuest('you@example.com');
  Logger.log('Guest added to: ' + event.getTitle());
}

Good to know. Adding a guest can send that person an email invitation, so only add addresses that expect it.

8

Find events by title in a range

Reads events for the next 30 days with getEvents, then filters them down to an exact title match.

Code.gs
function findEventsByTitle() {
  const calendar = CalendarApp.getDefaultCalendar();
  const startDate = new Date();
  const endDate = new Date(startDate.getTime() + 30 * 24 * 60 * 60 * 1000);
  const matchingTitle = 'Budget review';
  const matches = calendar.getEvents(startDate, endDate).filter((event) => event.getTitle() === matchingTitle);
  Logger.log('Matches found: ' + matches.length);
}

9

Read an event's start, end, and whether it is all day

Looks up one event by ID and logs its start time, end time, and all-day flag.

Code.gs
function logEventTiming() {
  const calendar = CalendarApp.getDefaultCalendar();
  const event = calendar.getEventById('REPLACE_WITH_EVENT_ID');
  if (!event) throw new Error('No event found for that ID.');
  Logger.log('Start: ' + event.getStartTime());
  Logger.log('End: ' + event.getEndTime());
  Logger.log('All day: ' + event.isAllDayEvent());
}

10

Get a calendar by ID for a shared calendar

Opens a calendar someone else shared with you, using its calendar ID instead of the default calendar.

Code.gs
function logSharedCalendarName() {
  const calendar = CalendarApp.getCalendarById('REPLACE_WITH_CALENDAR_ID');
  if (!calendar) throw new Error('No calendar found for that ID. Check that it is shared with you.');
  Logger.log('Shared calendar name: ' + calendar.getName());
}

Good to know. Find the ID for a shared calendar under its Settings and sharing page, in the Integrate calendar section.

11

Set an event color

Sets an event's color using the numeric color ID Google Calendar uses internally.

Code.gs
function setEventColor() {
  const calendar = CalendarApp.getDefaultCalendar();
  const event = calendar.getEventById('REPLACE_WITH_EVENT_ID');
  if (!event) throw new Error('No event found for that ID.');
  event.setColor('9');
  Logger.log('Color set for: ' + event.getTitle());
}

Good to know. Color IDs are the strings 1 through 11. The EventColor enum in the Calendar service reference names each one; 9 is blue.

Keep going

See these in a complete task

All snippet collections