Google Sheets script will not function (Cell reference out of range)

Viewed 15

The following script I have with the purpose of automatically sorting things does not work, and returns with "Exception: Cell reference out of range onEdit Code.gs.6 (which directs you to range.sort, placing you after the .)

Does anyone know a fix to this?

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("STANDINGS");
var range = sheet.getRange("S5:Y8");

function onEdit(e)  {
  range.sort([{column: 5, ascending: true}]);
}
1 Answers

Try it this way:

function onEdit(e) {
  //e.source.toast("Entry")
  const sh = e.range.getSheet();
  if (sh.getName() == "STANDINGS") {
    //e.source.toast("Flag1")
    sh.getRange("S5:Y8").sort([{ column: 23, ascending: true }]);
  }
}
Related