由于不必要的电话,电子表格上的简单Google脚本查询时间过长

时间:2014-10-17 04:36:21

标签: google-apps-script google-sheets google-spreadsheet-api

我需要从Google电子表格的活动单元格中获取数据。到目前为止请求数据的脚本非常简单:

function getSelectedRow(){

  Logger.log("Looking up active row...")
  var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
  var data = SpreadsheetApp.getActiveSheet().getSheetValues(cell.getRow(), 1, 1, 20);
  Logger.log(data)
}

但是,运行脚本需要很长时间。虽然执行记录显示:

[14-10-17 11:25:56:480 ICT] Execution succeeded [0.668 seconds total runtime]

矛盾的是,它显示了36秒之前的开始时间:

[14-10-17 11:25:19:145 ICT] Starting execution

实际上,第一个日志发生在与声明的执行时间匹配的时间:

[14-10-17 11:25:55:811 ICT] Logger.log([Looking up active row..., []]) [0 seconds]

然而,在开始时间和第一个日志之间,会发生以下情况:

[14-10-17 11:25:19:175 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:19:326 ICT] Sheet.getDataRange() [0.15 seconds]
[14-10-17 11:25:22:210 ICT] Range.getValues() [2.882 seconds]
[14-10-17 11:25:22:241 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:22:371 ICT] Sheet.getDataRange() [0.13 seconds]
[14-10-17 11:25:25:407 ICT] Range.getValues() [3.034 seconds]
[14-10-17 11:25:25:442 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:25:612 ICT] Sheet.getDataRange() [0.169 seconds]
[14-10-17 11:25:28:369 ICT] Range.getValues() [2.756 seconds]
[14-10-17 11:25:28:408 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:28:541 ICT] Sheet.getDataRange() [0.133 seconds]
[14-10-17 11:25:31:407 ICT] Range.getValues() [2.865 seconds]
[14-10-17 11:25:31:439 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:31:571 ICT] Sheet.getDataRange() [0.131 seconds]
[14-10-17 11:25:34:313 ICT] Range.getValues() [2.741 seconds]
[14-10-17 11:25:34:346 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:34:500 ICT] Sheet.getDataRange() [0.153 seconds]
[14-10-17 11:25:37:357 ICT] Range.getValues() [2.856 seconds]
[14-10-17 11:25:37:387 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:37:526 ICT] Sheet.getDataRange() [0.138 seconds]
[14-10-17 11:25:40:293 ICT] Range.getValues() [2.766 seconds]
[14-10-17 11:25:40:325 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:40:478 ICT] Sheet.getDataRange() [0.152 seconds]
[14-10-17 11:25:43:509 ICT] Range.getValues() [3.031 seconds]
[14-10-17 11:25:43:543 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:43:690 ICT] Sheet.getDataRange() [0.146 seconds]
[14-10-17 11:25:46:523 ICT] Range.getValues() [2.832 seconds]
[14-10-17 11:25:46:558 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:46:689 ICT] Sheet.getDataRange() [0.13 seconds]
[14-10-17 11:25:49:363 ICT] Range.getValues() [2.673 seconds]
[14-10-17 11:25:49:404 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:49:538 ICT] Sheet.getDataRange() [0.133 seconds]
[14-10-17 11:25:52:571 ICT] Range.getValues() [3.033 seconds]
[14-10-17 11:25:52:606 ICT] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 11:25:52:756 ICT] Sheet.getDataRange() [0.149 seconds]
[14-10-17 11:25:55:774 ICT] Range.getValues() [3.016 seconds]

基本上,脚本实际上需要很长时间才能运行,因为它不断调用电子表格。我不知道造成这些电话的原因是什么,除了功能方面的问题之外我没有打电话。

你能帮我理解和解决这个问题吗?感谢。

2 个答案:

答案 0 :(得分:1)

我很确定你有一些电子表格调用超出了你展示的功能,某种“全局变量”在某处使用Sheet.getDataRange()Range.getValues() ......

这些“外部”调用在调用任何函数时运行,这就是您在执行记录中看到的内容。

清理你的代码或将这些调用包装成一个函数,你将获得一个执行记录,如下所示:

[14-10-17 09:16:12:921 CEST] Starting execution
[14-10-17 09:16:12:941 CEST] Logger.log([Looking up active row..., []]) [0 seconds]
[14-10-17 09:16:12:941 CEST] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 09:16:12:942 CEST] Sheet.getActiveCell() [0 seconds]
[14-10-17 09:16:12:943 CEST] SpreadsheetApp.getActiveSheet() [0 seconds]
[14-10-17 09:16:12:943 CEST] Range.getRow() [0 seconds]
[14-10-17 09:16:13:048 CEST] Sheet.getSheetValues([1, 1, 1, 20]) [0.104 seconds]
[14-10-17 09:16:13:049 CEST] Logger.log([[[0.0, xxx0, some values here, some values there, another one, , , , , , , , , , , , , , , ]], []]) [0 seconds]
[14-10-17 09:16:13:050 CEST] Execution succeeded [0.108 seconds total runtime]

答案 1 :(得分:0)

查看此修改后的代码是否运行得更快

function getSelectedRow(){

  Logger.log("Looking up active row...")
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange(sheet.getActiveCell().getRow(), 1, 1, 20).getValues();
  Logger.log(data)
}