To integrate Google Calendar with Apps Script and create events programmatically, you can use the Calendar service provided by Google Apps Script. Here’s an example of how to accomplish this:
Open your Apps Script project in the Apps Script editor.
In the editor, click on the “Services” button (represented by a puzzle piece icon) in the toolbar.
In the “Services” dialog, search for “Calendar” and click on the “Calendar” service.
Click on the “Add” button to add the Calendar service to your project.
Close the “Services” dialog.
Write a function to create an event in Google Calendar. Here’s an example:
function createCalendarEvent() { var calendarId = 'primary'; // Replace with the desired calendar ID var event = { summary: 'My Event', start: { dateTime: '2023-05-31T10:00:00', timeZone: 'America/New_York' }, end: { dateTime: '2023-05-31T11:00:00', timeZone: 'America/New_York' }, description: 'This is a test event created using Apps Script.' }; var calendar = CalendarApp.getCalendarById(calendarId); var createdEvent = calendar.createEvent(event.summary, new Date(event.start.dateTime), new Date(event.end.dateTime), { description: event.description }); Logger.log('Event created: ' + createdEvent.getId()); }
In this example, the createCalendarEvent()
function creates a new event in Google Calendar. Adjust the values of summary
, start.dateTime
, end.dateTime
, and description
to match the details of your event.
The calendarId
variable represents the calendar where the event will be created. By default, 'primary'
refers to the primary calendar of the authenticated user. You can change it to a specific calendar ID if needed.
The CalendarApp.getCalendarById(calendarId)
method retrieves the calendar based on the calendarId
.
The createEvent()
method is used to create the event in the calendar. It takes the event details as parameters.
The created event’s ID is logged using Logger.log()
, but you can customize this part according to your needs.
- Save your code and run the
createCalendarEvent()
function. It will create the event in your Google Calendar.
Make sure that you have the necessary permissions to access and modify the target Google Calendar.
By integrating Google Calendar with Apps Script, you can programmatically create events, set reminders, and perform other calendar-related tasks, providing automation and efficiency in managing events and schedules.