Skip to content

Add an event

Google Apps Script has a built-in CalendarApp class, a library for working with Google Calendar.

This article explains how to use the CalendarApp class to add an event to a calendar.

Because GAS accesses Google Calendar, you need to grant permissions the first time you run the script.

To add an event to a calendar, you first need to get the calendar you want to add it to.

For how to get a calendar, see this article.

To add an event, use the following method.

Calendar.createEvent([event title], [start date and time], [end date and time], [options]);
Add a new event
const CALENDAR_ID = '___ID_of_the_target_calendar___';
function myFunction() {
const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
if (!calendar) {
console.log('The calendar was not found');
return;
}
const from = new Date();
const to = new Date();
from.setHours(from.getHours() + 1);
to.setHours(to.getHours() + 2);
calendar.createEvent('Event added from Google Apps Script', from, to);
}

When you run it, an event is created that starts 1 hour after the time you ran the script and ends 2 hours after it.

The fourth argument, options, is optional.

You can specify the following items.

Option details
calendar.createEvent(title, from, to, {
description: 'Event description',
location: 'Event location',
guests: 'xxx@gmail.com,yyy@gmail.com', // Comma-separated email addresses to invite as guests
sendInvites: false, // If true, invitation emails are sent (default: false)
});

This section shows how to add an event for a whole day, without specifying a time.

To add the event, use the following method.

Calendar.createAllDayEvent([event title], [date]);
Add a new all-day event
const CALENDAR_ID = '___ID_of_the_target_calendar___';
function myFunction() {
const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
if (!calendar) {
console.log('The calendar was not found');
return;
}
const date = new Date();
calendar.createAllDayEvent('All-day event added from Google Apps Script', date);
}

When you run it, an all-day event is created for the day you ran the script. Only the date part of the argument is used, and the time is ignored.