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

1.1k views Asked by At

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

There are 1 answers

0
magnetometer On

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