Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

javascript - Google App脚本.getEvents问题-如何检索事件(Google App Script .getEvents issue - How to retrieve events)

I'm trying to do some basic stuff with GAS.(我正在尝试使用GAS做一些基本的事情。)

I would like to retrieve the events from a specific calendar but every time the returned value is 0 (which is impossible since my calendar is full of events).(我想从特定的日历中检索事件,但是每次返回的值都是0时(由于我的日历中充满了事件,所以这是不可能的)。) I checked the settings in GAS Editor and my calendar.(我检查了GAS编辑器和日历中的设置。) All timeZones are set on GMT+01.(所有时区都在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('[email protected]'); Logger.log('CALENDAR: ' + calendar); var events = calendar.getEvents(now,nowPlusOneMonth); Logger.log('EVENTS: ' + events.length); } Here's the transcript:(这是成绩单:) 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([[email protected]]) [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] Why is there two identical PST dates (6th line in the transcript)?(为什么会有两个相同的PST日期(成绩单的第六行)?) Using this solve the problem.(用这个解决问题。) var now = new Date('2019-11-28'); var nowPlusOneMonth = new Date('2019-12-28'); Date format seems to be the issue.(日期格式似乎是问题所在。)   ask by Yotho translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are incorrectly creating the end date.(您错误地创建了结束日期。)

If you were to log the now date after you have constructed the nowPlusOneMonth you would see that both are the same.(如果在构造nowPlusOneMonth之后要记录now日期,您将看到两者相同。) So your range is just a few milliseconds.(因此,您的范围只有几毫秒。) I would change the creation of nowPlusOneMonth like so:(我将更改nowPlusOneMonth的创建, 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); } If you still need help I would suggest looking at the documentation of the getEvents() method.(如果您仍然需要帮助,建议您查看getEvents()方法的文档。) There is an example similar at what you are trying to achieve (two hour ahead instead of one month).(您尝试达到的目标有一个类似的示例(提前两个小时而不是一个月)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...