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.
Prerequisites
Section titled “Prerequisites”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.
Add an event
Section titled “Add an event”Method to use
Section titled “Method to use”To add an event, use the following method.
Calendar.createEvent([event title], [start date and time], [end date and time], [options]);Sample code
Section titled “Sample code”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.
Options
Section titled “Options”The fourth argument, options, is optional.
You can specify the following items.
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)});Add an all-day event
Section titled “Add an all-day event”This section shows how to add an event for a whole day, without specifying a time.
Method to use
Section titled “Method to use”To add the event, use the following method.
Calendar.createAllDayEvent([event title], [date]);Sample code
Section titled “Sample code”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.