I have followed the answer here, which was really helpful:
How do I send the standard invitation email when calling addGuest on a CalendarEvent?
However, when I create an event in Apps Script it doesn't have an attendees property to add the email address to.
If I add a person to the invite manually, and then run the code, it executes successfully because there is now an attendee property to update.
Code to create the event:
function createCalendarEntry(programmeCalendar, eventName, eventStartDateTime, eventEndDateTime) {
var programmeCalendarID = CalendarApp.getCalendarById(programmeCalendar);
var event = programmeCalendarID.createEvent(eventName,
new Date(eventStartDateTime),
new Date(eventEndDateTime),
{sendInvites:true});
console.log('Event ID: ' + event.getId());
//Stop guests inviting other people
event.setGuestsCanInviteOthers(false);
var eventID = event.getId();
return eventID;
}
Code to add a new guest:
// Invite the user to the given event using the Advanced Calendar API
var trimmedEventID = eventID.toString().replace('@google.com', '');
var programmeCalendarID = 'my-calendar@example.com';
var advancedEvent = Calendar.Events.get(programmeCalendarID, trimmedEventID);
console.log('Advanced Event:' + advancedEvent);
if (typeof advancedEvent.attendees === 'undefined') {
console.log('Advanced Event Attendees: is undefined');
// how can I add the attendee property here?
} else {
var attendees = advancedEvent.attendees;
console.log('Advanced Event Attendees: is defined' + attendees);
attendees.push({email: guestEmailAddress});
var resource = { attendees: attendees };
}
var args = { sendUpdates: "all" };
// Add the guest to the invite while also sending an invite email
var eventObjectForDebugging = Calendar.Events.patch(resource, programmeCalendarID, trimmedEventID, args);
console.log('Event object for debugging:' + eventObjectForDebugging);