Amitava Karan
Amitava Karan

Reputation: 677

office.js read worksheet properties

In office.js, is it possible to read worksheet properties?

Actually, someone develop a excel addin using VSTO, there they set a worksheet property. Now the excel web addin that I am developing, I need to read that property. Not sure if it is possible.

Upvotes: 2

Views: 909

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13480

The ability to read (and set) document properties using Office.js was recently made available as part of API Requirement Set ExcelApi 1.7. This requirement set is currently in Beta, so to use this API feature:

  • You'll need to reference the beta CDN: https://appsforoffice.microsoft.com/lib/beta/hosted/office.js

  • If you're using TypeScript, you'll want to reference the beta d.ts file: https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts

  • You must be using a sufficiently recent build of Excel (e.g., Office Insiders Fast). If you're not on a sufficiently recent build and try to use the read document properties API, an ApiNotFound error will be thrown.

The following code snippet shows show to read document properties (using JavaScript):

Excel.run(function (context) {
    var docProperties = context.workbook.properties;

    // Load a combination of read-only 
    // and writeable document properties.
    docProperties.load("author, lastAuthor, revisionNumber, title, subject, keywords, comments, category, manager, company, creationDate");

    return context.sync()
        .then(function () {

            // Write the document properties to the console.
            console.log("Author: " + docProperties.author);
            console.log("Last author : " + docProperties.lastAuthor);
            console.log("Revision number: " + docProperties.revisionNumber);
            console.log("Title: " + docProperties.title);
            console.log("Subject: " + docProperties.subject);
            console.log("Keywords: " + docProperties.keywords);
            console.log("Comments: " + docProperties.comments);
            console.log("Category: " + docProperties.category);
            console.log("Manager: " + docProperties.manager);
            console.log("Company: " + docProperties.company);
            console.log("Workbook creation date: " + docProperties.creationDate.toDateString());
        });
}).catch(errorHandlerFunction);

And here's the same snippet, but in TypeScript:

Excel.run(async (context) => {
    let docProperties = context.workbook.properties;

    // Load a combination of read-only 
    // and writeable document properties.
    docProperties.load("author, lastAuthor, revisionNumber, title, subject, keywords, comments, category, manager, company, creationDate");

    await context.sync();

    // Write the document properties to the console.
    console.log("Author: " + docProperties.author);
    console.log("Last author : " + docProperties.lastAuthor);
    console.log("Revision number: " + docProperties.revisionNumber);
    console.log("Title: " + docProperties.title);
    console.log("Subject: " + docProperties.subject);
    console.log("Keywords: " + docProperties.keywords);
    console.log("Comments: " + docProperties.comments);
    console.log("Category: " + docProperties.category);
    console.log("Manager: " + docProperties.manager);
    console.log("Company: " + docProperties.company);
    console.log("Workbook creation date: " + docProperties.creationDate.toDateString());
});

Upvotes: 4

Related Questions