Google表脚本:执行自定义功能的内部错误

发布于 2025-02-11 06:05:13 字数 1586 浏览 2 评论 0原文

我有一个定期返回错误的自定义函数:内部错误执行自定义函数。根据执行应用脚本网站。

我已经多次与Google联系,以确保这是一个平台错误,但是每次我都会得到相同的答案 - “在您的功能中添加随机延迟”或“使用指数向退回”。我尝试添加随机延迟,这确实有所帮助,但是错误仍然存​​在 - 发生率较小。我目前对所有应用程序脚本API都实现了指数缩写,即使使用此重试逻辑,我也会遇到相同的错误。

我遵循了全部应用程序脚本文档的最佳实践,包括使用范围,即使这样,错误仍然存​​在。

还有其他人在经历这个吗?我尝试在自定义功能的第一行中添加logger.log,并且当我收到此错误时,此代码不会触发。这使我相信自定义函数调用永远不会到达代码执行的服务器。这就是为什么我相信这是一个平台错误。

这是指数向后的代码:

function call_(func) {
    for (var n = 0; n < 6; n++) {
        try {
            return func()
        } catch (e) {
            Logger.log(`Retrying... ${n + 1} times exception: ${e}`)
            if (n === 6 - 1) {
                throw e
            }
            Utilities.sleep(
                Math.pow(2, n) * 1000 + Math.round(Math.random() * 1000)
            )
        }
    }
}

这是自定义功能的凝结版本:

function CUSTOMFUNCTION() {
    let apiResponse = call_(() =>
            UrlFetchApp.fetch(someUrl, {
                muteHttpExceptions: true,
            })
        )

      let response = JSON.parse(call_(() => apiResponse.getContentText()))
    // do some logic with the response
    return value

}

我知道不会捕获HTTP异常,这是有意的。在附加脚本的开发表中测试自定义功能时,无论尝试什么,我都无法重现错误。该错误仅在脚本通过Google Cloud Platform(GCP)部署之后发生。为了清楚起见,在GCP内,我使用Google Workspace Marketplace SDK附加脚本ID,以便任何人可添加到其Google表中。

如果有人有任何意见来帮助解决此错误,我将不胜感激,因为在过去的几个月中,Google团队对此问题并不是很有帮助。

I have a custom function that periodically will return the error: Internal error executing the custom function. The custom function execution lasts 0 (zero) seconds according to the executions page on the App Script website.
enter image description here

I have contact Google multiple times about this insisting that it is a platform error, but I get the same answer every time - "Add a random delay in your function" or "Use exponential backoff". I have tried adding a random delay and this did help slightly, but the error was still there - just less occurrences. I currently have exponential backoff implemented for all App Script APIs and even with this retry logic I get the same error.

I've followed the entirety of the App Script documentation for best practices including using ranges, and even with this the error is still there.

Is anyone else experiencing this? I tried adding a Logger.log on the very first line of my custom function and this code does not trigger when I receive this error. This leads me to believe that the custom function calls just never reach the server where the code executes. This is why I believe it's a platform error.

This is the exponential backoff code:

function call_(func) {
    for (var n = 0; n < 6; n++) {
        try {
            return func()
        } catch (e) {
            Logger.log(`Retrying... ${n + 1} times exception: ${e}`)
            if (n === 6 - 1) {
                throw e
            }
            Utilities.sleep(
                Math.pow(2, n) * 1000 + Math.round(Math.random() * 1000)
            )
        }
    }
}

This is a condensed version of the custom function:

function CUSTOMFUNCTION() {
    let apiResponse = call_(() =>
            UrlFetchApp.fetch(someUrl, {
                muteHttpExceptions: true,
            })
        )

      let response = JSON.parse(call_(() => apiResponse.getContentText()))
    // do some logic with the response
    return value

}

I'm aware that http exception won't be caught and this is intentional. While testing the custom function in the development sheet that the script is attached to, I can't reproduce the error no matter what I try. The error only ever happens after the script is deployed via Google Cloud Platform (GCP). For clarity, inside GCP I use the Google Workspace Marketplace SDK to attach the script ID in order to make the add-on available to anyone to add to their Google Sheets.

If anyone has any input to help with resolving this error I would appreciate it as the Google team hasn't been very helpful over the past few months of triaging this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2025-02-18 06:05:13

API限制。
https://developers.google.com/sheets/api/api/limits

,解决了我们的问题。

async function myFunction(params) {
  let result = -1

  for (let i = 0; i<10; i++){
    try {
      let response = await UrlFetchApp.fetch("MY URL");
      result = await response.getContentText()
      if (result >= 0) {return result}
    } catch(err) {
      await setTimeout(r, 1000*i);
    }
  }
  return err.message
}

然后,我们还将表格设置为自动删除公式
https://spreadsheetpoint.com/auto-refresh-google-sheets/
(可能没有关系)

API Limits.
https://developers.google.com/sheets/api/limits

implement exponential backoff, solved the issue for us.

async function myFunction(params) {
  let result = -1

  for (let i = 0; i<10; i++){
    try {
      let response = await UrlFetchApp.fetch("MY URL");
      result = await response.getContentText()
      if (result >= 0) {return result}
    } catch(err) {
      await setTimeout(r, 1000*i);
    }
  }
  return err.message
}

Then we also set the sheet to auto-refresh formulas
https://spreadsheetpoint.com/auto-refresh-google-sheets/
(might not be related)

小瓶盖 2025-02-18 06:05:13

这个问题最终是由我们的整体脚本大小引起的。

与Google联系后,我们被告知,在每个自定义功能请求上,Google将检索您应用脚本代码的全部内容。不仅是用于自定义功能的代码/文件。下载尺寸的上限为5MB。

因此,我们更改了Webpack配置,导致捆绑包大小(&lt; 1MB)。 Google还创建了一个文件缓存,以在较大的应用脚本项目上加快此过程。

希望这将帮助遇到这个问题的其他人。

The issue ended up being caused by our overall script size.

After reaching out to google we were informed that on each custom function request Google will retrieve the entire contents of your App Script code. Not just the code/file used for custom functions. The upper limit on their download size is 5mb.

Because of this, we changed our webpack configuration resulting in a much smaller bundle size (<1mb). Google also created a file cache to speed up this process on larger App Script projects.

Hopefully this'll help someone else who runs into this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文