第一个函数工作正常并且排序但是我不能得到第二个函数,它将行存档同时工作。
function onEdit(event){
var sheet = event.source.getActiveSheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 10;
var tableRange = "J:J3"; // What to sort.
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
function onEdit2(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Orders" && r.getColumn() == 10 && r.getValue() == "5. Archive") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archive");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
}
}
答案 0 :(得分:0)
您在if语句块中定义onEdit2(event)
,但您永远不会执行它。
在块内部定义一个函数是不好的做法。你无法有条件地定义一个函数。 See this link for more information on why this is bad.
如果onEdit2
条件为真,您似乎想执行 if
函数。
如果是这样,您应该全局定义该功能,以便您可以从任何需要的地方执行该功能。
你可能想做这样的事情:
function onEdit(event){
var sheet = event.source.getActiveSheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 10;
var tableRange = "J:J3"; // What to sort.
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
onEdit2(event); // Execute the function, passing it whatever event was passed to onEdit
}
} // End definition of onEdit
function onEdit2(passedEvent) { // Do the actual function definition out here
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = passedEvent.source.getActiveSheet();
var r = passedEvent.source.getActiveRange();
if(s.getName() == "Orders" && r.getColumn() == 10 && r.getValue() == "5. Archive") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archive");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}