如何使用Google Apps Script为大型数据集(100k行和10列)优化此功能?



此函数运行正常,但最多可用于20k行。我能改变什么让它跑得快?

const SOURCE_FILE_ID = 'ID';
function getData() {
const sourceSheet = SpreadsheetApp.openById(SOURCE_FILE_ID);
const sourceRng = sourceSheet.getSheetByName('ativcopiar').getRange(1, 1, sourceSheet.getLastRow(), 9);
const sourceValues  = sourceRng.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Ativ.');
var destinationRng = sheet.getRange(1, 1, sheet.getLastRow(), 9);
destinationRng.clearContent();
destinationRng.setValues(sourceValues);
}

有时,它会超过时间限制。

欣赏任何光线!谢谢

我相信您的目标如下。

  • 您希望降低脚本的处理成本

在这种情况下,使用Sheets API如何?当使用Sheets API时,可以略微降低工艺成本。参考当您的脚本使用Sheets API时,它将变为如下。

修改的脚本:

在使用此脚本之前,请在高级Google服务中启用Sheets API。

function myFunction() {
const SOURCE_FILE_ID = '###';
const sheet = SpreadsheetApp.openById(SOURCE_FILE_ID).getSheetByName('ativcopiar');
const dstSS = SpreadsheetApp.getActiveSpreadsheet();
var dstSheet = dstSS.getSheetByName('Ativ.');
var destinationRng = dstSheet.getRange(1, 1, dstSheet.getLastRow(), 9);
destinationRng.clearContent();
SpreadsheetApp.flush();
const values = Sheets.Spreadsheets.Values.get(dstSS.getId(), "'ativcopiar'!A1:I" + sheet.getLastRow()).values;
Sheets.Spreadsheets.Values.update({values}, dstSS.getId(), "Ativ.", {valueInputOption: "USER_ENTERED"});
}

参考文献:

  • 基准:使用谷歌应用程序脚本阅读和编写电子表格
  • 方法:spreadsheets.values.get
  • 方法:spreadsheets.values.update

最新更新