2

I have a google sheet that I would like to sort different ranges by the 2nd column in each range. Example:

A B C D E F G  
p 7 1 h 9 1   
q 6 2 j 3 2  
g 9 3 p 7 3

I need the range A:C sorted by B and the range E:G sorted by F. When I write the script, only the first set of data sorts. Here is the script I have so far based on my current spreadsheet:

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Sheet1")
  var range = sheet.getRange("A3:C450");

 // Sorts by the values in column 2 (B)
 range.sort({column: 2, ascending: true});

function onEdit(e) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Sheet1")
   var range = sheet.getRange("D3:F450");

 // Sorts by the values in column 5 (E)
 range.sort({column: 5, ascending: true});
  }
}

How can I get it to run both of thse?

2 Answers 2

0

You can do it natively with QUERY and ARRAY{}

In you example, try this :

={QUERY(A1:C3,"SELECT * order by B asc"),D1:D3,QUERY(E1:G3,"SELECT * order by F asc")}

Replace asc with desc according to your needs.

0

try:

function onEdit(e) {
  oE1(e);
  oE2();
 }
function oE1(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Sheet1") var range = sheet.getRange("A3:C450"); // Sorts by the values in column 2 (B) range.sort({column: 2, ascending: true}); }
function oE2() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Sheet1") var range = sheet.getRange("D3:F450"); // Sorts by the values in column 5 (E) range.sort({column: 5, 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.