在脚本的Google表格中获取带有#NA的公式的错误注释

2024-02-14

我找不到任何方法来获取失败公式的错误消息(实际上它提供了更多信息,而只是错误代码,因为 #NA 始终为 7)。 #NA 可能意味着不同的东西,我希望能够识别哪些细胞具有哪种类型的 #NA。

getNote不起作用。

有办法吗?

我使用 IMPORTXML(C1:C,"//@value")

但例如,当我 vlookup 失败时,这是我想要检索的错误消息


2021 年 2 月 1 日更新:

As 田池提到的 https://stackoverflow.com/questions/59396959/getting-the-error-note-of-a-formula-with-na-in-google-sheets-for-a-script/59412653#comment116703768_59412653,可以通过以下方式在 Sheets API 中检索错误消息电子表格.get https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get。此信息可以作为以下内容的一部分找到:扩展价值 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#ExtendedValue,对应于属性effectiveValue of the 细胞数据 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#CellData.

响应的示例如下:

{
  "sheets": [
    {
      "data": [
        {
          "rowData": [
            {
              "values": [
                {
                  "effectiveValue": {
                    "errorValue": {
                      "type": "DIVIDE_BY_ZERO",
                      "message": "Function DIVIDE parameter 2 cannot be zero."
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

然后你可以使用先进的床单服务 https://developers.google.com/apps-script/advanced/sheets通过 Apps 脚本访问此 API。例如,如果您想检索单元格的错误消息A1,您可以执行以下操作:

function getErrorMessage(cellNotation = "A1") {
  const optionalArgs = {
    fields: "sheets(data(rowData(values(effectiveValue))))",
    ranges: cellNotation
  }
  const spreadsheetId = SpreadsheetApp.getActive().getId();
  const response = Sheets.Spreadsheets.get(spreadsheetId, optionalArgs);
  const errorMessage = response.sheets[0].data[0].rowData[0].values[0].effectiveValue.errorValue.message;
  return errorMessage;
}

不幸的是,这不能用作自定义函数,因为它需要用户授权(请参阅使用 Apps 脚本服务 https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services).

原答案:

As far as I know, there is no built-in function, no Apps Script tool and no API method that will retrieve this information.

如果您认为拥有此功能非常有用,您可以考虑在以下位置创建功能请求:谷歌问题跟踪器 https://issuetracker.google.com/components/191608.

作为解决方法,您可以为您正在使用和使用的每个内置函数创建一个自定义函数试着抓 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch检索您得到的任何异常。

我希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在脚本的Google表格中获取带有#NA的公式的错误注释 的相关文章

随机推荐