Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-apps

Google Script: Add guest to event (Google Calender) but it doesn't send a invitation email


Thats my code:

function addToEvent() {

 var guest = "";
 var calendar = '';
 var event = "";

 var cal = CalendarApp.getCalendarById(calendar);
 var theEvent = cal.getEventSeriesById(event); 

 theEvent.addGuest(guest); }

Everything works but it does not send an invitation mail to the guest. I know it works with ( sendUpdates: "all" ) but I dont know how to add that here :)

Thanks in Advance!


Solution

  • Try the below code. I have tested it and it works for me. Just make sure you have enabled the V3 of Calendar API in advanced services.

    function addGuestAndSendNotification(calendarId, eventId, newGuestEmailAddress) {
    
      eventId = eventId.substring(0, eventId.indexOf('@google.com'));
    
      var event = Calendar.Events.get(calendarId, eventId);
      var attendees = event.attendees;
      if (!attendees) attendees = [];
      attendees.push({ email: newGuestEmailAddress });
    
      var resource = { attendees: attendees };
      var args = { sendUpdates: "all" };
    
      Calendar.Events.patch(resource, calendarId, eventId, args);
    }