CalendarEvent是什么类型的对象?

时间:2014-05-21 20:10:51

标签: javascript google-apps-script google-calendar-api

我正在编写一个脚本,可以从Google日历中抽取一天的事件,并最终制作电子邮件议程。我正在调整https://developers.google.com/apps-script/templates的摘录。

// Get today's events
var now = new Date();
var morning = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var night = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999)
var cals = [[in the real code, this contains about 30 valid, properly structured calendar IDs with commas and single quotes in all the right places]];
Logger.log(cals.length); //double-checking number to ensure that I didn't miss a calendar
//cycle through calendars and get events
var events = [];
var working;
var currentCal;
for (var i=0; i<cals.length; i++){
  currentCal = cals[i];
  working = CalendarApp.getCalendarById(currentCal).getEvents(morning,night);
  Logger.log(working.getTitle());
  events.push(working[0]);
}
Logger.log(events);

我在倒数第二个Logger.log调用中尝试了各种策略。最初,它只是记录工作。问题是,我不能让它停止记录“CalendarEvent” - 字符串,就像它在这里 - 而不是实际的日历事件。事件记录为空数组和偶尔的“CalendarEvent”。我怀疑我在这里忽略了一些愚蠢的东西,就像实际上“工作”的对象类型一样。

那么,CalendarEvent应该是什么样的对象?是,我怀疑它是自己的结构化的东西,还是只是一个包含事件的标题,时间,描述等的数组? (如上所述,我尝试将其视为一个数组,但无济于事。)

1 个答案:

答案 0 :(得分:0)

在这个特殊的墙壁上投掷几乎所有东西几天之后,我似乎偶然发现了一个解决方案!在此处发布,以防其他人遇到此特定问题。

我无法解释原因,但以下有效:

// Get today's events
var now = new Date();
var morning = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var night = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999)
var cals = [properly formatted array of about 30 different calendar IDs];
Logger.log(cals.length); //double-checking number of employees to ensure that I didn't miss a calendar
//cycle through calendars and get events
var events = [];
var working = [];
var currentCal;
for (var i=0; i<cals.length; i++){
  currentCal = cals[i];
  working = CalendarApp.getCalendarById(currentCal).getEvents(morning,night); 
if (working.length != 0) {
  Logger.log("Working "+ working[0]);
  events.push.apply(events,working);
  Logger.log("First event "+events[0]);
  Logger.log(events[0].getTitle());
}}

明确地将工作和事件视为数组似乎有很大帮助......或者说它本来没有任何关系。我所知道的是,这现在从正确的事件中拉出一个事件标题。 (我仍然需要在多个日历中使用多个事件来测试它。)我还更改了上面的代码,在events数组中只包含calendarEvents,而不是一个calendarEvent和30个null。