Google Sheet Script正在复制值两次

时间:2017-03-21 22:15:13

标签: javascript google-sheets

我编写了以下代码,以便在编辑“BOS”工作表中的特定列时运行。时间戳工作得很好,但代码的第二部分运行了两次,我不确定为什么会这样。任何帮助将不胜感激!

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();

  // Checks to make sure the right sheet and cell were edited
  if( s.getName() == "BOS") {
    var ac = s.getActiveCell();
    if( ac.getColumn() == 12) {

      // Adds a timestamp when a new url is posted
      var nextCell = ac.offset(0, 1);
      var date = Date();
      nextCell.setValue(date);

      // Copies the row to the History sheet
      var row = ac.getRow();
      var rangeToCopy = s.getRange(row, 1, 1, 13);
      var destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("History");
      var destAvals = destination.getRange("A1:A").getValues();
      var destAlast = destAvals.filter(String).length;;
      rangeToCopy.copyTo(destination.getRange((destAlast + 1), 1), {contentsOnly: true});    
    };
  };
}

1 个答案:

答案 0 :(得分:0)

@SandyGood是正确的,有一个安装的onEdit触发器也在运行,所以它触发了两次。谢谢!