Reputation: 13
I have been looking for a way to script an auto-sort on a Google Sheet that does the following:
I tried the following code, but it will not work properly:
function onEdit(event) {
var postDate = 1;
var postTime = 2;
var sheet = event.source.getActiveSheet();
var tableRange = "A35:I911";
var range = sheet.getRange(tableRange);
range.sort([
{column: postDate, ascending: true},
{column: postTime, ascending: true}
]);
}
Thank you in advance!
Here is a link to a redacted version of the Google Sheet: https://docs.google.com/spreadsheets/d/1NUy6aY-lEA66UFXg4Yx4mXsTgFvj5g2Dhv4jToYsQnw/edit?usp=sharing)
Upvotes: 1
Views: 317
Reputation: 201398
but it will not work properly
, when I saw your sample Spreadsheet, it seems that the max row is 910 at the sheet "March 2021". I thought that this might be the reason of your issue of it.When above points are reflected to your script, it becomes as follows.
function onEdit(event) {
var range = event.range;
var sheet = event.source.getActiveSheet();
if (sheet.getSheetName() == "Overview" || range.rowStart < 35 || range.rowStart >= 911 || range.columnStart > 2) return;
var postDate = 1;
var postTime = 2;
var tableRange = sheet.getMaxRows() < 911 ? "A35:I" + sheet.getLastRow() : "A35:I911";
var range = sheet.getRange(tableRange);
range.sort([
{column: postDate, ascending: true},
{column: postTime, ascending: true}
]);
}
range.columnStart > 9
.Upvotes: 1