首页 > 解决方案 > 在 Office 加载项中启用扩展错误日志记录(查看有关错误对象的完整语句)

问题描述

我正在开发加载项,但遇到错误。
一堆东​​西被打印到控制台,其中一些是:

这意味着什么?如何启用该设置?

标签: javascriptexcelerror-handlingoffice-js

解决方案


好问题!

想象一下你有这样的代码:

async function run() {
  await Excel.run(async (context) => {
    const range = context.workbook.getSelectedRange();
    range.getRow(1000).format.fill.color = "yellow";
    await context.sync();
  });
}

并想象您的选择只是一个很小的范围,这样getRow(1000)会导致异常。

如果你今天运行它,你会得到一些信息:

错误截图

但请注意,您没有得到完整的语句集,只有“周围”的语句(如果你有一堆代码,可能还不够)。而且,有些信息是“...” - 为您提供的。

现在,将以下行粘贴在代码上方的某处:

OfficeExtension.config.extendedErrorLogging = true;

一旦你有了它,你就会得到完整的错误日志(这对调试很有用,但请不要在生产应用程序中这样做——你不想支付性能成本,更重要的是,你不想想要记录和存储敏感数据,这些数据很可能出现在完整语句的日志中(例如,包含客户信息的二维值数组......)

完整的语句,启用后

来自DefinitelyTyped 上的Office d.ts 文件

/** Configuration */
var config: {
  /**
   * Determines whether to log additional error information upon failure.
   *
   * When this property is set to true, the error object will include a "debugInfo.fullStatements" property that lists all statements in the batch request, including all statements that precede and follow the point of failure.
   *
   * Setting this property to true will negatively impact performance and will log all statements in the batch request, including any statements that may contain potentially-sensitive data.
   * It is recommended that you only set this property to true during debugging and that you never log the value of error.debugInfo.fullStatements to an external database or analytics service.
   */
   extendedErrorLogging: boolean;
};

推荐阅读