Skip to content

Instantly share code, notes, and snippets.

@bpmore
Created June 9, 2016 17:41
Show Gist options
  • Save bpmore/0337b4ac1fc9110975e70fc5df53203f to your computer and use it in GitHub Desktop.
Save bpmore/0337b4ac1fc9110975e70fc5df53203f to your computer and use it in GitHub Desktop.
Sort Tabs in Google Spreadsheets
1. Copy/Paste the information below to the clipboard
2. Open the spreadsheet whose sheets need to be alphabetised
3. Choose Tools > Script editor > Blank (this opens a new tab in the browser)
4. Press Control+A followed by Control+V copy and paste the script in
5. Press Control+S to save the script
6. Choose Run > sortSheets
7. Go back to the spreadsheet tab to view the new sorted tab order
--Copy everything below this line--
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);
}
}
@nolafsen
Copy link

nolafsen commented Sep 8, 2021

That worked great - thank you. One follow up question. Is there a way to exclude tabs that you want to remain at the beginning? Or a way to freeze the current position of tabs?

@gr00tie
Copy link

gr00tie commented Oct 26, 2021

Works like a charm. You made my day, thanks!

@shapiroezra
Copy link

It's taking a white, but it's working! Thank you!

@jrgiadachg
Copy link

Excellent, thanks a lot

@CjLamar
Copy link

CjLamar commented Feb 24, 2022

Awesome!

@Pseudandry
Copy link

It's not working for me. I copied the script verbatim, and when I run it within the script editor, I get execution started and execution completed notices, but the order of the sheet tabs doesn't change. How can I find out what's wrong?

Did you ever get an answer for this? Mine is doing the same thing.

@shapiroezra
Copy link

shapiroezra commented Dec 8, 2022 via email

@HoosierFDaddy
Copy link

I also am looking for a unique solution. Your alphabetizer works like a dream for me and i thought i could figure out my own solution but every example of other examples were varied in their style. I understood all of them separately, but when i tried to combine them... the downward spiral.
I currently have about 60 sheets that I'm trying to organize by:

  1. The first five sheets will be in the same specific order every time. (I don't think i had any problem with that...)
  2. After that I would like the Blue tabs, then Orange, then Pink and finally Red, with...
  3. Each color group individually Alphabetized.

I had the first 5 sheets and two color groups working fine but i did something somewhere trying to wrap it up and destroyed al the progress i made. The more i tried to cobble it back together the worse it got until i finally realized i had spent hours just staring at the screen. I'm one of those stubborn people and I love to figure things out, but right now I'm that guy with just enough knowledge to be dangerous. I patiently await my opportunity to be guided to enlightenment.

@EldarAliev
Copy link

Thank you!

anyway to sort them in descending alphabetical order?
Thank you!

Yes, should work with reverse().

sheetNameArray.sort().reverse();

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