我正在使用 Office JS API 开发 Word 插件。
目前,我可以通过执行以下操作向 Word 文档添加自定义属性:
context.document.properties.load();
context.document.properties.customProperties.add("file-name-prop", "my file name");
如果我随后下载该文件,我可以在压缩的 docx 内的“custom.xml”文件中看到该属性。
但我无法读回该属性。
我正在尝试这样做:
context.document.properties.load();
var filenameProp = context.document.properties.customProperties.getItemOrNullObject("file-name-prop");
if (filenameProp) {
// use filenameProp.value
} else {
// ignore, property not set
}
这样做时,我收到以下错误:
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"
哪种方法是读回该属性的正确方法?
(我正在使用这个office js:appsforoffice.microsoft.com/lib/beta/hosted/office.js
)
可能您没有包含部分代码,但我没有看到您同步上下文的任何地方。您提供的错误消息表明相同的内容:“在读取属性值之前,请在包含对象上调用 load 方法,并在关联的请求上下文上调用“context.sync()”。“。看来你失踪了context.sync()
全部或部分。同步上下文后,您应该能够获取自定义属性。例如,要创建自定义属性,代码应类似于...
function setProperty (prop_name, prop_value) {
Word.run(function (context) {
context.document.properties.customProperties.add(prop_name, prop_value);
return context.sync()
.catch(function (e) {
console.log(e.message);
})
})
}
当您需要获取属性时,代码仍然使用“sync”来使属性可用。例如,要获取自定义属性,代码应类似于...
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);
})
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)