简体   繁体   English

如何提高Google Docs应用脚本的速度

[英]How to increase speed of google docs apps scripts

I have a google apps script which fetches data from spreadsheet. 我有一个Google Apps脚本,可以从电子表格中获取数据。 The data is fetched pretty slowly, I think because of amount of data. 我认为由于数据量的原因,数据的获取速度非常慢。 The first call of this function takes till a second: 此函数的第一次调用要花到第二秒:

sheet.getRange(row, coll).getValue()

I think there must be away to create global variables which will be available all the time the spreadsheet is opened. 我认为必须要创建全局变量,该变量在打开电子表格的所有时间都可用。 I would store the whole spreadsheet on open and access it, which should be much faster. 我将整个电子表格存储在打开位置并访问它,这应该更快。 But I could not find this option. 但是我找不到这个选项。

You can use: sheet.getDataRange().getValues(); 您可以使用: sheet.getDataRange().getValues(); it will result in a 2D array containing all values in the sheet up to the last row and column containing data. 它将导致一个二维数组,其中包含工作表中的所有值,直到最后一行和最后一列包含数据。

For example, if you have a sheet with data in cells A1:B10 and then data in cell E20 the .getDataRange() method will get the range A1:E20 including empty cells. 例如,如果您有一个工作表,其中单元格A1:B10中有数据,然后单元格E20中有数据,则.getDataRange()方法将获得范围A1:E20,其中包括空单元格。

You can then use loops or filters to access or modify the arrays. 然后,您可以使用循环或过滤器来访问或修改阵列。

Without more information or knowing what exactly you are trying to achieve that is the best method I can give you. 如果没有更多信息,或者不知道您到底要实现什么目标,那是我能给您的最好方法。

NOTE: Storing the whole spreadsheet onOpen() is not a very good idea because any changes to the spreadsheet will not be picked up unless you refresh or reopen the spreadsheet. 注意:将整个电子表格存储在onOpen()上不是一个好主意,因为除非刷新或重新打开电子表格,否则不会对电子表格进行任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM