【发布时间】:2017-10-06 09:39:01
【问题描述】:
在office.js中,是否可以读取工作表属性?
实际上,有人使用 VSTO 开发了一个 excel 插件,他们在那里设置了一个工作表属性。现在我正在开发的 excel 网络插件,我需要阅读该属性。不确定是否可行。
【问题讨论】:
在office.js中,是否可以读取工作表属性?
实际上,有人使用 VSTO 开发了一个 excel 插件,他们在那里设置了一个工作表属性。现在我正在开发的 excel 网络插件,我需要阅读该属性。不确定是否可行。
【问题讨论】:
最近,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());
});
【讨论】: