Microsoft office.js Excel 加载项 - 使用 javascript/react 检索 worksheet/workbook 唯一 ID
Microsoft office.js Excel add-in - retrieve worksheet/workbook unique ID using javascript/react
我们构建了一个主要用于工作表的 Excel 任务窗格插件。根据我们的要求,我们希望在用户关闭 excel 并再次打开它时使用唯一 ID 识别 excel。
从工作簿加载 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);
});
}
我们收到以下错误:
Uncaught RichApi.Error: The property 'id' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.
.sync()
方法是异步的,returns 需要等待的承诺。
Therefore, the sync() API call in Office.js returns a 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);
});
}
使用 async/await 的解决方案:
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);
});
}
我们构建了一个主要用于工作表的 Excel 任务窗格插件。根据我们的要求,我们希望在用户关闭 excel 并再次打开它时使用唯一 ID 识别 excel。
从工作簿加载 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);
});
}
我们收到以下错误:
Uncaught RichApi.Error: The property 'id' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.
.sync()
方法是异步的,returns 需要等待的承诺。
Therefore, the sync() API call in Office.js returns a 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);
});
}
使用 async/await 的解决方案:
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);
});
}