Answer the question
In order to leave comments, you need to log in
Is it possible to make a working script to sort data in a table?
Help fix the script!
There is a table that needs sorting in descending order in column E , that is, I will change the values, and the entire row should take its place according to the serial number that is in column A (it should NOT change), that is, the entire row from B to Z must be sorted.
https: //docs.google.com/spreadsheets/d/1iPl4-OEvhU ...
function onEdit(e) {
const eventCell = e.range;
if (eventCell.getColumn() == 5 && eventCell.getNumColumns() == 1 && eventCell.getNumRows() == 1 && eventCell.getRow() > 2) {
const sheet = eventCell.getSheet();
const lastRow = sheet.getRange('E3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
sheet.getRange(3, 2, lastRow - 2, 13).sort({column: 5, ascending: false});
};//
} // onEdit event
Answer the question
In order to leave comments, you need to log in
1) "The script that I mentioned in the question swears (TypeError: Cannot read property 'range' of undefined)" everything is correct, it should be so when you run the script manually. The variable e that you pass to the function only contains data when the simple onEdit trigger fires, but when manually triggered by this function, it will be undefined.
Add the following stub to the beginning of the script (so you could run the script manually):
e = e || {
range: SpreadsheetApp.getActiveRange()
};
function onEdit(e) {
e = e || {
range: SpreadsheetApp.getActiveRange()
};
const range = e.range;
if (range.getColumn() !== 5)
return;
if (range.getRow() < 3)
return;
const sheet = range.getSheet();
const frozen_rows = 2;
const last_row = sheet
.getRange('E'+(frozen_rows+1))
.getNextDataCell(SpreadsheetApp.Direction.DOWN)
.getRow();
sheet
.getRange(frozen_rows+1, 2, last_row - frozen_rows, 13)
.sort({column: 5, ascending: false});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question