【问题标题】:Google Apps Script Write While Executing执行时编写 Google Apps 脚本
【发布时间】:2021-02-02 20:05:40
【问题描述】:

我正在使用 google 电子表格和 Apps 脚本来收集一些财务数据,其中一个功能需要一些时间,因为它需要处理大量信息。

因此,谷歌向我展示了超过最大执行时间,但即使有一个 for 循环在每个循环上写入文件,它也不会在函数完成之前执行它。

有人可以告诉我在执行时如何编写电子表格吗?

谢谢。

在这里留下一个示例脚本。

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var rangeSheet = activeSheet.getRange(1, 1, activeSheet.getLastRow() - 1, 1).getValues();
  // after cleared out i have an array of values so i do:
  rangeSheet.forEach(async (el, idx) => {
    let result = await anotherFunction(el) // <--- this is the function taking around 2 minutes to complete
    sheet.getRange(`$B${idx + 1}`).setValue(`${result}
  })
}

【问题讨论】:

  • 能否提供anotherFunction相关的代码?

标签: javascript google-apps-script google-sheets


【解决方案1】:

这会给你的性能带来一些提升:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var rangeSheet = activeSheet.getRange(1, 1, activeSheet.getLastRow() - 1, 1).getValues();
  var data = [];
  rangeSheet.forEach(async (el, idx) => {
    let result = await anotherFunction(el) // <--- this is the function taking around 2 minutes to complete
    data.push([result]);
  })
  sheet.getRange(1,2,data.length,data[0].length).setValues(data);
}

阅读Best Practices了解更多信息。

但是,如果anotherFunction 花费了太多时间,那么恐怕你需要重新设计你的逻辑。如果您想获取多个 URL,我建议您使用 UrlfetchApp.fetchAll()

参考资料:

google apps script with UrlfetchApp.fetchAll() or with async/ await for multiple http requests?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多