Это пример помещения параметров в объект и последующего циклического перемещения по объекту.
function onEdit() {
const sheet = SpreadsheetApp.getActiveSheet(),
editedCell = sheet.getActiveCell(),
editedColumn = editedCell.getColumn(),
sheetName = sheet.getName();
// Define all the sheets to sort using an object literal
const objSheetsToSort = {
"Sheet1": {columnToSortBy: 1, rangeToSort: "A2:AB"},
"Sheet2": {columnToSortBy: 2, rangeToSort: "A2:AB"},
"Sheet3": {columnToSortBy: 3, rangeToSort: "A2:AB"},
"Sheet4": {columnToSortBy: 4, rangeToSort: "A2:AB"}
}
// Get the sort info for this sheet (or `undefined` if we didn't define how to sort the edited sheet).
var config = objSheetsToSort[sheetName];
if (config && editedColumn === config.columnToSortBy) {
// We want to sort the edited sheet.
sortSheet_(sheet, config);
}
}
// This function can only be called by other functions in this Script Project, not manually or via menu.
function sortSheet_(sheetToSort, objectOfSettings) {
Logger.log('sheetToSort: ' + sheetToSort.getName())
var range = sheetToSort.getRange(objectOfSettings.rangeToSort);
range.sort( { column : objectOfSettings.columnToSortBy, ascending: false } );
}