在Excel Online中加载自定义属性

时间:2018-07-12 14:09:33

标签: javascript excel office365 office-js office-addins

我想为Office API for 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错误:未捕获(承诺):GeneralException:内部   处理请求时发生错误。       RichApi.Error:处理请求时发生内部错误。           在新的r(excel-web-16.00.js:21)           在t.c.processRequestExecutorResponseMessage(excel-web-16.00.js:21)           在excel-web-16.00.js:21           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:388)           在Object.onInvoke(core.js:3760)           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:387)           在Zone.push ../ node_modules / zone.js / dist / zone.js.Zone.run(zone.js:138)           在zone.js:872           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:421)           在Object.onInvokeTask(core.js:3751)           在新的r(excel-web-16.00.js:21)           在t.c.processRequestExecutorResponseMessage(excel-web-16.00.js:21)           在excel-web-16.00.js:21           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:388)           在Object.onInvoke(core.js:3760)           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:387)           在Zone.push ../ node_modules / zone.js / dist / zone.js.Zone.run(zone.js:138)           在zone.js:872           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:421)           在Object.onInvokeTask(core.js:3751)           在resolvePromise(zone.js:814)           在zone.js:724           在被拒绝时(main.js:103)           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:388)           在Object.onInvoke(core.js:3760)           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:387)           在Zone.push ../ node_modules / zone.js / dist / zone.js.Zone.run(zone.js:138)           在zone.js:872           在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:421)           在Object.onInvokeTask(core.js:3751)

编辑2:

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

2 个答案:

答案 0 :(得分:1)

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

  1. 在第一行缺少异步
  2. 缺少“功能”
  3. 最后,如小腿所说,您错过了context.sync()的等待状态

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

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

答案 1 :(得分:0)

我想出了一种解决方法来加载项目。事实证明,如果确保计数大于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");
        }
    });
}