Word加载项-如何读取自定义文档属性 [英] Word Add-in - How to read custom document property

查看:103
本文介绍了Word加载项-如何读取自定义文档属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Office JS API开发Word插件.

I am developing a Word plugin using the Office JS API.

目前,我可以通过以下操作将自定义属性添加到Word文档中:

Currently I can add custom properties to the Word document by doing:

context.document.properties.load();
context.document.properties.customProperties.add("file-name-prop", "my file name");

如果我随后下载该文件,则可以在压缩的docx内的"custom.xml"文件中看到该属性.

If I then download the file I can see the property in the "custom.xml" file inside the zipped docx.

但是我无法读回该属性.

But I am not able to read the property back.

我正在尝试这样做:

context.document.properties.load();
var filenameProp = context.document.properties.customProperties.getItemOrNullObject("file-name-prop");
if (filenameProp) {
    // use filenameProp.value
} else {
    // ignore, property not set
}

这样做时,出现以下错误:

When doing that, I am getting the following error:

code : "PropertyNotLoaded"
message : "The property 'type' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context."
name : "OfficeExtension.Error"

哪种是正确读取属性的正确方法?

Which would be the right way to read the property back?

(我正在使用此办公室js:appsforoffice.microsoft.com/lib/beta/hosted/office.js)

(I am using this office js: appsforoffice.microsoft.com/lib/beta/hosted/office.js)

推荐答案

可能是您没有包含代码的各个部分,但是我看不到任何同步上下文的地方.您提供的错误消息表示相同的内容:"在读取属性值之前,在包含的对象上调用load方法,并在关联的请求上下文上调用" context.sync()".".看起来您完全或部分缺少了context.sync().上下文同步后,您应该能够获得自定义属性.例如,要创建自定义属性,代码应类似于...

May be you didn't include the parts of your code, but I don't see anywhere you sync context. The error message you have provided indicates the same: "Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.". Looks like you are missing context.sync() at all or partially. You should be able to get custom properties after the context is synced. For example to create custom property the code should look similar to ...

function setProperties() { 
    Word.run(function (context) {
        context.document.properties.customProperties.add("prop_name", "prop_value");
        return context.sync()
        .catch(function (e) {
            console.log(e.message); 
        })
    })
}

并且当您需要获取属性时,代码仍会使用同步"来使属性可用.例如,要获取自定义属性,代码应类似于...

And when you need to get a property the code is still uses "sync" to make property available. For example to get custom property the code should look similar to ...

function getProperties() { 
    Word.run(function (context) {
        var customDocProps = context.document.properties.customProperties;
        context.load(customDocProps);
        return context.sync()
            .then(function () {
                console.log(customDocProps.items.length);
             })
     })
}

这篇关于Word加载项-如何读取自定义文档属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆