3

I have the following code that I'm trying to link to a button to sort a range on the page by the column H (Date). I got the button linked but the code is not working

function sortRange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FBY Team');
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A6:M100");

 range.sort({column: 2, ascending: true});

 }

I have added a link to my test spreadsheet. https://docs.google.com/spreadsheets/d/1iWQ40boplJcJmdFg9HNIyOAOrHOjlCRu362LWRdV5y0/edit?usp=sharing

1 Answer 1

2

The problems

  1. 'FBY Team' is not one of your sheet.
  2. you usually use ss variable for spreadsheets here you are trying to grab a sheet.
  3. when line 2 is corrected you can delete line 3.
  4. The header starts at row 7 not 6 so range is incorrect

Solution

what I suggest is you change your code to:

function sortRange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Your Sheet 1');
  var range = sheet.getRange("A7:M100");
  range.sort({column: 2, ascending: true});
 }

this should work now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.