How to use getGuestList to get Names, not email ad

2019-09-06 17:03发布

问题:

I've found this script (posted by Jonathon), and need to modify it to provide user names.

When I try to get the guest names (using getGuestList()) it returns EventGuest in my spreadsheet.

How can I get the guest names (not emails) into the spreadsheet?

function caltest3(){

var ss = SpreadsheetApp.openById( 'spreadsheetId' ),
  sheet = ss.getSheetByName( 'sheetName' ),
  cals = ['id1', 'id2', 'id3'], c, cal, calName,
  start = new Date( 'whenever' ), end = new Date( 'whenever' ),
  events, i, details,
  eventslog = [], e,
  rows = [], range;

for (c = 0; c < cals.length; c += 1) {

cal = CalendarApp.getCalendarById(cals[c]);
calName = cal.getTitle();
events = cal.getEvents(start, end);

// add the events of the current calendar to the array of all events
  eventslog = eventslog.concat(
   events.map(function(event) {
     return {
       time: new Date(event.getStartTime()).getTime(), // sort by this
        details: [
         event.getTitle(),
         event.getStartTime(),
         event.getEndTime(),
         event.getDescription(),
         event.getLocation(),
         calName // change calendar info position in array to suit
        ]
      };
    })
  );
 }

// sort array of event so date order can be either way by reversing a & b
eventslog.sort(function(a, b) { return a.time - b.time; });

rows = eventslog.map(function(entry) { return entry.details; });

range = sheet.getRange(2, 1, rows.length, 6);
range.setValues(rows);
}

回答1:

You need to iterate over the guests returned by Event.getGuestList(), and retrieve each name with EventGuest.getName(), documented here. You should validate that the name isn't blank, and perhaps settle for the email in that case - I'll leave that to you.

Here's how your function would change:

function caltest3(){

var ss = SpreadsheetApp.openById( 'spreadsheetId' ),
  sheet = ss.getSheetByName( 'sheetName' ),
  cals = ['id1', 'id2', 'id3'], c, cal, calName,
  start = new Date( 'whenever' ), end = new Date( 'whenever' ),
  events, i, details,
  eventslog = [], e,
  rows = [], range;

for (c = 0; c < cals.length; c += 1) {

cal = CalendarApp.getCalendarById(cals[c]);
calName = cal.getTitle();
events = cal.getEvents(start, end);

// add the events of the current calendar to the array of all events
  eventslog = eventslog.concat(
   events.map(function(event) {
     var deets =  {
       time: new Date(event.getStartTime()).getTime(), // sort by this
        details: [
         event.getTitle(),
         event.getStartTime(),
         event.getEndTime(),
         event.getDescription(),
         event.getLocation(),
         calName // change calendar info position in array to suit
        ]
      };
      var guestList = event.getGuestList();
      event.guests = [];
      for (var i in guestList) {
        event.guests.push(guestList[i].getName());
      } 
      deets.guests = guestList;
      return deets;
    })
  );
 }