本文介绍了合并或组合两个 onEdit 触发函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上收集了 Google 表格的脚本,并在此处获得了一些帮助.不,我有 2 个 onEdit 冲突.我通过为 onEdit2 创建脚本触发器来克服这个问题.它有效,但我认为这不是最好的解决方案.你能帮忙把这两个分离的 onEdit 和 if 函数合二为一吗?

I have script for Google Sheets that I collected on interwebs and got some help here. No I have 2 onEdit in conflict. I overcome that by creating script Trigger for onEdit2. It works but I don't think it is the best solution. Could you help get those two separated onEdit with if functions into one, please?

//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);
  }
}

整个脚本在此处,如果需要.>

Whole script is here, if needed.

推荐答案

一个脚本不能包含两个同名的函数.将您的第一个 onEdit 函数重命名为 onEdit1(实际上最好指定一个描述性名称),然后添加以下函数:

A script cannot contain two functions with the same name. Rename your first onEdit function to onEdit1 (actually it will be better to assign a descriptive name), then add the following function:

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

相关:

10-15 01:47