Translate

Reorder Worksheets in Google Spreadsheets by Name


How to Reorder Worksheets in Google Spreadsheets by Name Using Apps Script

If you work with complex or large Google Spreadsheets, you’ve probably come across a situation where your worksheets (tabs at the bottom) are out of order. Maybe they were created at different times, or someone else added them in random order. Manually rearranging them can be tedious—especially if you have 20, 50, or even 100+ sheets.

The good news is: you can automatically reorder all worksheets alphabetically by name using Google Apps Script.

In this comprehensive guide, we’ll walk you through the what, why, and how of reordering Google Sheets tabs by name using a script. You’ll get full working code, a breakdown of how it works, and bonus tips to customize it further.


Why Reorder Sheets by Name?

Here are some real-world reasons why people want their sheets sorted alphabetically:

  • Easier navigation when there are dozens of sheets

  • Consistent structure for shared documents

  • Useful in reporting dashboards, student or client lists

  • Automation of repetitive file maintenance

  • Clean organization for exported reports, logs, or imports

When you're dealing with a growing number of tabs, organizing them can drastically improve your productivity and data accessibility.


Can You Do This Manually?

Yes, Google Sheets allows manual reordering by dragging tabs left or right. But this quickly becomes inefficient for large spreadsheets, especially when you're trying to maintain an alphabetical or numeric order.

That’s where Google Apps Script becomes a powerful ally.


Getting Started with Apps Script

To use Apps Script:

  1. Open your Google Spreadsheet.

  2. Click on Extensions > Apps Script.

  3. A new tab opens with the script editor.

  4. Delete any default code and paste in your custom script.

  5. Click the disk icon to save and Run the script.

Now, let’s write the code to reorder your sheets alphabetically.


Script: Reorder Sheets Alphabetically

javascript
function reorderSheetsAlphabetically() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheets = spreadsheet.getSheets(); // Create an array of sheet names and their current sheet objects var sheetData = sheets.map(function(sheet) { return { name: sheet.getName(), sheet: sheet }; }); // Sort the array by sheet name sheetData.sort(function(a, b) { return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); }); // Move each sheet to its new position for (var i = 0; i < sheetData.length; i++) { spreadsheet.setActiveSheet(sheetData[i].sheet); spreadsheet.moveActiveSheet(i + 1); // Position is 1-based } SpreadsheetApp.flush(); }

How the Script Works

Step-by-Step Breakdown:

  1. Get the Active Spreadsheet

    • SpreadsheetApp.getActiveSpreadsheet() fetches the current spreadsheet.

  2. Fetch All Sheets

    • getSheets() returns an array of all worksheet tabs.

  3. Map to Name-Sheet Pairs

    • We create an array of objects: { name: sheetName, sheet: sheetObject }

  4. Sort Alphabetically

    • We sort the array using localeCompare() for proper alphabetical ordering (case-insensitive).

  5. Reorder Sheets

    • Google Sheets allows repositioning using moveActiveSheet(index), which is 1-based (first position is 1).

  6. Flush

    • SpreadsheetApp.flush() forces pending changes to be applied immediately.


Case Sensitivity

The default code uses toLowerCase() to make the sorting case-insensitive. If you want case-sensitive sorting (so uppercase tabs come before lowercase), remove the toLowerCase():

javascript
sheetData.sort(function(a, b) { return a.name.localeCompare(b.name); });

Script to Sort Sheets in Reverse Order (Z–A)

If you prefer descending order (Z to A), modify the sort like this:

javascript
sheetData.sort(function(a, b) { return b.name.toLowerCase().localeCompare(a.name.toLowerCase()); });

Filter Sheets Before Sorting (Optional Customization)

Suppose you want to exclude specific sheets like "Dashboard", "Summary", or "Settings". You can do this by filtering them out:

javascript
var excludedSheets = ["Dashboard", "Summary", "Settings"]; var sheetData = sheets .filter(sheet => !excludedSheets.includes(sheet.getName())) .map(sheet => { return { name: sheet.getName(), sheet: sheet }; });

Then sort and move just those sheets. This keeps important tabs like dashboards fixed at the front.


Schedule the Reordering Automatically (Optional)

You can automate this task to run daily or weekly.

Steps:

  1. In the script editor, click on the clock icon in the toolbar.

  2. Click Add Trigger.

  3. Choose the reorderSheetsAlphabetically function.

  4. Set the event to Time-driven, and select a frequency.

Your sheets will now stay sorted on autopilot.


Common Use Cases

  • Teachers: Reorder student sheets alphabetically.

  • Project Managers: Organize task sheets by team or department name.

  • Business Analysts: Keep monthly or regional data in order.

  • Data Entry Teams: Automatically sort new tabs created via form submissions.


Limitations & Considerations

  • The script requires edit access to the spreadsheet.

  • moveActiveSheet(index) reorders based on a 1-based index, so calculations must account for this.

  • Sheet names must be unique.

  • Sheets shared with other users may briefly re-render during execution.


Final Thoughts

Reordering worksheets alphabetically in Google Sheets can drastically improve usability and organization—especially for spreadsheets with many tabs. While manual reordering works for a few tabs, Google Apps Script provides a much more scalable and automated solution.

With just a few lines of code, you can sort your entire spreadsheet's tabs in seconds—and even automate the task going forward.

So next time your spreadsheet feels chaotic, let the script do the organizing. Clean, structured data starts with clean, structured sheets.