To use this script, follow these steps:
- Open a Google Sheet that you want to use to trigger the creation of the calendar event.
- Go to the “Tools” menu and select “Script editor.”
- Copy and paste the code provided into the script editor.
- Modify the calendarId, eventTitle, startTime, endTime, and eventDescription variables to match your desired settings.
- Save the script and close the script editor.
- In the Google Sheet, add a trigger to run the createCalendarEvent function on a specified schedule or event trigger.
When the createCalendarEvent function is triggered, it will create a new calendar event in the specified calendar with the specified details. The event ID will be logged to the console, which can be useful for tracking and referencing the event in future script executions.
Paste this code below in Apps Script:
function createCalendarEvent() {
var calendarId = 'primary'; // ID of the calendar to create the event in
var eventTitle = 'Example Event'; // Title of the new event
var startTime = new Date('2023-04-05T09:00:00-07:00'); // Start time of the event
var endTime = new Date('2023-04-05T10:00:00-07:00'); // End time of the event
var eventDescription = 'This is an example event.'; // Description of the new event
var event = CalendarApp.getCalendarById(calendarId).createEvent(eventTitle, startTime, endTime, {
description: eventDescription
});
// Log the event ID to the console
Logger.log('Event ID: ' + event.getId());
}