我有以下脚本来记录编辑字段的人员和日期时间:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetName();
var r = s.getActiveCell();
var live = true;
var row = r.getRow();
var user = Session.getActiveUser();
var time = new Date();
SpreadsheetApp.getActiveSheet().getRange('S' + row.toString()).setValue(time);
SpreadsheetApp.getActiveSheet().getRange('T' + row.toString()).setValue(user);
};
更新显示在我的工作表中需要30秒。查看执行日志,我发现这需要40秒才能完成,只需要获得S6和T6的值即可。
如何加快速度?
[17-03-18 12:23:28:831 EDT] SpreadsheetApp.getActiveRange() [0 seconds]
[17-03-18 12:23:28:831 EDT] Range.getRow() [0 seconds]
[17-03-18 12:23:28:831 EDT] Range.getLastRow() [0 seconds]
[17-03-18 12:23:28:831 EDT] Range.getColumn() [0 seconds]
[17-03-18 12:23:28:831 EDT] Range.getLastColumn() [0 seconds]
[17-03-18 12:23:28:831 EDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[17-03-18 12:23:28:833 EDT] Session.getActiveUser() [0.001 seconds]
[17-03-18 12:23:28:833 EDT] User.getEmail() [0 seconds]
[17-03-18 12:23:28:835 EDT] Starting execution
[17-03-18 12:23:28:841 EDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[17-03-18 12:23:28:843 EDT] SpreadsheetApp.getActiveSheet() [0 seconds]
[17-03-18 12:23:28:843 EDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[17-03-18 12:23:28:887 EDT] Spreadsheet.getSheetName() [0.044 seconds]
[17-03-18 12:23:28:888 EDT] Sheet.getActiveCell() [0 seconds]
[17-03-18 12:23:28:889 EDT] Range.getRow() [0 seconds]
[17-03-18 12:23:28:889 EDT] Range.getColumn() [0 seconds]
[17-03-18 12:23:28:889 EDT] Range.getColumn() [0 seconds]
[17-03-18 12:23:28:889 EDT] Range.getRow() [0 seconds]
[17-03-18 12:23:28:889 EDT] Session.getActiveUser() [0 seconds]
[17-03-18 12:23:28:890 EDT] SpreadsheetApp.getActiveSheet() [0 seconds]
[17-03-18 12:23:48:345 EDT] Range.setValue([Sat Mar 18 12:23:28 GMT-04:00 2017]) [0 seconds]
[17-03-18 12:23:48:346 EDT] SpreadsheetApp.getActiveSheet() [0 seconds]
[17-03-18 12:24:08:847 EDT] Execution failed: Exceeded maximum execution time [40.002 seconds total runtime]
答案 0 :(得分:0)
变化:
var user = Session.getActiveUser();
为:
var user = Session.getActiveUser().getEmail();
或者更快的方式。使用onEdit返回的事件数据:
function onEdit(e) {
var row = e.range.getSheet().getActiveCell().getRow()
var user=e.user
e.range.getSheet().getRange('S' + row.toString()).setValue(Utilities.formatDate(new Date(), "GMT-6:00", "MM/dd/yyyy HH:mm:ss"));
e.range.getSheet().getRange('T' + row.toString()).setValue(user);
};