2

I have a data-set which I would like to have sorted with first column 6 and then column 7.

From this post: How can I make some data on a Google Sheets auto-sorting? I have gotten ideas about making a script.

I have made it so far it is sorting by column 6 but NOT column 7.

Any idea what I do wrong?

function myFunction() {

}
function onEdit(event){
  var sheet = event.source.getActiveSheet();
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 6;
  var tableRange = "A2:U";

  if(editedCell.getColumn() == columnToSortBy){   
    var range = sheet.getRange(tableRange);
  range.sort([{column: 6, ascending: true}, {column: 7, ascending: true}]);
  }
}
3
  • Does it really have to be using a script? It could be easier using a formula. Commented Dec 20, 2019 at 22:00
  • @marikamitsos because by doing it with scripts you get the added bonus of the onEdit misfiring because of it's inability to handle more than 2 edits at a time which in turn will create more stackexchange questions!
    – CodeCamper
    Commented Dec 21, 2019 at 23:32
  • @CodeCamper Something new every day. Thanks. Looking forward for your answer Commented Dec 22, 2019 at 9:52

1 Answer 1

0

Sort by columns 6 and 7 when column 6 is edited

function onEdit(e) {
  e.source.toast('Entry');
  const sh = e.range.getSheet();
  if (sh.getName() == "Sheet1" && e.range.columnStart == 6) {
    let range = sh.getRange(2, 1, sh.getLastRow() - 1, 21);
    range.sort([{ column: 6, ascending: true }, { column: 7, ascending: true }]);
  }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.