C_Naga
C_Naga

Reputation: 1

How to sort a specific column in multiple Google sheets

I'd like to sort the forth column of more than one sheet in ascending order. I can do this in the active sheet (sheet1), but can not figure out how to do it in the non-active sheets.

Below is my code.


function sort() {   

   var bus = SpreadsheetApp.openByUrl("URI");    
   var sheet = bus.getSheets()[0];   

   sheet.sort(4,true);    
}

Upvotes: 0

Views: 453

Answers (1)

Kriggs
Kriggs

Reputation: 3778

Instead of getting just the first sheet, get all of them in a variable (notice the absence of the zero index - [0]):

var sheets = bus.getSheets();   

Then iterate through all of them in a FOR loop:

for( sheet in sheets)
  sheets[ sheet ].sort(4,true);

That will sort every sheet in the active Spreadsheet, you can also open sheets by ID, URL, name and the position in Spreadsheet, to find the methods you want just read the documentation.

Upvotes: 1

Related Questions