从Google应用脚本切换时区到Google表格是有意的。

huangapple go评论55阅读模式
英文:

datetime switching timezone from google app script to google sheets intentionally

问题

我试图在Google表格单元格中填入当前日期和时间,日期没有问题,但时间偏了6小时。
这是我使用的代码:

function start() {
  var ss = SpreadsheetApp.getActive();
  var timestamp = new Date();
  Logger.log('start, timestamp: ' + timestamp);
  var date = Utilities.formatDate(timestamp, 'Europe/Brussels', "dd/MM/yyyy");
  Logger.log('date: ' + date);
  var time = Utilities.formatDate(timestamp, 'Europe/Brussels', "HH:mm:ss");
  Logger.log('time: ' + time);
  var sheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
  sheet.appendRow([date,time]);
}

日期和时间的日志确实给我预期的日期和时间,但一旦appendRow完成后,在Google表格中,我得到了偏移的时间...
我尝试过自己调整时间,以纠正到正确的值,但没有改变任何事情。

英文:

I'm trying to fill some google sheets cells with the current date and time, date is ok, but the time is off by 6 hours.
This is the code I used:

      function start() {
      var ss = SpreadsheetApp.getActive();
      var timestamp = new Date();
      Logger.log('start, timestamp: ' + timestamp);
      var date = Utilities.formatDate(timestamp, 'Europe/Brussels', "dd/MM/yyyy");
      Logger.log('date: ' + date);
      var time = Utilities.formatDate(timestamp, 'Europe/Brussels', "HH:mm:ss");
      Logger.log('time: ' + time);
      var sheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
      sheet.appendRow([date,time]);
    }

The logs of the date and time do give me the expected date and time, but once the appendRow is done, in Google Sheets, I'm getting the offset time...
I have tried to offset it myself, to correct to the right value, but that didn't change anything.

答案1

得分: 2

这有点奇怪,我测试了你的代码,对我来说是有效的。

从Google应用脚本切换时区到Google表格是有意的。

从Google应用脚本切换时区到Google表格是有意的。

它正确显示了'Europe/Brussels'的时区。还使用var time = Utilities.formatDate(timestamp, 'GMT+1', "HH:mm:ss"); 进行测试,两者都在执行日志和单元格中显示了正确的时间。

从Google应用脚本切换时区到Google表格是有意的。

从Google应用脚本切换时区到Google表格是有意的。

如果你在新的Apps Script中测试代码,它也显示了错误的时区吗?你是否在项目设置 > 常规设置中检查了时区?

从Google应用脚本切换时区到Google表格是有意的。

如果经过所有这些步骤后仍然存在相同的问题,我建议按照此链接中的步骤创建一个问题跟踪器:https://developers.google.com/apps-script/support#developer_product_feedback

英文:

That is odd, I tested your code and it works for me.

从Google应用脚本切换时区到Google表格是有意的。

从Google应用脚本切换时区到Google表格是有意的。

It's properly showing the timezone of 'Europe/Brussels'. Also, tested using var time = Utilities.formatDate(timestamp, 'GMT+1', "HH:mm:ss"); and both display the correct time inside the Execution log and the cell.

从Google应用脚本切换时区到Google表格是有意的。

从Google应用脚本切换时区到Google表格是有意的。

If you test the code in a new Apps Script does it show the incorrect time zone too? Have you reviewed the time timezone inside Project Settings > General Settings?

从Google应用脚本切换时区到Google表格是有意的。

If after all that you still have the same issue, I will recommend creating an issue tracker by following the steps here.

huangapple
  • 本文由 发表于 2023年2月8日 20:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定