【问题标题】:Google Sheets Automatic Timestamp Script for Multiple Tabs用于多个选项卡的 Google 表格自动时间戳脚本
【发布时间】:2016-03-03 15:13:38
【问题描述】:

我正在制作一个带有多个工作表标签的大型谷歌电子表格。当我编辑第 6 列时,我需要将时间戳自动输入到第 1 列中。我需要在每张纸上独立发生这种情况。这是我到目前为止的脚本,它运行良好。但是,我将至少再添加 10 个选项卡,我想知道是否有更简单的方法可以做到这一点,而且出错的空间更小。

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "A" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 6 ) { //checks the column
      var nextCell = r.offset(0, -5);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setNumberFormat("MM/dd HH:mm:ss")
        nextCell.setValue(new Date());
    }
  }
  if( s.getName() == "B" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 6 ) { //checks the column
      var nextCell = r.offset(0, -5);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setNumberFormat("MM/dd HH:mm:ss")
        nextCell.setValue(new Date());
    }
  }
  if( s.getName() == "C" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 6 ) { //checks the column
      var nextCell = r.offset(0, -5);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setNumberFormat("MM/dd HH:mm:ss")
        nextCell.setValue(new Date());
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    是的,有。只需将所有工作表名称放入一个数组中,然后检查当前编辑的工作表是否在该数组中(使用 indexOf)。如果它不在数组中,它将返回-1。在这种情况下,脚本退出。 看看这样的事情是否有效:

    function onEdit(e) {
    var s = e.source.getActiveSheet(),
        sheets = ["A", "B", "C"],
        stampCell = e.range.offset(0, -5);
    if (sheets.indexOf(s.getName()) == -1 || e.range.columnStart !== 6 || !e.value || stampCell.getValue()) return;
    stampCell.setValue(new Date()).setNumberFormat("MM/dd HH:mm:ss")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多