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检索您得到的任何异常。
我希望这有帮助。