我有3个onEdit函数,我在onEdit(e)的主函数下将它们命名为onEdit1(),onEdit2(),onEdit3(e),但它不起作用,它正在使用它下面的2个函数但是当我添加时主函数的第3个最后一个}保持红色,并且该函数给出了以下范围的错误:TypeError:无法从undefined读取属性“range”。 (第93行,文件“代码”)在这一行:if(e.range.getSheet()。getName()!='Sheet1')return;
问题:如何使这3个onEdit函数在一个onEdit(e)函数下一起工作?
第一个代码:它会创建两个相同的工作表,在这种情况下记录sheetToWatch列表中单元格的每个更改(Sheet1)
function onEdit1() {
// This script records changes to the spreadsheet on a "Changelog" sheet.
// The changelog includes these columns:
// "Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"
var sheetsToWatch = ['Sheet1', 'add more'];
var changelogSheetName = "Changelog";
var HelperSheetName = "Helper";
var timestamp = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
var sheetName = sheet.getName();
// if it is the changelog sheet that is being edited, do not record the change
if (sheetName == changelogSheetName) return;
if (sheetName == HelperSheetName) return;
// if the sheet name does not appear in sheetsToWatch, do not record the change
var matchFound = false;
for (var i = 0; i < sheetsToWatch.length; i++) {
if (sheetName.match(sheetsToWatch[i])) matchFound = true;
}
if (!matchFound) return;
var columnLabel = sheet.getRange( 3, cell.getColumn()).getValue();// row 3
var rowLabel = sheet.getRange(cell.getRow(), 2).getValue();// column B
var changelogSheet = ss.getSheetByName(changelogSheetName);
var HelperSheet = ss.getSheetByName(HelperSheetName);
if (!changelogSheet) {
// no changelog sheet found, create it as the last sheet in the spreadsheet
changelogSheet = ss.insertSheet(changelogSheetName, ss.getNumSheets());
// Utilities.sleep(2000); // give time for the new sheet to render before going back
// ss.setActiveSheet(sheet);
changelogSheet.appendRow(["Email","Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"]);
changelogSheet.setFrozenRows(1);
}
if (!HelperSheet) {
// no Helper sheet found, create it as the last sheet in the spreadsheet
HelperSheet = ss.insertSheet(HelperSheetName, ss.getNumSheets());
// Utilities.sleep(2000); // give time for the new sheet to render before going back
// ss.setActiveSheet(sheet);
HelperSheet.appendRow(["Email","Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"]);
HelperSheet.setFrozenRows(1);
}
changelogSheet.appendRow([Session.getActiveUser().getEmail(), timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue()]);
HelperSheet.appendRow([Session.getActiveUser().getEmail(), timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue()]);
}
第二代码:此代码通过将更改日志表中的单元格与帮助工作表进行比较来保护更改日志表不被编辑,如果它是空的,则允许写入数据(如果是不是空它丢弃新数据并从帮助表中恢复旧数据,但是这个代码不保护,如果其中一个用户选择拖动多个单元格,例如10个单元格并点击键盘上的删除,这就是第3个代码。
function onEdit2() {
var masterSheetName = "Changelog" // sheet where the cells are protected from updates
var helperSheetName = "Helper" // sheet where the values are copied for later checking
var firstDataRow = 1; // only take into account edits on or below this row
var firstDataColumn = 1; // only take into account edits on or to the right of this column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if (sheet.getName() != masterSheetName) return;
var cell = sheet.getActiveCell();
if (cell.getRow() < firstDataRow || cell.getColumn() < firstDataColumn) return;
var helperSheet = ss.getSheetByName(helperSheetName);
var helperCell = helperSheet.getRange(cell.getA1Notation());
var newValue = cell.getValue();
var oldValue = helperCell.getValue();
if (oldValue == "") {
helperCell.setValue(newValue);
} else {
masterCell.setValue(oldValue);
}
}
第三代码:此代码检查更改日志表中选择的数据范围,如果其中一个用户一次删除多个单元格,则从帮助表单中恢复它们
function onEdit3(e) {
// If the edit is made on an invalid sheet, exit.
if(e.range.getSheet().getName() != 'Changelog') return;
// Count the number of cells being edited
var cellsNumber = [].concat.apply([],e.range.getValues()).length;
if(cellsNumber == 1 ){
var helper = e.source.getSheetByName('Helper').getRange(e.range.getA1Notation());
if(e.range.getValue() != ''){
helper.setValue(e.value);
} else {
e.range.setValue(helper.getValue());
}
} else {
var helper = e.source.getSheetByName('Helper').getRange(e.range.getA1Notation());
// Count the number of characters on the edited range
var charsNumber = [].concat.apply([],e.range.getValues()).join('').length;
if(charsNumber > 0) {
helper.setValues(e.range.getValues());
} else {
e.range.setValues(helper.getValues());
}
}
}