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}]);
  • Does it really have to be using a script? It could be easier using a formula. – marikamitsos Dec 20 '19 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 Dec 21 '19 at 23:32
  • @CodeCamper Something new every day. Thanks. Looking forward for your answer – marikamitsos Dec 22 '19 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.