正在 Excel 在线加载自定义 属性

Loading custom property in Excel Online

我想尝试为 Office API 为 JavaScript 执行此功能:

public loadCustomProperties() {
    Excel.run(async (ctx) => {
        let custom = ctx.workbook.properties.custom;    
        custom.load();
        return ctx.sync();
    })
}

但是我得到了一个错误 ERROR Error: Uncaught (in promise): GeneralException: An internal error occurred...(没有具体的)

当我尝试加载 properties 而不是 properties.custom 时一切正常。

请帮忙:)

编辑:
这是我得到的错误:

ERROR Error: Uncaught (in promise): GeneralException: An internal error occurred while processing the request. RichApi.Error: An internal error occurred while processing the request. at new r (excel-web-16.00.js:21) at t.c.processRequestExecutorResponseMessage (excel-web-16.00.js:21) at excel-web-16.00.js:21 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) at Object.onInvoke (core.js:3760) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138) at zone.js:872 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:3751) at new r (excel-web-16.00.js:21) at t.c.processRequestExecutorResponseMessage (excel-web-16.00.js:21) at excel-web-16.00.js:21 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) at Object.onInvoke (core.js:3760) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138) at zone.js:872 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:3751) at resolvePromise (zone.js:814) at zone.js:724 at rejected (main.js:103) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) at Object.onInvoke (core.js:3760) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138) at zone.js:872 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:3751)

编辑 2:

我发现这是一个已知错误:https://github.com/OfficeDev/office-js/issues/179

修改后的代码有 3 处更改。

  1. 第一行缺少异步
  2. 缺少'function'
  3. 最后,正如 shanks 提到的,您错过了 context.sync()
  4. 上的 await

我添加了一个 console.log 只是为了验证属性是否已加载并且它们是。

async function loadCustomProperties() {
   await Excel.run(async (ctx) => {
        let custom = ctx.workbook.properties.custom;
        custom.load();
        await ctx.sync();
        console.log(custom);
    })
}

我想出了这个加载项目的变通方法。事实证明,如果你保证count大于0,你就可以安全地加载customProperties。

async function loadCustomPropertiess() {
    await Excel.run(async (context) => {
        var customProperty = context.workbook.properties.custom;
        var customPropertyCount = customProperty.getCount();
        await context.sync();

        if (customPropertyCount.value > 0) {
            customProperty.load();
            await context.sync();
            customProperty.items.forEach(prop => console.log(prop));
        } else {
            console.log("No custom properties");
        }
    });
}