Google App Script .getEvents 问题 - 如何检索事件

Google App Script .getEvents issue - How to retrieve events

我正在尝试使用 GAS 做一些基本的事情。我想从特定日历中检索事件,但每次返回值为 0(这是不可能的,因为我的日历充满了事件)。

我检查了 GAS 编辑器和我的日历中的设置。所有时区都设置​​为 GMT+01。

function SearchEventForFirstCall() {

 var now = new Date();
 Logger.log('NOW: ' + now);
 var nowPlusOneMonth = new Date(now.setMonth(now.getMonth()+1));
 Logger.log('DATE PLUS 1 MONTH: ' + nowPlusOneMonth);

 var calendar = CalendarApp.getCalendarById('123456789@group.calendar.google.com');
 Logger.log('CALENDAR: ' + calendar);
 var events = calendar.getEvents(now,nowPlusOneMonth);
 Logger.log('EVENTS: ' + events.length);

}

这是文字记录:

1[19-11-28 20:43:54:617 CET] Starting execution
2[19-11-28 20:43:54:706 CET] Logger.log([NOW : Thu Nov 28 2019 20:43:54 GMT+0100 (CET), []]) [0 seconds]
3[19-11-28 20:43:54:706 CET] Logger.log([DATE PLUS 1 MONTH : Sat Dec 28 2019 20:43:54 GMT+0100 (CET), []]) [0 seconds]
4[19-11-28 20:43:54:850 CET] CalendarApp.getCalendarById([123456789@group.calendar.google.com]) [0.142 seconds]
5[19-11-28 20:43:54:850 CET] Logger.log([CALENDAR : Calendar, []]) [0 seconds]
6[19-11-28 20:43:55:094 CET] CalendarApp.Calendar.getEvents([Sat Dec 28 11:43:54 PST 2019, Sat Dec 28 11:43:54 PST 2019]) [0.242 seconds]
7[19-11-28 20:43:55:095 CET] Logger.log([Events: 0, []]) [0 seconds]
8[19-11-28 20:43:55:097 CET] Execution succeeded [0.391 seconds total runtime]

为什么有两个相同的 PST 日期(记录中的第 6 行)?

用这个解决问题。

 var now = new Date('2019-11-28');
 var nowPlusOneMonth = new Date('2019-12-28');

日期格式似乎有问题。

您错误地创建了结束日期。如果您在构造 nowPlusOneMonth 之后记录 now 日期,您会发现两者是相同的。所以你的范围只有几毫秒。

我会像这样更改 nowPlusOneMonth 的创建:

function SearchEventForFirstCall() {

 var now = new Date();
 var nowPlusOneMonth = new Date(); // Create a date as of now
 nowPlusOneMonth.setMonth(nowPlusOneMonth.getMonth() +1 ); // Sum one month
 Logger.log('NOW: ' + now); // Also I changed the log after the creation of the dates
 Logger.log('DATE PLUS 1 MONTH: ' + nowPlusOneMonth);

 var calendar = CalendarApp.getCalendarById('primary');
 Logger.log('CALENDAR: ' + calendar);
 var events = calendar.getEvents(now,nowPlusOneMonth);
 Logger.log('EVENTS: ' + events.length);

}

如果您仍然需要帮助,我建议您查看 getEvents() 方法的文档。有一个与您要实现的目标相似的示例(提前两个小时而不是一个月)。