1

I have been using this script for sorting and so I know it works. Now I'm trying to use it on a new Google Sheet project. Except that the column I want to sort the entire sheet by isn't just one column but a merge, which I derived by merging columns 3 to 6. How do I modify my script to sort by the derived column? Here is my script:

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

  var columnToSortBy = 3; // doing 3:6 doesn't work
  var tableRange = "A11:R82";

  if(editedCell.getColumn() == columnToSortBy){   
    var range = sheet.getRange(tableRange);
    range.sort( { column : columnToSortBy } );
  }
}

After some trials and error, I did the following, but it's still not working:

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

  var columnToSortBy = [3,4,5,6];
  var tableRange = "A11:R82";

  if(columnToSortBy.includes(editedCell.getColumn()) ){   
    var range = sheet.getRange(tableRange);
    range.sort( { column : columnToSortBy[0] }, { column : columnToSortBy[1] },{ column : columnToSortBy[2] },{ column : columnToSortBy[3] } );
  }
}

1 Answer 1

0

Normally it's just:

range.sort([{column: 3}, {column: 4}, {column: 5}, {column: 6}]);

So based on this wouldn't it be column: 3[0] , column 3[1] etc ? I dont know, I'm just kibitzing. Give it a go!

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.