首页 > 解决方案 > Excel:如何在运行 context.sync() 之前确定单元格是否处于编辑模式

问题描述

我正在开发的一个插件在单元格处于编辑模式时遇到 context.sync 失败的问题。

我想在 Excel.Run 中使用 delayForCellEdit ,但它会导致 UI 挂起,直到用户退出编辑模式。

有没有一种方法可以在调用 context.sync 之前确定 Excel 是否处于编辑模式?

我的第一个倾向是跑步

Excel.run(ctx => { 
  ctx.sync().then(() => { 
    console.log("not in edit mode); 
  }).catch(err => { 
    console.log("in edit mode");
  });
});

每隔几秒就有一次心跳,但想要更优雅一点的东西。

标签: exceloffice-jsoffice-js-ux

解决方案


Currently we only support delayForCellEdit to delay the context.sync() until exit edit mode. And I tested locally didn't meet the hang issue as you mentioned.

Here is my sample code for test and just for your reference: $("#run").click(() => tryCatch(run));

async function run() {
  await Excel.run({ delayForCellEdit: true }, async context => {
    let sheet;
    sheet = context.workbook.worksheets.getItem("Sheet1");
    let range = sheet.getRange("A1");
    range.values = [["1"]];
    await context.sync();
  }); 
}

推荐阅读