0

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 = ["user1@gmail.com","user2@gmail.com"];
                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 user1@gmail.com and 2 invites (event created, event canceled) to user2@gmail.com, I am unable to identify why the event canceled invite is generated using this script. If I interchange the emails in newGuests array:

newGuests = ["user2@gmail.com","user1@gmail.com"];

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

2 Answers2

0

In your script, how about modifying as follows?

Modified script:

function SendMultiple(calendarId, eventId) {
  var newGuests = ["user1@gmail.com", "user2@gmail.com"];
  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.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • it sends google invitations alright, however when I add my personal email address to `newGuests` array, it does not send a notification to it, is it the google policy or something I am missing? thank you –  Mar 06 '23 at 01:17
  • @Roomi About your new question, I would like to support you. But the issue of your comment is new issue, and that is different from your question. So can you post it as new question by including the detail information? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Mar 06 '23 at 01:19
  • Thank you, doing it now. –  Mar 06 '23 at 01:21
0

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