Calendar.getEvents仅适用于PDT时区,无法检索GMT + 02.00(欧洲/华沙,CEST)区域内的事件。
我是编程JS和GAS的新手,但它看起来像是一些问题,也许我的问题有一些解决方法?
Utilities.formatDate还将新的Date()视为PDT时区。
我的所有应用都适用于同一时区。
原谅我的语言,不是我的母语。
代码示例:
function date () {
var dateS = new Date(2015,3,20);
var dateE = new Date();
Logger.log("Start :", dateS);
Logger.log("Stop :", dateE);
var timeZone = Session.getScriptTimeZone();
var spr = CalendarApp.setTimeZone(timeZone);
var rps = SpreadsheetApp.getActive()
.setSpreadsheetTimeZone(timeZone);
var cal = CalendarApp.getCalendarsByName("Norske hellig- og merkedager");
var week = Utilities.formatDate(dateS, timeZone, "ww");
Logger.log("Week nr:", week);
var events = cal[0].getEvents(dateS, dateE);
for (i=1; i < events.length; i++)
{
Logger.log(events[i]);
}
}
返回:
[15-04-26 13:49:48:630 CEST] Logger.log([Start :, [Mon Apr 20 00:00:00 GMT+02:00 2015]]) [0 s]
[15-04-26 13:49:48:630 CEST] Logger.log([Stop :, [Sun Apr 26 13:49:48 GMT+02:00 2015]]) [0 s]
[15-04-26 13:49:48:630 CEST] Session.getScriptTimeZone() [0 s]
[15-04-26 13:49:49:166 CEST] CalendarApp.setTimeZone([Europe/Warsaw]) [0,535 s]
[15-04-26 13:49:49:166 CEST] SpreadsheetApp.getActive() [0 s]
[15-04-26 13:49:49:189 CEST] Spreadsheet.setSpreadsheetTimeZone([Europe/Warsaw]) [0,022 s]
[15-04-26 13:49:49:415 CEST] CalendarApp.getCalendarsByName([Norske hellig- og merkedager]) [0,225 s]
[15-04-26 13:49:49:416 CEST] Utilities.formatDate([Sun Apr 19 15:00:00 PDT 2015, Europe/Warsaw, ww]) [0 s]
[15-04-26 13:49:49:416 CEST] Logger.log([Week nr:, [17]]) [0 s]
[15-04-26 13:49:49:647 CEST] Calendar.getEvents([Sun Apr 19 15:00:00 PDT 2015, Sun Apr 26 04:49:48 PDT 2015]) [0,23 s]
答案 0 :(得分:0)
您不能依赖执行记录中显示的值(有时也会记录在Logger中,至少在过去......现在还不确定),不要忘记此代码是在谷歌的服务器位于加利福尼亚州,使用太平洋时区作为标准;-)
但如果您正确使用Utilities.formatDate
,您将获得正确的值。
尝试从您的代码改编后,查看Logger结果:
function date () {
var dateS = new Date(2015,3,20);
var dateE = new Date();
Logger.log("Start :", dateS);
Logger.log("Stop :", dateE);
var timeZone = Session.getScriptTimeZone();
Logger.log(timeZone);
var spr = CalendarApp.setTimeZone(timeZone);
var rps = SpreadsheetApp.getActive()
.setSpreadsheetTimeZone(timeZone);
var cal = CalendarApp.getCalendarsByName("test_agenda")[0];// I changed to a calendar I own for my test
cal.createEventFromDescription('test event, today 9AM');// create a dummy event for test purpose
var week = Utilities.formatDate(dateS, timeZone, "ww");
Logger.log("Week nr:", week);
var events = cal.getEvents(dateS, dateE);
for (i=1; i < events.length; i++)
{
Logger.log(Utilities.formatDate(events[i].getStartTime(),timeZone,"yyyy/MM/dd @ HH:mm"));
}
}
日期对象是正确的,但它在执行记录中的显示方式不同,您可以忽略此差异。
Utilities.formatDate
方法允许正确显示。