Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Range on Spreadsheet with button linked to app script

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

like image 333
Joshua Doan Avatar asked Dec 04 '25 00:12

Joshua Doan


1 Answers

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.

like image 183
JSmith Avatar answered Dec 05 '25 13:12

JSmith