Search code examples
sortinggoogle-sheets

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.


Solution

  • 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)