Google脚本类CalendarEvent

问题描述 投票:0回答:1

我对编码非常基础,如果您觉得这很明显,请对不起。我的请求是如何使用类CalendarEvent getGuestList获取“来宾”的名称?https://developers.google.com/apps-script/reference/calendar/calendar-event#getGuestList%28%29

代码测试1:

// Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) 
{
var row=i+2;
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below
var details=[[events[i].getTitle(),events[i].getStartTime(),events[i].getLocation(),events[i].getGuestList(),events[i].getDescription()]];
var range=sheet.getRange(row,1,1,5);
range.setValues(details);
}

问题代码1:我的“ guestName”结果为:EventGuest(而不是名称或电子邮件地址)

如果我尝试此代码,请进行代码测试2:

//Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) 
{
var row=i+2;
//Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below

// trying to get guest name
var Nameguest = events[i].getGuestList(); 

var details=[[events[i].getTitle(),events[i].getStartTime(),events[i].getLocation(),**Nameguest[i].getName()**,events[i].getDescription()]];
var range=sheet.getRange(row,1,1,5);
range.setValues(details);
}

问题代码2:它只写第一行并显示正确的名称,但是脚本停止并提供此错误:“ TypeError:无法调用未定义的方法“ getName”。(第xx行,文件“”)关闭“

希望这是一个简单的答案:)

谢谢你。

google-apps-script google-calendar-api
1个回答
0
投票

.getGuestList()返回一个必须与外部事件循环分开循环的数组。

[Nameguest[i]使用与仅i应该使用的相同events计数器。

代替尝试:

var names = "";
var guestList = events[i].getGuestList();
for ( var n = 0; n < guestList.length; n++ ) {
  var name = guestList[n].getName(); 
  if ( name ) {
    names += name + ', ';
  }
}

var details=[[events[i].getTitle(), ..., names, ...]];
© www.soinside.com 2019 - 2024. All rights reserved.