繁体   English   中英

CalendarEvent是哪种对象?

[英]What kind of object is a CalendarEvent?

我正在编写一个脚本,该脚本将从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应该是哪种对象? 我怀疑它是它自己的结构化对象,还是仅包含事件的标题,时间,描述等的数组? (正如您在上面看到的,我尝试将其视为数组,但无济于事。)

在这堵墙上扔了几乎所有东西几天后,我似乎偶然发现了一个解决方案! 如果有人在此发布,则会遇到此特定问题。

我无法解释原因,但以下工作原理可以解决:

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

将工作和事件显式地视为数组似乎有很大的帮助……或者这可能与它无关。 我所知道的是,这现在从正确的事件中提取事件标题。 (不过,我仍然需要对多个日历中的多个事件进行测试。)我还更改了上面的代码,以在事件数组中仅包含calendarEvents,而不是一个calendarEvent和30个null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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