How can I read Excel custom properties in Office Javascript API?

旧街凉风 提交于 2020-01-14 03:06:21

问题


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.

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.

However, the Office.context.document.getFileAsync() is not available in Excel. Is there another way to read the custom properties?


回答1:


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);
            }
        })
 })
 }


来源:https://stackoverflow.com/questions/30374549/how-can-i-read-excel-custom-properties-in-office-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!