office.js读取工作表属性

时间:2017-10-06 09:39:02

标签: office-js excel-addins

在office.js中,是否可以阅读工作表属性?

实际上,有人使用VSTO开发了一个excel插件,他们在那里设置了一个工作表属性。现在我正在开发的excel web插件,我需要读取该属性。不确定是否可能。

1 个答案:

答案 0 :(得分:4)

最近,使用Office.js读取(和设置)文档属性的功能已作为API Requirement Set ExcelApi 1.7 的一部分提供。此要求集当前位于 Beta 中,因此要使用此API功能:

  • 您需要参考测试版 CDN:https://appsforoffice.microsoft.com/lib/beta/hosted/office.js

  • 如果您使用的是TypeScript,则需要引用 beta d.ts文件:https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts

  • 您必须使用最新版本的Excel(例如Office Insiders Fast)。如果您没有使用足够新的版本并尝试使用读取文档属性API,则会引发ApiNotFound错误。

以下代码段显示show以读取文档属性(使用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);

这是相同的片段,但在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());
});