简体   繁体   English

Microsoft office.js Excel 插件 - 使用 javascript/react 检索工作表/工作簿唯一 ID

[英]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

Solution using promises:使用承诺的解决方案:

  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);
  });
}

Solution using async/await:使用 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);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft office.js Excel 插件 - 如何为折线图中的数据系列实现箭头样式? - Microsoft office.js Excel add-in - How to implement arrow style for a data series in a line chart? Office.js 加载项:在 Excel 2016 中插入图像/图片 - Office.js Add-in: Insert image/picture in Excel 2016 如何使用office.js加载项在在线Excel右任务窗格上显示/隐藏关闭按钮 - How to show/hide the close button on online excel right task pane using office.js add-in 使用异步执行上下文为Office.js Javascript Word加载项添加子例程 - Add sub-routine for Office.js Javascript Word add-in using asynchronous execution context Office.JS:如何使用Office 2016 OnPrem加载Word / Excel任务窗格加载项? - Office.JS: How to sideload Word/Excel Taskpane add-in with Office 2016 OnPrem? 是否可以从 Office.JS Excel 插件中启动现有的 JS 模块? - Is it possible to kick-off an existing JS module from within an Office.JS Excel Add-in? 在office.js Excel加载项中为单个列设置列宽 - Set column width for a single column in office.js excel add-in Office.js 加载项不适用于动态列 - Office.js Add-in not working with dynamic columns 我使用Office.js时的附加模块 - My add-in block when I use Office.js 如何使用 Office.js API 缩放 Excel 中的工作表? - How to zoom worksheet in Excel with Office.js API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM