office.js 读取工作表属性

office.js read worksheet properties

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

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

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

  • 您需要参考 beta 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 错误。

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