gpt4 book ai didi

javascript - Google Apps 脚本 : Calendar . getEvents 在错误的时区导入事件

转载 作者:行者123 更新时间:2023-11-30 00:30:13 26 4
gpt4 key购买 nike

Calendar.getEvents 仅适用于 PDT 时区,无法检索我的 GMT+02.00(欧洲/华沙,CEST)时区的事件。
我是 JS 和 GAS 编程的新手,但它看起来像一些问题,也许我的问题有一些解决方法?
Utilities.formatDate 还将 new 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]

最佳答案

您不能依赖执行记录中显示的值(有时也显示在 Logger 中,至少在过去......现在不确定),不要忘记这段代码是在 Google 的服务器上执行的总部设在加利福尼亚州,并使用太平洋时区作为标准;-)但如果您正确使用 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"));
}
}

enter image description here

日期对象是正确的,但在执行记录中显示不同,您可以忽略这种差异Utilities.formatDate 方法允许正确显示。

enter image description here

关于javascript - Google Apps 脚本 : Calendar . getEvents 在错误的时区导入事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29877374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com