Skip to content

Instantly share code, notes, and snippets.

@bpmore
Created June 9, 2016 17:41
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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);
}
}
@WilliamTales
Copy link

Works like a charm! thx

@paluka
Copy link

paluka commented Jun 7, 2019

Thank you! Worked well

@MeCompute61
Copy link

Is there a good way to sort sheets which do need to be sorted alphabetically but have dates listed at the end of the tab name by which a GoogleSheet needs to be organized? The location of the date does not change and we could make sure that all the dates are the same length.

image

@tanganbing
Copy link

tanganbing commented Nov 6, 2019

//https://gist.github.com/bpmore/0337b4ac1fc9110975e70fc5df53203f
function sortSheets () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheetMap = {};
var sheets = ss.getSheets();

for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i].getName());
sheetMap[sheets[i].getName()] = sheets[i]
}
sheetNameArray.sort();

for( var j = 0; j < sheets.length; j++ ) {
//ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
ss.setActiveSheet(sheetMap[sheetNameArray[j]]);
ss.moveActiveSheet(j + 1);
}
}
//https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet
//sort by name and color?

@dsysoyev
Copy link

For Alphanumerical sorting can be used following function

...
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
sheetNameArray.sort(collator.compare);
...

@ydlugy
Copy link

ydlugy commented Feb 21, 2020

Is there a way to sort tabs in different ways within one google spreadsheet file? I have a file with lots of tabs for different countries. I want to sort them alphabetically, but within regions. So for example sort all the countries that I have for Africa, then Europe, Asia, etc. Is there a way to do this?
thank you in advance!

@MayraBear
Copy link

anyway to sort them in descending alphabetical order?

Thank you!

@crisgraphics
Copy link

anyway to sort them in descending alphabetical order?

Thank you!

Yes, should work with reverse().

sheetNameArray.sort().reverse();

@ve3mic
Copy link

ve3mic commented Oct 5, 2020

This script doesn't work if your tabs begin with, or just have a date eg. 10/5/2020
Is there a way that this script can work with dates?
Thanks in advance.

@rwillman483
Copy link

Is there anyway to adapt this script to sort the sheets based on the value of a cell? For example I have a workbook with multiple sheets. There is a numerical value in cell P2 of every sheet. I wish to order the sheets in ascending order based on this value. I have been trying to adapt this script but keep banging my head on the wall. Any help would be appreciated!

@scott-englander
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?

@RihardsKazainis
Copy link

Thank you!

@welinafarah
Copy link

perfect thank you!

@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.

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