【问题标题】:Google App Script .getEvents issue - How to retrieve eventsGoogle App Script .getEvents 问题 - 如何检索事件
【发布时间】:2019-11-28 19:51:25
【问题描述】:

我正在尝试用 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');

日期格式似乎是个问题。

【问题讨论】:

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


    【解决方案1】:

    您错误地创建了结束日期。如果您在构建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() 方法的文档。有一个与您想要实现的目标类似的示例(提前两小时而不是一个月)。

    【讨论】:

    • 非常感谢!我还找到了month = now.getMonth()+1; 的解决方法。我做了日期和年份,最后得到了nowPlusOneMonth = new Date(year,month,date);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    相关资源
    最近更新 更多