Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script Additional Sorting Rules

I am working on a Google Apps Script spreadsheet application, and one of the abilities I would like the program to have is to automatically sort a series of form responses based on data from 2 different columns. So I would want to sort it by the data in column 16 and then sort by column 1. I can achieve this functionality manually using the method at: https://drive.googleblog.com/2010/06/tips-tricks-advanced-sorting-rules-in.html

Currently I am running the Spreadsheet.sort(column, ascending) function with the first column, but I cannot make it sort so that it will accept the second column as an additional sorting rule. Is there a method in Google Apps Script that I could use to emulate this functionality?

like image 565
Spino Prime Avatar asked Dec 16 '22 12:12

Spino Prime


1 Answers

See doc: https://developers.google.com/apps-script/reference/spreadsheet/range#sort(Object)

function sortFormResponses() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // change name of sheet to your sheet name
  var s = ss.getSheetByName("Form Responses");
  var lastCol = s.getLastColumn();
  var lastRow = s.getLastRow();

  // assumes headers in row 1
  var r = s.getRange(2, 1, lastRow - 1, lastCol);

  // Note the use of an array
  r.sort([{ column: 1, ascending: true }, { column: 16, ascending: true}]);

}
like image 174
ScampMichael Avatar answered Dec 28 '22 06:12

ScampMichael