Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:12:11+00:00 2026-06-12T05:12:11+00:00

I have a spreadsheet that I’d like to sort by multiple columns many times

  • 0

I have a spreadsheet that I’d like to sort by multiple columns many times as I work on the document. This is a multi-step process using the GUI, and seemed far too time-consuming. I have to check the box for a header row, set up the first sorting parameter, and then add another one. Phew! It gets old quick.

Sorting the sheet was no problem, but the header row was also sorted! I don’t find any way to specify that I have a header row as a parameter to the sort() function, nor do I see any way to globally define a header row that will be automatically noticed by sort().

In lieu of that, I saved the header row, deleted the first row, sorted the sheet, inserted a new row at the very top, re-created the header row, and formatted the header row. Wow! It didn’t take that long to write and it works well, but it’s awfully complicated!

From the user perspective, the header row briefly disappears before popping back up. It’s not too bad, but it’s plainly obvious something is going on.

So finally I get around to my question: Have I overlooked the ability to acknowledge the presence of a header row when I’m sorting? Or does sorting a sheet with a header row present in a script also sort the header row, with no recourse?

If my method, or one like it, is required to avoid including the header row in the sort process, is there somewhere that I can notify the Google Apps team so they can consider adding this feature? Since specifying the existence of a header row exists in the GUI with a simple checkbox, I hope it’s present or can be added when sorting within a script.

EDIT

My original code (with documentation/comments removed):

sheet.deleteRow(1);

rows.sort([<1st col>, <2nd col>]);

sheet.insertRowsBefore(1, 1);
for (var j = 0; j <= (numCols - 1); j++) {
  sheet.getRange(1, (j + 1)).setValue(firstRow[j]);
}

sheet.getRange(1, 1, 1, numCols).setFontWeight("bold");
sheet.getRange(1, 1, 2, numCols).setBorder(true, true, true, true, true, true);

What I’ve tried in place of the above code thanks for Thomas’ suggestion:

sheet.setFrozenRows(1);
sheet.sort(<2nd col>);
sheet.sort(<1st col>);
sheet.setFrozenRows(0);

Unfortunately, this just sorts by column, not by row. Replacing the 2 sheet.sort(); calls with the rows.short(); line in the first code block doesn’t work. Trying this results in the initial problem I reported in my comment where the header row is sorted along with the other data, even though the first row is frozen before sorting.

Also, unless another line of code is added below this I get a perpetual “Working…” notification at the top of the spreadsheet. It doesn’t seem to affect anything, however.

In all cases, var sheet = SpreadsheetApp.getActiveSheet();, var rows = sheet.getDataRange();, and firstRow is an array of the header row data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:12:13+00:00Added an answer on June 12, 2026 at 5:12 am

    In all cases, var sheet = SpreadsheetApp.getActiveSheet();, var rows =
    sheet.getDataRange();, and firstRow is an array of the header row
    data.

    Rather than using getDataRange(), you can just get the range you want to sort (that is, exclude the header row):

    var sheet = SpreadsheetApp.getActiveSheet();
    var rows = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
    

    This next part is just an answer/observation to a “secondary” question:

    After the script is finished, the header row was sorted along with all
    the other rows although the first row is frozen.

    I think this is a bit of a limitation in GAS where some of these “spreadsheet tool” methods don’t play nice with one another – this issue is not the same as yours but I think it’s (kind of) related.

    So with the “frozen header” workaround, I’d be interested to see if SpreadsheetApp.flush(); inserted just after sheet.setFrozenRows(1); makes it work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel spreadsheet that has 2 columns. Something like this |ColA |
Like many, I have spreadsheet that draws data from over 40 text files as
I have a google spreadsheet that looks like this, for budget keeping. I want
I have an Excel spreadsheet that looks like: spreadsheet http://img186.imageshack.us/img186/6495/exelf.jpg I'd like to convert
I have an excel spreadsheet that is password-protected. I need to open this spreadsheet
I have an excel spreadsheet that has two columns. When I choose to save
I have an xl spreadsheet that I'd like to scrap. I'll replace it with
Ok, so I have an excel spreadsheet that contains data that I would like
I have a google spreadsheet that uses this function: =SUM(E:E) - SUM(C:C) It adds
i have an excel spreadsheet that is about 300,000 rows and about 100 columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.