Google apps script Google Sheets用于多个选项卡的自动时间戳脚本

Google apps script Google Sheets用于多个选项卡的自动时间戳脚本,google-apps-script,scripting,tabs,google-sheets,timestamp,Google Apps Script,Scripting,Tabs,Google Sheets,Timestamp,我正在制作一个带有多个工作表标签的大型谷歌电子表格。编辑第6列时,需要在第1列中自动输入时间戳。我需要在每一张纸上都独立地进行。这是我到目前为止的脚本,它运行良好。不过,我打算再添加至少10个选项卡,我想知道是否有一种更简单的方法可以减少出错的空间 function onEdit() { var s = SpreadsheetApp.getActiveSheet(); if( s.getName() == "A" ) { //checks that we're on the correc

我正在制作一个带有多个工作表标签的大型谷歌电子表格。编辑第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());
    }
  }
}

是的,有。只需将所有工作表名称放在一个数组中,并检查当前编辑的工作表是否在该数组中(使用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")
}