【问题标题】:Edit Google Script "new Date" timestamp format编辑谷歌脚本“新日期”时间戳格式
【发布时间】: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 列中的时间戳单元格留为空白。有什么想法吗?

【问题讨论】:

标签: javascript google-apps-script google-sheets timestamp timezone


【解决方案1】:
function onEdit(e) {
  var sh=e.range.getSheet();
  e.source.toast('Here');
  if(sh.getName()!="Sheet1" )return;
  if( e.range.columnStart==14 && e.range.offset(0,1).isBlank()) {
    e.range.offset(0,1).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "d/MM/yyyy HH:mm"));
  } 
} 

Utilities.formatDate()

onEdit Event Object

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2021-01-14
    • 2021-02-23
    • 2020-11-16
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多