我正在运行一个简单的脚本来对我的Google表单进行排序
function sort() {
//Variable for column to sort first
var sortFirst = 7; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
var sortFirstAsc = false; //Set to false to sort descending
var headerRows = 1;
var activeSheet = SpreadsheetApp.getActiveSheet();
var sheetName = activeSheet.getSheetName(); //name of sheet to be sorted
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
range.sort([{column: sortFirst, ascending: sortFirstAsc}]);
}
我遇到的问题是,当我手动运行它时,它是完美的,但是当我通过基于时间的触发器运行它时,它什么都不做。检查日志,它显示它执行完美,但工作表上的结果并不反映它已执行。
手动运行:
[18-05-08 16:26:28:854 PDT] Starting execution
[18-05-08 16:26:28:990 PDT] SpreadsheetApp.getActiveSheet() [0.131 seconds]
[18-05-08 16:26:28:990 PDT] Sheet.getSheetName() [0 seconds]
[18-05-08 16:26:28:991 PDT] SpreadsheetApp.getActive() [0 seconds]
[18-05-08 16:26:28:992 PDT] Spreadsheet.getSheetByName([Call]) [0 seconds]
[18-05-08 16:26:28:992 PDT] Sheet.getMaxRows() [0 seconds]
[18-05-08 16:26:29:087 PDT] Sheet.getLastColumn() [0.094 seconds]
[18-05-08 16:26:29:088 PDT] Sheet.getRange([2, 1, 999, 12]) [0 seconds]
[18-05-08 16:26:29:090 PDT] Range.sort([[{column=7.0, ascending=false}]]) [0 seconds]
[18-05-08 16:26:29:214 PDT] Execution succeeded [0.232 seconds total runtime]
运行基于时间的触发器:
[18-05-08 16:27:28:466 PDT] Starting execution
[18-05-08 16:27:28:775 PDT] SpreadsheetApp.getActiveSheet() [0.299 seconds]
[18-05-08 16:27:28:775 PDT] Sheet.getSheetName() [0 seconds]
[18-05-08 16:27:28:777 PDT] SpreadsheetApp.getActive() [0 seconds]
[18-05-08 16:27:28:777 PDT] Spreadsheet.getSheetByName([Call]) [0 seconds]
[18-05-08 16:27:28:778 PDT] Sheet.getMaxRows() [0 seconds]
[18-05-08 16:27:28:900 PDT] Sheet.getLastColumn() [0.121 seconds]
[18-05-08 16:27:28:901 PDT] Sheet.getRange([2, 1, 999, 12]) [0 seconds]
[18-05-08 16:27:28:902 PDT] Range.sort([[{column=7.0, ascending=false}]]) [0.001 seconds]
[18-05-08 16:27:29:038 PDT] Execution succeeded [0.429 seconds total runtime]
非常感谢任何帮助。
答案 0 :(得分:0)
在您的情况下,当脚本由时间触发器运行时,需要提供工作表名称。那么这个修改怎么样?
function sort() {
//Variable for column to sort first
var sortFirst = 7; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
var sortFirstAsc = false; //Set to false to sort descending
var headerRows = 1;
var workbook = SpreadsheetApp.getActiveSpreadsheet(); // Modified
var sheetName = "### sheet name ###"; // Added
var sheet = workbook.getSheetByName(sheetName); // Added
var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
range.sort([{column: sortFirst, ascending: sortFirstAsc}]);
}