【问题标题】:Add a timestamp to one column when other columns changed当其他列更改时,将时间戳添加到一列
【发布时间】:2017-07-07 00:20:19
【问题描述】:

当同一行中的其他单元格完成或更改时,我正在尝试向列添加时间戳。

如果只更改一列而不是多列,则以下脚本有效。

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 7) { //checks the column
      var nextCell = r.offset(0, 7);
          nextCell.setValue(new Date());
      }
  }
}

我正在尝试获取它,以便如果列 E、F、G 或 H 发生更改,则更新列 M 中的时间戳。谢谢

【问题讨论】:

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


    【解决方案1】:

    也许这会起作用

    function onEdit() {
      var s = SpreadsheetApp.getActiveSheet();
      if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if( r.getColumn() == 5) { //checks the column
          var nextCell = r.offset(0, 8);
              nextCell.setValue(new Date());
          }
           if( r.getColumn() == 6) { //checks the column
          var nextCell = r.offset(0, 7);
              nextCell.setValue(new Date());
          }
           if( r.getColumn() == 7) { //checks the column
          var nextCell = r.offset(0, 6);
              nextCell.setValue(new Date());
          }
           if( r.getColumn() == 8) { //checks the column
          var nextCell = r.offset(0, 5);
              nextCell.setValue(new Date());
          }
      }
    }

    【讨论】:

    • 哦,更改您的工作表名称 - 我将其作为 sheet1
    • 虽然稍微改变它(以及工作表名称)确实有效。谢谢。
    【解决方案2】:

    这样的?

    function onEdit() {
          var s = SpreadsheetApp.getActiveSheet();
          if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
            var r = s.getActiveCell();
            if( r.getColumn() >= 5 && r.getColumn() <= 8) { //checks the column
               s.getRange(r.getRow(),13).setValue(new Date());
              }
          }
        }
    

    【讨论】:

    • 虽然我现在发现即使我在时间戳列中添加/删除时间戳,这也会添加更新的时间戳。
    • 确实是这样,我从 or 来回切换。这是一个小改进,因为在代码中添加或删除列不需要您为每列复制粘贴更多代码
    猜你喜欢
    • 2019-02-03
    • 2017-06-17
    • 2021-07-31
    • 2021-08-12
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多