0

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);

2 Answers2

0

As you can read in the documentation, you can add guests with the option object, which has the following parameters:

enter image description here

You are actually already using this object in your code:

{sendInvites:true});

So adding the field guests to your object will be enough to add attendees:

{guests:'test1@example.com, test2@example.com', sendInvites:true});
Jescanellas
  • 2,555
  • 2
  • 9
  • 20
0

It looks like you don't want to add guests to the event at the time of creation. Therefore you will end up with an event without an attendees[] property in the Event resource.

When you do come to add an attendee, you can do it like this:

    // Invite 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);
      
    var attendees = advancedEvent.attendees;
      if (attendees) {
        // If there are already attendees, push the new attendee onto the list
        attendees.push({email: guestEmailAddress});
        var resource = { attendees: attendees };
      } else {
        // If attendees[] doesn't exist, add it manually (there's probably a better way of doing this)
        var resource = { attendees: [{email: guestEmailAddress}] }
      }

      var args = { sendUpdates: "all" };
      
      var eventObjectForDebugging = Calendar.Events.patch(resource, programmeCalendarID, trimmedEventID, args);
      console.log('Event object for debugging:' + eventObjectForDebugging);

David
  • 1