Rocco Barletta
Rocco Barletta

Reputation: 13

Excluding Rows when Auto Sorting

I have a google sheet that contains 8 sheets that needs data to be sorted upon entry. I have been able to modify a script to auto sort each individual sheet, but I need to exclude Rows 1 and 2 from the sort. I am fairly new to using scripts so any help is appreciated. Basically, as data is entered onto a Master Sheet it is sent to the Events Pages (Event 1-Event 8). From the pages I would like the data to be sorted by Column A, but exclude Rows 1 and 2.

function AutoSortOnEdit() {
var sheetNames = ["Event 1", "Event 2", "Event 3", "Event 4", "Event 5", 
"Event 6", "Event 7", "Event 8"];

var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 
sheet.getLastColumn());
range.sort({column: 1, ascending: true});
});

I would like each sheet to auto sort Column 1, but exclude the first two rows. I have been able to get most of the script to work, but cannot figure out how to exclude Rows 1 and 2.

My result with this script is that each sheet will sort, but it includes the first two rows

Upvotes: 1

Views: 1922

Answers (1)

Tanaike
Tanaike

Reputation: 201553

How about this modification?

  • When you want to sort rows excluded row 1 and row 2 of each column by Column (A), the range is getRange(3, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).
    • In the case of getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()), only row 1 is excluded.

So can you try the following modification?

From :

var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());

To :

var range = sheet.getRange(3, 1, sheet.getLastRow() - 1, sheet.getLastColumn());

Reference :

If I misunderstand your question, I'm sorry.

Upvotes: 2

Related Questions