Sorting blanking out

135 views
Skip to first unread message

Shelley Stephen - Voice Talent

unread,
Apr 8, 2022, 11:02:50 AM4/8/22
to Google Apps Script Community
I originally created this sort with macro and it worked fine. But, now it is leaving the entire data portion of the sheet blank.

What I want to do is sort all the non-blank rows by the customer name. Nothing fancy - just a basic sort. 

Clark Lind

unread,
Apr 9, 2022, 7:34:07 AM4/9/22
to Google Apps Script Community
Hi Shelley,
Looks like a simple fix.  On line 4, change  ss.getRange  to  sheet.getRange.

See if that works.

Shelley Stephen - Voice Talent

unread,
Apr 9, 2022, 6:12:58 PM4/9/22
to Google Apps Script Community
Sadly, it does the same thing even with your suggested change. Thank you for the advice!

Clark Lind

unread,
Apr 10, 2022, 9:18:09 AM4/10/22
to Google Apps Script Community
Here you go. Because the range you are sorting includes blank rows, you have to find the actual last row with data and only sort those. The downside of this approach is it stops at the first blank row, so you can't have any blank rows within the data.
function SortbyCustomerName() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Master');

  //get the rows with data (stops at first blank row)
  //returns e.g., "A7:A44".  We want the "44".
      var rows = sheet.getRange("A7").getDataRegion(SpreadsheetApp.Dimension.ROWS).getA1Notation().toString();

  //parse the result to get the last row number
      var lastRow = rows.slice(rows.indexOf(":") +2)

  //add the ending row number to the sort range
      sheet.getRange('A7:az' + lastRow).activate().sort({column: 1, ascending: true});
};
Reply all
Reply to author
Forward
0 new messages