Last active
October 23, 2023 01:23
-
-
Save chipoglesby/26fa70a35f0b420ffc23 to your computer and use it in GitHub Desktop.
Automatically move the sheets in a spreadsheet into alphabetical order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
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.