英文:
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相同。听起来非常简单,但我的所有尝试都不起作用。在调试代码时,这是结果:
我甚至检查了表格 - 在设置值之后,单元格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:
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论