如何使用Google脚本正确读取Google表格中的时间戳?

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

How to correctly read a timestamp from a Google Sheet using Google Scripts?

问题

我在读取时间戳时有点困惑。我尝试通过Google脚本从Google表格中读取时间戳。

这是我的测试代码:

  var nowStamp = new Date();

  debugSheet.getRange(1,2).setValue(nowStamp);

  var readbackStamp = debugSheet.getRange(1,2).getValue;
  var readbackStamp1 = debugSheet.getRange(1,2).getValue.toString;
  var readbackStamp2 = debugSheet.getRange(1,2);
  var readbackStamp3 = new Date(debugSheet.getRange(1,2).getValue);
  var readbackStamp4 = new Date(debugSheet.getRange(1,2).getValue.toString);

最终,我需要一个Date对象,其内容与nowStamp相同。听起来非常简单,但我的所有尝试都不起作用。在调试代码时,这是结果:

如何使用Google脚本正确读取Google表格中的时间戳?

我甚至检查了表格 - 在设置值之后,单元格1,2包含了正确的日期/时间格式的时间戳。

在读取值时,我做错了什么?

英文:

I am a bit lost regarding reading a timestamp correctly. I try to read back a timestamp from a google sheet via google scripts.

This is my test code:

  var nowStamp = new Date();

  debugSheet.getRange(1,2).setValue(nowStamp);

  var readbackStamp = debugSheet.getRange(1,2).getValue;
  var readbackStamp1 = debugSheet.getRange(1,2).getValue.toString;
  var readbackStamp2 = debugSheet.getRange(1,2);
  var readbackStamp3 = new Date(debugSheet.getRange(1,2).getValue);
  var readbackStamp4 = new Date(debugSheet.getRange(1,2).getValue.toString);

So in the end, I need to have a Date object, with the same content as nowStamp. It sounds dead simple, but none of my attempts work. When debugging the code, this is the result:

如何使用Google脚本正确读取Google表格中的时间戳?

I even checked the sheet - after setting the value, the cell 1,2 contains a correct timestamp in a date/time format.

What did I do wrong when reading back the value?

答案1

得分: 1

Your variables are referencing/pointing to functions instead of calling them. Try this instead:

var nowStamp = new Date();

debugSheet.getRange(1, 2).setValue(nowStamp);

var readbackStamp = debugSheet.getRange(1, 2).getValue();
var readbackStamp1 = debugSheet.getRange(1, 2).getValue().toString();
var readbackStamp2 = debugSheet.getRange(1, 2);
var readbackStamp3 = new Date(debugSheet.getRange(1, 2).getValue());
var readbackStamp4 = new Date(debugSheet.getRange(1, 2).getValue().toString());
英文:

Your variables are referencing/pointing to functions instead of calling them. Try this instead:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  var nowStamp = new Date();

  debugSheet.getRange(1,2).setValue(nowStamp);

  var readbackStamp = debugSheet.getRange(1,2).getValue();
  var readbackStamp1 = debugSheet.getRange(1,2).getValue().toString();
  var readbackStamp2 = debugSheet.getRange(1,2);
  var readbackStamp3 = new Date(debugSheet.getRange(1,2).getValue());
  var readbackStamp4 = new Date(debugSheet.getRange(1,2).getValue().toString());

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 03:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376614.html
匿名

发表评论

匿名网友

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

确定