Excel数据写入过程中工作簿挂起

问题描述 投票:0回答:1

我们使用 React 框架开发了一个 Excel Javascript 插件。

插件工作流程如下:

  1. 使用带有一些参数的 API 从 Ui 请求数据
  2. 等待响应并将其保存在本地缓存中
  3. 开始将数据写入活动的 Excel 工作表

问题:

在Excel数据写入过程中,Excel工作簿被冻结,直到数据未写入sheet中

我的情况来自API的最大数据量是5-8K。

以下是示例代码。

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

任何解决方案都会对我有帮助。

谢谢。

excel office365 office-js office-addins excel-web-addins
1个回答
2
投票

如果可能,将 context.sync() 移出循环。在循环内多次调用 context.sync() 可能会导致性能下降和延迟增加。

避免在循环中使用 context.sync 方法

© www.soinside.com 2019 - 2024. All rights reserved.