本文介绍了将两个onEdit与if函数合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有在网上收集的Google表格脚本,并在此处获得了一些帮助。不,我有2个 onEdit 有冲突。通过为 onEdit2 创建脚本触发器来克服这一问题。它有效,但我认为这不是最佳解决方案。您能否帮助将这两个分开的 onEdit 与if函数合而为一? / / Dependent下拉列表 function onEdit(e){//当我们在表中编辑值时运行的函数。 masterSelector(master1,master2,master3,master4); var activeCell = e.range; //返回刚刚编辑的单元格的坐标。 var val = activeCell.getValue(); //返回在我们刚刚编辑的列中输入的值。 var r = activeCell.getRow(); //返回我们编辑的单元格的行号。 var c = activeCell.getColumn(); //返回我们编辑的单元格的列号。 var wsName = activeCell.getSheet()。getName(); if(wsName === masterWsName&& c === firstLevelColumn& r> masterNumberOfHeaderRows){// if分隔对onEdit的修改和操作敏感的部分。 applyFirstLevelValidation(val,r); }否则,如果(wsName === masterWsName&& c === secondLevelColumn& r> masterNumberOfHeaderRows){ applySecondLevelValidation(val,r); } } // onEdit的结尾 // addRow通过复选框函数onEdit2(e){ masterSelector(master1,master2,master3, master4); //如果已编辑的单元格在第4列= D中,因此是一个复选框,并且选中了(未选中)已编辑的单元格: if(e.range.columnStart === 4& ;&e.range.getValue()=== true){ var sheet = SpreadsheetApp.getActiveSheet(), row = sheet.getActiveCell() .getRow(), //(活动行,来自column,numRows,numColumns) rangeToCopy = sheet.getRange(row,1,1,30); sheet.insertRowAfter(row); rangeToCopy.copyTo(sheet.getRange(row + 1,1)); //重置第4列中的复选框 sheet.getRange(row,4,2,1).setValue(false); } } 整个脚本为如果需要。 > 将第一个 onEdit 函数重命名为 onEdit1 (实际上,分配一个描述性名称会更好) ,然后添加以下函数: function onEdit(e){ onEdit1(e); onEdit2(e); } 相关 如何在同一个Google脚本(Google工作表)中运行多个onEdit函数? 用括号括住多个onEdit函数 多种OnEdit功能的最佳做法 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 listfunction 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 checkboxesfunction 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. 解决方案 Rename your first the 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);}RelatedHow to run multiple onEdit functions in the same google script (google sheets)?Bracketing multiple onEdit functionsBest Practices for Multiple OnEdit Functions 这篇关于将两个onEdit与if函数合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 05:51