Sending multiple Google calendar invites using Google apps script

172 Views Asked by At

Following my previous question, I was able to send a Google calendar invite using the script proposed by @Tanaike:

function testNotification(){

         var calendarId = "###";
         var eventId = "###";
         var email = "###@gmail.com"
         addGuestAndSendEmail(calendarId,eventId,email)
       }
           
        function addGuestAndSendEmail(calendarId, eventId, newGuest) {
           Calendar.Events.patch({ attendees: [{ email: newGuest }] }, calendarId, eventId, { sendUpdates: "all" });
    }

However, there is a slight glitch that I am not able to identify. When I try to send invites to multiple email addresses at the same time, it behaves unusually. Here is the new script:

      function SendMultiple(calendarId, eventId) {
    
                newGuests = ["[email protected]","[email protected]"];
                newGuests.forEach(function(e){
                 Utilities.sleep(10000);
                Calendar.Events.patch({ attendees: [{ email: e.toString()}] }, calendarId, eventId, { sendUpdates: "all" });
  });    
}

Output:

when the SendMultiple() function finishes running, it sends 2 invites (event created, event canceled) to [email protected] and 2 invites (event created, event canceled) to [email protected], I am unable to identify why the event canceled invite is generated using this script. If I interchange the emails in newGuests array:

newGuests = ["[email protected]","[email protected]"];

then it behaves the same, I would appreciate it if you help me identify the issue, thank you

2

There are 2 best solutions below

3
Tanaike On BEST ANSWER

In your script, how about modifying as follows?

Modified script:

function SendMultiple(calendarId, eventId) {
  var newGuests = ["[email protected]", "[email protected]"];
  Calendar.Events.patch({ attendees: newGuests.map(email => ({ email })) }, calendarId, eventId, { sendUpdates: "all" });
}
  • In your script, Calendar.Events.patch is run with sendUpdates: "all". I thought that this might be the reason for your current issue. By this modification, 2 users are added by one API call. I thought that this modification might be able to remove your current issue.
0
Carlos Medina Gallego On

This worked for me when creating an event.

To sum up, it seems it still needs the old parameter sendNotifications set to true.

// event details for creating event.
linvitados = []
for (var i = 0; i < invitados.length; i++) {
  linvitados.push({'email': invitados[i]});
}
for (var i = 0; i < opcionales.length; i++) {
  linvitados.push({'email': opcionales[i], 'optional':true});
}
let event = {
  summary: titulo,
  location: location,
  description: detalles,
  start: {
    dateTime: start.toISOString()
  },
  end: {
    dateTime: end.toISOString()
  },
  attendees: linvitados,
  sendUpdates : 'all',
  sendNotifications : true,
};
if (meet) {
  conferenceDataVersion = 1
          event['conferenceData'] = {
              'createRequest': {
                  'requestId'            : (new Date()).getTime(),
                  'conferenceSolutionKey': {'type':'hangoutsMeet'}
              }
          }
}
else {
  conferenceDataVersion = 0
}
try {
  // call method to insert/create new event in provided calandar
  event = Calendar.Events.insert(event, calendarId, {conferenceDataVersion: conferenceDataVersion, sendUpdates : 'all', sendNotifications : true});
  console.log('Event ID: ' + event.id);
  //Calendar.Events.patch(event, calendarId, event.id, {attendees: linvitados, sendUpdates: "all" });
} catch (err) {
  console.log('Failed with error %s', err.message);
}