Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets script to sort table gives error "Cell reference out of range"

I'm relatively new to scripting in google sheets and am trying to write something that can be assigned to a button to sort a table by column 5 in that table. I am using the code below:

    function sort() {
       var ss = SpreadsheetApp.getActiveSpreadsheet();
       var sheet = ss.getSheetByName("items");
       var range = sheet.getRange("J2:O13");
       range.sort(5);
    }

However, when I assign it to a button and click the button, I get the error message "Cell reference out of range". This may be a stupid question but thanks for any help in advance.

like image 543
John Smith Avatar asked Aug 01 '16 14:08

John Smith


People also ask

Is there a Sortby function in Google Sheets?

The SORTBY Function is not available in Google Sheets, but its SORT Function can be used instead and is more powerful than the SORT Function in Excel. It allows us to use multiple columns to dynamically sort data ranges.


1 Answers

Seems like you are trying to sort on col 5 (E) but that is not in your range. The first column in your range is col 10 (J). If you want to sort on col O you'll need

range.sort(15)
like image 92
JPV Avatar answered Sep 27 '22 23:09

JPV