简体   繁体   中英

Create dynamic list of attendees in Google Apps Script calendar event

Trying to add a list of comma separated email addresses to a Google Apps Script in order to email those people of a new calendar event.

Seems that I can statically include emails using the format

attendees: [
  {email: 'alice@example.com'},
  {email: 'bob@example.com'}
],

But I cannot figure out how to take a csv string and convert those entries to a list of email addresses.

var calId = 'xxxx@resource.calendar.google.com';
  var event = {
    sendNotifications: true,
    sendUpdates: "all",
    summary: summary, 
    start: {
      date: eventDate
    },
    end: {
      date: eventDate
    }
  };

  var adds = response.getResponseText();
  if( adds.length > 0 ){
    //Create Mail List
    var addresses = adds.split(',');
    for( var i=0; i < addresses.length; i++){
      var addee=addresses[i].trim();
      Logger.log(addee);
      event.attendees.push( "email: "+addee);
    }
  event = Calendar.Events.insert( event, calId, {sendNotifications: true} 
);

It seems to me that your problem could comes from :

event.attendees = new List<EventAttendee>();
...
event.attendees.email=addee;

In my understanding, this array will be empty, and it's probably not possible to add email this way... I didn't try it but this code looks better :

    ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
...
    attendees.add(new EventAttendee().setEmail(addee)); // in the loop
...
    event.setAttendees(attendees);

I know it's late but maybe the answer could help someone else. You need to do 2 things: 1. you need to add the attendees as a property id inside "event" so that when you try to push an element, it won't say 'undefined'. Something like this:

var event = {
summary: subject,
location: dir,
description: (part1+'\n'+part2linebreak),
start: {
  dateTime: start.toISOString()
},
end: {
  dateTime: end.toISOString()
},
attendees: [

  ],
// Red background. Use Calendar.Colors.get() for the full list.
colorId: 11
};

attendees is created, but it's empty.

2.Now we enter data into 'attendees' like this:

for(var i=0; i<mailist.length; i++){
event.attendees.push({email: mailist[i]});
}

at least that how I did it :) also sendUpdates: "all" or sendNotifications: true go outside the event, like this:

event = Calendar.Events.insert(event, calid,{senUpdates: "all"});

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM