We have a script that we connect to the Google Sheets responses table of a form that allows us to send an invitation to a calendar entry.
It seems to be really hit and miss, some people are able to get it working first time and I'm now trying to run it but keep getting the error message in the execution log:
TypeError: Cannot read property 'push' of undefined
at addGuestAndSendEmail(Code:47:13)
at addGuest(Code:39:3)
This is the code (with placeholders - which I've double and triple checked) and I've also made sure to turn on access and grant permission for the Calendar API service. Any help would be really appreciated - I've just hit a total block on where to go!
// Add a trigger - add guest on form submit
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("addGuest")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function addGuest(e) {
// Find your calendar ID in "Settings and sharing" > "Integrate Calendar". Mine was my email address.
var calendarId = "EMAIL ADDRESS GOES HERE";
var email = e.namedValues['Email'].toString();
// This will only work for standalone events, or the first in an event series (recurring event).
// To find eventId: https://stackoverflow.com/a/46428456
var eventId = "EID GOES HERE";
addGuestAndSendEmail(calendarId, eventId, email);
}
// Note: requires advanced API https://stackoverflow.com/a/55509409
// If an Event Series, this will only work for the the first event
function addGuestAndSendEmail(calendarId, eventId, newGuest) {
var event = Calendar.Events.get(calendarId, eventId);
var attendees = event.attendees;
attendees.push({email: newGuest});
var resource = { attendees: attendees };
var args = { sendUpdates: "all" };
Calendar.Events.patch(resource, calendarId, eventId, args);
}