简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM