【问题标题】:office.js read worksheet propertiesoffice.js 读取工作表属性
【发布时间】:2017-10-06 09:39:01
【问题描述】:

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

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

【问题讨论】:

    标签: office-js excel-addins


    【解决方案1】:

    最近,API Requirement SetExcelApi 1.7 提供了使用 Office.js 读取(和设置)文档属性的功能。此要求集目前处于测试版,因此要使用此 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 错误。

    以下代码 sn-p 展示了如何读取文档属性(使用 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);
    

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

    【讨论】:

    • 感谢@Kim 的回复。当我在网上尝试 excel 但在 excel 桌面上没有时,它正在工作。我认为这是因为我没有“Office Insider Fast”。我订阅了 Office 365 专业增强版。如 Office Insider 主页products.office.com/en-us/office-insider?tab=Windows-Desktop 中所述,我在 Excel 桌面的 Account 页面中找不到 Office Insider 磁贴
    • 我们也在阅读工作簿属性。你知道如何读取工作表属性吗?
    • @amitavak -- 感谢您提供更多信息。我怀疑 Office.js 尚不支持在 worksheet(而不是在 workbook 上,正如我的回答所描述的那样)获取/设置自定义属性,但我已经达到让中小企业确认是否是这种情况。如果我能够以一种或另一种方式得到确认,我将在此处添加另一条评论。
    • @MHS -- 在我的示例中,正在读取(并记录到控制台)的 title 属性是文件级属性 title如果您选择 File >> Info(在 Windows 桌面上运行的 Excel 中),则会显示在 Properties 下。它不是您在文档顶部看到的“标题”(文本),也不是文件本身的名称。我怀疑如果您选择 File >> Info,您会看到 Title 字段未填充(在 Properties 下) - - 除非您明确设置它,否则它不会被填充。
    • @MHS -- 谢谢你的链接,我会看看那个帖子。另外,关于获取登录用户信息的问题 - 请参阅此答案以获取更多信息:stackoverflow.com/questions/44024992/…
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多