Microsoft office.js Excel加载项-使用javascript/react检索工作表/工作簿唯一ID
我们已经构建了一个Excel任务窗格加载项,它主要用于工作表。根据我们的要求,每当用户关闭excel并再次打开它时,我们希望用唯一ID标识excel 从工作簿加载excel ID的示例代码:Microsoft office.js Excel加载项-使用javascript/react检索工作表/工作簿唯一ID,javascript,excel,office365,office-js,office365api,Javascript,Excel,Office365,Office Js,Office365api,我们已经构建了一个Excel任务窗格加载项,它主要用于工作表。根据我们的要求,每当用户关闭excel并再次打开它时,我们希望用唯一ID标识excel 从工作簿加载excel ID的示例代码: Office.initialize = () => { Excel.run(function(context) { var sheet = context.workbook.worksheets.getItem("Sheet1"); const worksheets = conte
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);
});
}
我们收到以下错误:
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);
});
}
未捕获的RichApi。错误:属性“id”不可用。在读取属性值之前,对包含的对象调用load方法,并对关联的请求上下文调用“context.sync()”。
.sync()
方法是异步的,返回一个需要等待的承诺
因此,Office.js中的sync()API调用返回一个承诺
使用承诺的解决方案:
使用异步/等待的解决方案:
您是否尝试过:
sheet.load('id')代码>或sheet.load({id:true})代码>?尝试使用sheet.load('id')我发布了一个包含解决方案的答案:)谢谢Lumpenstein。这真的很有帮助。
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);
});
}