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?
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}]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With