我正在开发一个 Google Apps Script 电子表格应用程序,我希望该程序具有的一项功能是根据来自 2 个不同列的数据自动对一系列表单响应进行排序。所以我想按第 16 列中的数据对其进行排序,然后按第 1 列排序。我可以使用以下方法手动实现此功能:
https://drive.googleblog.com/2010/06/tips-tricks-advanced-sorting-rules-in.html
目前,我正在对第一列运行 Spreadsheet.sort(column, ascending)
函数,但我无法对其进行排序,以便它接受第二列作为附加排序规则。 Google Apps Script 中是否有可以用来模拟此功能的方法?
最佳答案
见文档:
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}]);
}
关于sorting - Google Apps 脚本附加排序规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15859024/