google-apps-script - 导致 DriveApp.getFileById + Utilities.parseCsv 执行速度大幅下降的原因
问题描述
该CSV
文件通常有大约 10 行的值,它很小、很轻,但即便如此,所有执行都需要 200 秒才能将CSV
文件中的值导入电子表格。
什么可能导致这种情况,我应该在代码中进行哪些更改以使其快速运行?
function ImportWebData() {
const cleansheet = SpreadsheetApp.getActive().getSheetByName('ImportWebData');
cleansheet.getRange('A1:Z' + cleansheet.getMaxRows()).clear({contentsOnly: true, skipFilteredRows: true});
var sheet = SpreadsheetApp.getActive().getSheetByName('ImportWebData');
var file = DriveApp.getFileById("1aYQLX-67EFJKbGy9TFnis1vjtxiro3r8");
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
注:Drive API
活跃于Google Advanced Services
解决方案
我通过不使用 getMaxRows() 避免了清除可能不存在的数据的不必要步骤,我只使用了 sh.getLastRow(); 我可能已经使用 getLastColumn() 来获取数据,但我不知道文件中的内容。
function ImportWebData() {
const ss = SpreadsheetApp.getActive();
const sh1 = ss.getSheetByName('ImportWebData');
sh1.getRange(1,1,sh1.getLastRow(),26).clear({contentsOnly: true, skipFilteredRows: true});
var file = DriveApp.getFileById("1aYQLX-67EFJKbGy9TFnis1vjtxiro3r8")
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
sh1.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
我猜你把它公开了,它在几秒钟内就运行了
推荐阅读
- javascript - 计时器中会发生反应泄漏吗?
- swift - 如何在 Swift 中设置日期的默认值?
- python - 将 seaborn 散点图图例与高维图例图分开
- python - 如何检查列表是否包含每个元素中相同的第一个索引
- html - 如何将 html 存储在 mysql 数据库中并将其作为 html 在 nodejs 应用程序中检索?
- r - 如何使用 Rugarch 保留原始日期
- javascript - 多文件夹命令处理程序
- while-loop - 如何创建一个一直持续到 5 个变量都等于 10 的 while 循环
- python - 在自动安装 Pylance 之前,我有一个运行良好的 python django 应用程序。现在我收到一个错误:发生异常:
- python - 如何将数据从 ESP8266 发送到 Django 本地服务器