合并或组合两个 onEdit 触发函数

Merging or Combining two onEdit trigger functions

提问人:sidlo 提问时间:6/27/2020 最后编辑:TheMastersidlo 更新时间:9/10/2022 访问量:627

问:

我有在互联网上收集的 Google 表格脚本,并在这里得到了一些帮助。不,我有 2 个冲突。我通过创建脚本 Trigger 来克服这个问题。它有效,但我认为这不是最好的解决方案。你能帮忙把这两个函数分开吗 if 函数合二为一?onEditonEdit2onEdit

//Dependent Dropdown list
function onEdit(e){ // Function that runs when we edit a value in the table.
  masterSelector(master1,master2,master3,master4);
  var activeCell = e.range; // It returns the coordinate of the cell that we just edited.
  var val = activeCell.getValue(); // Returns the value entered in the column we just edited.
  var r = activeCell.getRow(); // returns the row number of the cell we edit.
  var c = activeCell.getColumn(); // returns the column number of the cell we edit.

  var wsName = activeCell.getSheet().getName();
  if (wsName === masterWsName && c === firstLevelColumn && r > masterNumberOfHeaderRows) { // the if delimits the section sensitive to modification and action of the onEdit.
    applyFirstLevelValidation(val,r);
  } else if (wsName === masterWsName && c === secondLevelColumn && r > masterNumberOfHeaderRows){
      applySecondLevelValidation(val,r);
    }
} // end of onEdit

// addRow by checkboxes 
function onEdit2(e) {
    masterSelector(master1,master2,master3,master4);
  //IF the cell that was edited was in column 4 = D and therefore a checkbox AND if the cell edited was checked (not unchecked):
  if (e.range.columnStart === 4 && e.range.getValue() === true) {
    var sheet = SpreadsheetApp.getActiveSheet(),
        row = sheet.getActiveCell()
        .getRow(),
        //(active row, from column, numRows, numColumns)
        rangeToCopy = sheet.getRange(row, 1, 1, 30);
    sheet.insertRowAfter(row);
    rangeToCopy.copyTo(sheet.getRange(row + 1, 1));
    //Reset checked boxes in column 4
    sheet.getRange(row,4,2,1).setValue(false);
  }
}

如果需要,整个脚本都在这里

javascript google-apps-script google-sheets 触发器

评论


答:

5赞 Wicket 6/27/2020 #1

一个脚本不能包含两个同名的函数。将第一个函数重命名为 (实际上最好分配一个描述性名称),将第二个函数重命名为 ,然后将它们放在一个函数中,并将参数传递给它们:onEditonEdit1onEdit2onEdite

function onEdit(e){
  onEdit1(e);
  onEdit2(e);
}

相关: