-
-
Save BobRupholdt/70f0e248d537d00f61844a3bcf83e4fb to your computer and use it in GitHub Desktop.
Sort Tabs in Google Spreadsheets
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
/* | |
Sort sheets in a Google spreadsheet. | |
Allows specifying first/last sheets as arrays of sheet names | |
Requires Apps Script runtime powered by Chrome V8 (not Rhino) | |
*/ | |
function sortSheets(){sortSheets_(first=[config.indexSheetName], last=["log"]);} | |
function sortSheets_ (first=[],last=[]) { | |
if(!Array.isArray(first)) throw "Parameter 'first' must be an array"; | |
if(!Array.isArray(last)) throw "Parameter 'last' must be an array"; | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheetNameArray = []; | |
var sheets = ss.getSheets(); | |
for (var i=0; i<sheets.length; i++) { | |
if( first.indexOf(sheets[i].getName())>-1 || last.indexOf(sheets[i].getName())>-1 ) continue; | |
sheetNameArray.push(sheets[i].getName()); | |
} | |
sheetNameArray.sort(); | |
sheetNameArray = [...first, ...sheetNameArray, ...last]; | |
for( var j=0; j<sheets.length; j++ ) { | |
var sheet = ss.getSheetByName(sheetNameArray[j]); | |
if(!sheet) continue; //in case the input sheets don't exist | |
ss.setActiveSheet(sheet); | |
ss.moveActiveSheet(j + 1); | |
} | |
ss.setActiveSheet(ss.getSheets()[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment