需要在 Google 表中标记时间和日期,但要提前四个小时反映

Need to time & date stamp in Google sheets but reflect four hours earlier

我在 Google 工作表中有一列,当我输入首字母时,脚本时间戳记到下一列,然后通过不同值的现有单元格公式(示例:=B9-Time(4, 4,0) ) 反映了其他几个偏移模拟时间戳。

我现在遇到的问题是,任何后续条目都必须是实时的并加盖戳记,这使我无法使用时区技巧来做到这一点。基本上,我只需要脚本来显示当前时间戳减去四小时(这是我的 "set-up" 自动回填模拟时间的单元格)但对于所有未来的条目正常运行(这是药物管理模拟如果理解目的有助于澄清我的问题)。

这是我的工作脚本(减去从当前减去四个小时的能力):

if( x.getName() == "Vitals" ) { //checks that we're on the correct sheet 
  var y = x.getActiveCell();
    if( y.getColumn() == 1 ) { //checks the column
      var nextCell = y.offset(0,1);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }

这是我用来测试的代码。你可以把时间调整部分拉出来。

function onEdit(e) {
  x=e.source.getSheetName()
  if( x == "Vitals" ) { //checks that we're on the correct sheet 
   var y = e.range.getSheet().getActiveCell();
    if( y.getColumn() == 1 ) { //checks the column
      var nextCell = y.offset(0,1);
      if( nextCell.getValue() === '' ) //is empty?
       var d = new Date();// d is now a date object
       var h=d.getHours()
       var m=d.getMinutes()
       var s=d.getSeconds()
       var nh=h-4 //subtract 4 hrs
       d.setHours(nh,m,s,0); //set the new hours , min Secs and milliSecs as well
       nextCell.setValue(d);
    }
  } 
}

下面是我共享的测试电子表格的 link。复制一份试试看

https://docs.google.com/spreadsheets/d/1sMcd0ivy_LE8kkCto264ETjzMubRniWcdFYzYoh-Cj8/edit?usp=sharing