Snippets · 11 functions
Google Calendar snippets
Eleven short functions for common Calendar tasks: reading the default or a shared calendar, creating events, adding a guest, and setting an event color.
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 your own calendar, the one you see first when you open Google Calendar.
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.
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.
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.
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, such as for a deadline.
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.
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.
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.
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.
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.
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.
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. For example, 9 is blue.
Keep going
See these in a complete task
- Task guide Create Calendar events from Sheet rows
Turn rows on an Events tab into events on your Google Calendar. The script creates an event for each row that has no event ID yet, then writes the new ID back into the row so a later run skips it.
- Task guide Email yourself tomorrow's calendar agenda
Get an email each evening listing tomorrow's events on your main Google Calendar. Run the setup function once to install the daily trigger.
- Basics Dates, time zones, and Utilities.formatDate
A Date holds one instant in time. Use Utilities.formatDate(date, timeZone, pattern) and Session.getScriptTimeZone() to format it in the right time zone.
- Basics Google Apps Script triggers explained
Google Apps Script triggers run functions automatically when a file opens, a cell changes, a form is submitted, or a scheduled time arrives. Use a simple onEdit or onOpen for work in the bound file. Choose an installable trigger for a schedule, form submission, or work that needs authorization.