Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microsoft office.js Excel加载项-使用javascript/react检索工作表/工作簿唯一ID_Javascript_Excel_Office365_Office Js_Office365api - Fatal编程技术网

Microsoft office.js Excel加载项-使用javascript/react检索工作表/工作簿唯一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

我们已经构建了一个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);
  });
}
我们收到以下错误:

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