Google apps script 谷歌应用程序脚本在电子表格上的迭代速度非常慢
第一次发布,长时间阅读:) 我刚刚编写了我的第一个GoogleApps脚本,用于整理14个电子表格中的信息,每个电子表格包含2-30个工作表,并将其整合到一个报告电子表格中 该脚本运行良好,它检查单个列中的数据,如果找到,则获取该行的电子表格名称、工作表名称、第一列数据以及检查列中的数据,并将其作为数据子数组添加到数组中 然后计算子数组数组的面积,并将数据写入报告文件(脚本从该文件运行) 我唯一的问题是脚本运行大约需要2分钟 我想知道我的方法是否效率低下,是否希望有人能检查脚本并告诉我是否犯了一些错误 下面是:Google apps script 谷歌应用程序脚本在电子表格上的迭代速度非常慢,google-apps-script,google-sheets,Google Apps Script,Google Sheets,第一次发布,长时间阅读:) 我刚刚编写了我的第一个GoogleApps脚本,用于整理14个电子表格中的信息,每个电子表格包含2-30个工作表,并将其整合到一个报告电子表格中 该脚本运行良好,它检查单个列中的数据,如果找到,则获取该行的电子表格名称、工作表名称、第一列数据以及检查列中的数据,并将其作为数据子数组添加到数组中 然后计算子数组数组的面积,并将数据写入报告文件(脚本从该文件运行) 我唯一的问题是脚本运行大约需要2分钟 我想知道我的方法是否效率低下,是否希望有人能检查脚本并告诉我是否犯了一
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function getFaults() {
/** opens each spreadsheet for Liddon and examines the "Report/Replace" column "F"
if there is data there then get the
[Sheetname], [fault area (column "A" row relative to the "F" field found)] and the ["F" field data]
**/
var reportsheet = SpreadsheetApp.getActiveSheet();
var reportdata = []
var reportrow = 0
var liddonblocks = [
"1APshQevK7iZxhP7--zmtuM3K6dPTgTZjmNarQ6CEsV4", "1riCQMOa38jo4nCD4qjW1BFZKk5xpXFZiCXHzXpiYKIU", "1NTKXmted1-U12MiqvCGRuYBdhPy1_eLiPn7v8_oVKFE", "1RKOJUNNi5TAg5dETZDtLjZOkUSheuguzmtdPelMclMI",
"1b5-fzCp0wzW8llpUc_6xi1iTFzsapZh9ASSFgDYt4WU", "1qJtY37K0zwoJcz7LdyHhWgkypRMP9LabBchNLM4Fgow", "1yvf4W8-SkfTH-n-PdDNQeyEDEz-shzTe-Id57S_YB2M", "1ETZc1xeNGXU6ipb1XQiD8SiIyRXzZtiJfS4AClKroJk",
"1tJ5u3Hv0uz-n2cdw-QYixKnuMG9skvrUbz1UROhIm34", "1DjhmIdD0GrPxR-fv7pCPkIwIyfai5BHsK9GhT-Hcs3k", "15w39NZZIacD1OfiTWG1E3HmOhV0B_e2Jsuan_ySwf2Q" , "1cK2HBLEftYOZEkCcxs1TX1PxcJRiKTZpQrcsOfE4B1s",
"16W_bfMKk98wkLpEmm2Q68Ta_SrCA8EBarQyGF2yfm18","1_Z_tgF5UAfq3fxPsDEe40z2GZSehhL-u4hEuVszrbn8" ]
// loop through the spreadsheets
for (block = 0; block < liddonblocks.length; block++) {
//open the spreadsheet using the index from the liddonblocks list
var ss = SpreadsheetApp.openById(liddonblocks[block]);
//get all of the sheets within the spreadsheet
var sheets = ss.getSheets();
//loop through each sheet in each spreadsheet using the length of the number of sheets in the spreadsheet as the index
for (var sheetnum = 0; sheetnum < sheets.length; sheetnum++) {
//get an array of all data in the sheet
//assigns array in the form of: [[area, fault], [Bedroom, Broken Bed], [Bathroom, ]]
//where each sub-array is a row of data starting at row 1 eg: [[row1-col1, row1-col2...],[row2-col1, row2-col2...]...]
data = sheets[sheetnum].getDataRange().getValues();
//get the text name of the sheet
name = sheets[sheetnum].getSheetName();
// iterate over the data set and look for values in the 5th column, starting at row 7 to exclude the headers.
// this is the column named "Report / Replace "
for (var count = 7; count < data.length; count++) {
if (data[count][5] != "" && data[count][5] != 0) {
//if there is data in the 5th column of the row then append the following data to the reportdata array and a sub-array
// [ sheetname, columnA, columnF ]
reportdata[reportrow] = [ ss.getName(), name, data[count][0], data[count][5]]
//increment the reportcount variable so any further hits on data in column 5 are created as sequentail sub-arrays in the reportdata array.
reportrow++
}
}
}
}
//write the contents of reportdata to the console
var range = reportsheet.getRange(2,1,reportrow,reportdata[0].length);
range.setValues(reportdata);
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Update Report",
functionName : "getFaults"
}];
spreadsheet.addMenu("Keble Scripts", entries);
};
/**
*检索活动电子表格中包含数据和日志的所有行
*每行的值。
*有关使用电子表格API的更多信息,请参见
* https://developers.google.com/apps-script/service_spreadsheet
*/
函数getFaults(){
/**打开Liddon的每个电子表格并检查“报告/替换”列“F”
如果有数据,则获取
[Sheetname],[fault area(找到的与“F”字段相关的列“A”行)]和[“F”字段数据]
**/
var reportsheet=SpreadsheetApp.getActiveSheet();
var reportdata=[]
var reportrow=0
变量liddonblocks=[
“1PSHQEVK7IZXHP7——ZMTUMU3K6DPTGTZJMNARQ6CESV4”、“1RICQMOA38JO4NCD4QJW1BFZK5XPXFZICxHZZPYKIU”、“1NTKXmted1-U12IQVCGRUYBDHY1_eLiPn7v8ñoVKFE”、“1KOJUNNI5TAG5DETTLZDJOKZKUSHEUZMPELMCLMI”,
“1b5-fzCp0wzW8llpUc6xi1iTFzsapZh9ASSFgDYt4WU”、“1QJTY370KZWOJCZ7LDYHHWGKYPRMP9LABBCHNLM4FGOW”、“1yvf4W8-SkfTH-n-PDDNQEYEEDEZ-shzTe-Id57S_YB2M”、“1ETZC1XENGXU6IPB1XQID8SIIYRXZTIJFS4ACLKROJK”,
“1tJ5u3Hv0uz-n2cdw-QYixKnuMG9skvrUbz1UROhIm34”、“1DJHMID0GRPXR-FV7PCPKIYFAI5BHSK9GHT-Hcs3k”、“15W39NZZZIACD1ofITWG13HMOHV0B_ySwf2Q”、“1CK2HBLEFTYOZEKCS1TXX1TXCJRIKTZPQRCSOFE4B1S”,
“16W_BFMKK98WKLPEM2Q68TA_SrCA8EBarQyGF2yfm18”,“1_Z_TGF5UAFQ3FXPSDEE40Z2GZSEHL-u4hEuVszrbn8”]
//循环浏览电子表格
对于(block=0;block
我同意Serge的评论,即此代码已经进行了很好的优化,打开许多电子表格需要一些时间
我看到了一个改进的机会,但它可能对速度的影响非常小(如果有的话)。您可以将ss.getName()调用移出内部循环,而不是在打开电子表格后立即将其分配给变量,然后在最内部的循环中引用该变量
请注意,根据我的经验,谷歌服务调用的速度往往相差很大,所以有时可能运行得更快或更慢。通过查看“视图”菜单下脚本编辑器的执行记录,可以查看每个调用所花的时间。首先,查看有关的信息 我经历过的三件事会让你慢下来: