如何在使用 Office JS 与用户交互期间覆盖暂停公式计算的默认行为

huangapple go评论85阅读模式
英文:

How to override the default behavior of suspending formula calculations during user interactions with Office JS

问题

我正在使用Office JS API执行公式。

默认情况下,Excel在用户积极与工作表交互,如编辑单元格值、选择单元格或进行其他更改时,会暂停公式计算。一旦用户完成交互,Excel会自动恢复公式计算并相应地更新单元格值。

当我使用Office JS API执行类似Web服务的公式(该公式在线获取结果并需要一些时间来执行),并与工作表进行交互时,它不会等待公式执行。那么,是否有办法在与工作表交互时等待公式结果?

我正在按照以下步骤使用Office JS API执行公式。

https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-ranges-set-get-values

英文:

I am executing formulas with the help of Office JS API.

By default, Excel suspends formula calculations while the user is actively interacting with the worksheet, such as editing cell values, selecting cells, or making other changes. Once the user finishes their interaction, Excel automatically resumes the formula calculations and updates the cell values accordingly.

When I am executing formulas like web service (which fetches results online & takes time to execute) with the help of Office JS API and interacting with worksheets it does not wait for formula execution. So, Is there any way to wait for formula results while interacting with worksheets?

I am following this steps to execute formulas using Office JS API.

https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-ranges-set-get-values

答案1

得分: 1

听起来你需要在类似以下代码的上下文中设置计算模式:

async function setCalculationMode() {
  await Excel.run(async (context) => {
    context.application.calculationMode = Excel.CalculationMode.manual;
    await context.sync();
  });
}

Excel会在代码中的更改几乎实时显示出来。对于大型迭代数据集,你可能不需要在屏幕上实时看到这些进展。Application.suspendScreenUpdatingUntilNextSync()会暂停Excel的视觉更新,直到插件调用context.sync(),或者直到Excel.run结束(隐式调用context.sync)。请注意,Excel在下一次同步之前不会显示任何活动迹象。

了解更多信息,请查阅使用Excel JavaScript API进行性能优化文章。

英文:

Sounds like you need to set the calculation mode in a context like in the following code:

async function setCalculationMode() {
  await Excel.run(async (context) => {
    context.application.calculationMode = Excel.CalculationMode.manual;
    await context.sync();
  });
}

Excel displays changes your add-in makes approximately as they happen in the code. For large, iterative data sets, you may not need to see this progress on the screen in real-time. Application.suspendScreenUpdatingUntilNextSync() pauses visual updates to Excel until the add-in calls context.sync(), or until Excel.run ends (implicitly calling context.sync). Be aware, Excel will not show any signs of activity until the next sync.

Read more about that in the Performance optimization using the Excel JavaScript API article.

huangapple
  • 本文由 发表于 2023年6月12日 12:39:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定