Excel Javascript API:在Excel中写入数据时无法使用任何工作簿和任何其他工作表

tf7tbtn2  于 2023-05-19  发布在  Java
关注(0)|答案(1)|浏览(129)

我使用React框架开发了一个Excel Javascript插件。
加载项工作流很简单。
1.使用API从Ui请求数据
1.传递实体的列名和授权的API密钥
1.通过拉动按钮开始将数据写入活动Excel工作表。
问题:
在Excel数据写入过程中,整个Excel工作簿冻结,在写入数据之前,我无法使用任何其他工作簿。
来自API的最大数据量为5- 8 K。
下面是示例代码。

try {
  var i = 0;
  // these data are coming from APIs
  var allCompanies =[12,32,33,43,45,66,12,32,10,12,21,90];
  allCompanies.forEach(async _company => {
    // calling APIs for each _docs and writing data for that companies
    await fetchData(_company).then(async (responseRows) => {
      await Excel.run(async (context) => {
        let sheet = context.workbook.worksheets.getActiveWorksheet();
        sheet.load(["name"]);
        await context.sync();
        for (let i = 0; i < responseRows.length; i++) {
          // data operations into Exel sheet
          // creating table, table header, applying Css
          // writting value to each cells
        }
        sheet.getUsedRange().format.autofitColumns();
        sheet.getUsedRange().format.autofitRows();
        await context.sync();
      });
    });
  });
} catch (error) {
  console.log("error on appending data into excel " + error);
}

任何解决办法都对我有帮助。
谢谢。

yftpprvb

yftpprvb1#

如果可能,将context.sync()移出循环。在一个循环中多次调用context.sync()会导致性能下降和延迟增加。
Avoid using the context.sync method in loops

相关问题