Could someone please help code a more concise script?
I only want the script to run on sheets 'Trip 1' to 'Trip 3'.
This is the script I'm using:
SORT_DATA_RANGE = "A4:K";
SORT_ORDER = [
{column: 2, ascending: false}, // 3 = column number, sorting by descending order
{column: 6, ascending: true}, // 1 = column number, sort by ascending order
{column: 7, ascending: true},
{column: 8, ascending: true},
];
function onEdit(event){
var editedRange= event.range;
var editedSheet= editedRange.getSheet()
if(editedRange.getColumn() == 2 && editedSheet.getName() == "Trip 1")
{
var range = editedSheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
}
else if(editedRange.getColumn() == 2 && editedSheet.getName() == "Trip 2")
{
var range = editedSheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
}
else if(editedRange.getColumn() == 2 && editedSheet.getName() == "Trip 3")
{
var range = editedSheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
}
}
CodePudding user response:
You can define a constant - an array with the names of the sheets on which you want to apply sorting SORT_ON_SHEETS = ['Trip 1','Trip 2','Trip 3']
And in the onEdit() function, check if this array includes the name of the sheet where the event occurred
function onEdit(event){
var editedRange= event.range;
var editedSheet= editedRange.getSheet()
if (editedRange.getColumn() == 2 && SORT_ON_SHEETS.includes(editedSheet.getName())){
var range = editedSheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
}
}
CodePudding user response:
Try
function onEdit(event){
var editedRange= event.range;
var editedSheet= editedRange.getSheet()
if(editedRange.getColumn() == 2 && (editedSheet.getName() == "Trip 1" || "Trip 2" || "Trip 3")){
var range = editedSheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
}
}