【发布时间】:2020-02-05 16:30:23
【问题描述】:
我有一个谷歌脚本(如下),它正在检查第 N (14) 列何时更新单元格,然后在相邻单元格的 O 列中放置时间戳。但是,我无法设置时间戳的格式。目前时间戳是正确的日期格式,但是,时间格式不是 GMT,但似乎是 GMT-8。我可以通过在工作表中添加另一列来解决这个问题,并有一个我可以用来更改时区的自定义公式,但我真的希望这一切都在下面的 onEdit 函数中完成,以便节省不必要的额外列和脚本。
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //The sheet name that the timestamp will be implemented on
var r = s.getActiveCell();
if( r.getColumn() == 14 ) { //Enter column number i.e A = 1, B = 2 etc...
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //Checks if the column for the timestamp is empty
nextCell.setValue(new Date());
}
}
}
我尝试过以多种方式编辑最后一行,例如:
nextCell.setValue(new Date("GMT","dd/mm/yyyy hh:mm"));
nextCell.setValue(new Date("dd/mm/yyyy hh:mm"));
但这不起作用,因为它将 O 列中的时间戳单元格留为空白。有什么想法吗?
【问题讨论】:
-
您检查过您的脚本/电子表格时区吗?
-
完美,谢谢。电子表格位置设置为英国,但时区为 GMT-8。这样就解决了。
-
如果您从不更改日期格式,方法 formatDate() 可能对您有用:developers.google.com/apps-script/reference/utilities/…
标签: javascript google-apps-script google-sheets timestamp timezone