gpt4 book ai didi

google-apps-script - Google Apps 脚本在电子表格上迭代非常慢

转载 作者:行者123 更新时间:2023-12-03 07:21:48 26 4
gpt4 key购买 nike

第一次发帖,读了好久:)

我刚刚编写了第一个 Google Apps 脚本,用于将 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);
};

最佳答案

首先查看this page的信息.

我经历过的三件事确实会减慢你的速度:

  1. 多次调用外部服务
  2. 调用 SpreadsheetApp.flush()循环强制更新工作表。
  3. 调用 Range.getValue()很多。

对于第一部分,尝试批量调用。例如,如果您请求本地货币和外币的汇率,请尝试在一个请求中发送一堆查询。

对于第二部分,除非确实必要,否则不要调用此方法。

对于第三部分,尝试修改您的代码,以便您可以调用此方法一次并移动其结果,而不是在多个方法中调用它以获得相同的结果。

提示:为了避免修改许多方法参数,只需在方法树调用的开头传递一个对象并用您的参数填充它。这样您就不必修改您传递的每个方法来添加\删除参数。这个概念适用于所有带有函数的编程语言。

关于google-apps-script - Google Apps 脚本在电子表格上迭代非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27166493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com