[英]Microsoft office.js Excel add-in - retrieve worksheet/workbook unique ID using javascript/react
We have built an Excel Task pane add in that primarily works with worksheets.我们已经构建了一个 Excel 任务窗格插件,主要用于工作表。 As per our requirement we wants to identify excel with there unique ID's whenever user close's the excel and reopen it again.根据我们的要求,我们希望在用户关闭 excel 并再次重新打开时使用唯一 ID 识别 excel。
Sample code to load excel ID from workbook:从工作簿加载 excel ID 的示例代码:
Office.initialize = () => {
Excel.run(function(context) {
var sheet = context.workbook.worksheets.getItem("Sheet1");
const worksheets = context.workbook.worksheets;
//tried to load ID property using below code
worksheets.load("id, name");
worksheets.load(["items/id", "items/name"]);
worksheets.load(["id", "name", "worksheet/id"]);
sheet.load(["items/id", "items/name"]);
context.sync();
//below is the code to print excel ID
console.log(sheet.id);
// OR
worksheets.items.forEach(ws => {
console.log(`id: ${ws.id}, name: ${ws.name}`)
});
}).catch(function(error) {
console.log(error.debugInfo);
});
}
We are receiving the following error:我们收到以下错误:
Uncaught RichApi.Error: The property 'id' is not available.未捕获的 RichApi.Error:属性“id”不可用。 Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.在读取属性的值之前,调用包含对象的 load 方法,并在关联的请求上下文上调用“context.sync()”。
The .sync()
method is async and returns a promise that needs to be awaited. .sync()
方法是异步的,并返回一个需要等待的承诺。
Therefore, the sync() API call in Office.js returns a promise First Paragraph因此,Office.js 中的sync() API 调用返回一个promise First Paragraph
Office.initialize = () => {
Excel.run(function (context) {
var sheet = context.workbook.worksheets.getItem("Sheet1");
sheet.load(["items/id", "items/name"]);
context.sync().then(() => {
console.log(sheet.id);
});
});
}).catch(function(error) {
console.log(error.debugInfo);
});
}
Office.initialize = () => {
Excel.run(async function (context) {
var sheet = context.workbook.worksheets.getItem("Sheet1");
sheet.load(["items/id", "items/name"]);
await context.sync()
console.log(sheet.id);
});
}).catch(function(error) {
console.log(error.debugInfo);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.