问题描述
我有一个标签窗格应用,需要访问当前MS Office文档的自定义属性,可以是Word或Excel。
I have a tab pane app that needs to access the custom properties of the current MS Office document, which can be Word or Excel.
似乎没有使用Office JavaScript API构建此方法,但在Word中,我使用Office.context.document.getFileAsync()方法返回整个文件。然后我可以解压缩它,读入custom.xml文件,然后浏览XML以获取自定义属性。
There seems to be no built in way of doing this with the Office JavaScript API, but in Word, I use the Office.context.document.getFileAsync() method to return the entire file. I can then unzip it, read in the custom.xml file, and navigate through the XML to get the custom properties.
但是,Office.context.document.getFileAsync()在Excel中不可用。还有另一种方法来读取自定义属性吗?
However, the Office.context.document.getFileAsync() is not available in Excel. Is there another way to read the custom properties?
推荐答案
我知道这个问题已经很老了,但是因为我偶然发现它在自己寻找答案的同时,我会回答它。以下JavaScript函数将在当前文档的末尾打印所有自定义文档属性。它需要Office API的1.3版(另请参阅)。
I know that the question is quite old, but since I stumbled upon it while searching for the answer myself, I'm going to answer it nevertheless. The following JavaScript function is going to print all custom document properties at the end of the current document. It requires version 1.3 of the Office API (see also https://dev.office.com/reference/add-ins/word/documentproperties).
function getProperties() {
Word.run(function (context) {
var body=context.document.body;
var customDocProps = context.document.properties.customProperties;
context.load(customDocProps);
return context.sync().then(function () {
for (var i = 0; i < customDocProps.items.length; i++) {
body.insertText(customDocProps.items[i].key, Word.InsertLocation.end);
body.insertText('\n', Word.InsertLocation.end);
body.insertText(customDocProps.items[i].value, Word.InsertLocation.end);
body.insertText('\n', Word.InsertLocation.end);
}
})
})
}
这篇关于如何在Office Javascript API中读取Excel自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!