Skip to content

Instantly share code, notes, and snippets.

@chipoglesby
Last active October 23, 2023 01:23
Show Gist options
  • Save chipoglesby/26fa70a35f0b420ffc23 to your computer and use it in GitHub Desktop.
Save chipoglesby/26fa70a35f0b420ffc23 to your computer and use it in GitHub Desktop.
Automatically move the sheets in a spreadsheet into alphabetical order.
function sortSheets(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i].getName());
}
sheetNameArray.sort();
for( var j = 0; j < sheets.length; j++ ) {
ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
ss.moveActiveSheet(j + 1);
}
}
@rwillman483
Copy link

Can you tell me how to move the sheets in my spreadsheet based on the value of a cell? For example if I have cell P2 on each sheet with a number in it and I want to sort the sheets in ascending order based on cell P2 how would I do it? I've been toying with your script trying to figure it out but I am stuck. I would appreciate any assistance you can provide.

@chipoglesby
Copy link
Author

chipoglesby commented Jan 4, 2021

Can you tell me how to move the sheets in my spreadsheet based on the value of a cell? For example if I have cell P2 on each sheet with a number in it and I want to sort the sheets in ascending order based on cell P2 how would I do it? I've been toying with your script trying to figure it out but I am stuck. I would appreciate any assistance you can provide.

@rwillman483 That should be possible. You would need get the value of each cell in each sheet, push that to an array and then sort it. Let me see if I can work up an example for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment