如何在任务窗格加载项中使用 Office.StartupBehavior.load?

How to use Office.StartupBehavior.load inside a taskpane add-in?

根据the docs,打开文档后可以设置代码为运行:

You can configure your Office Add-in to load and run code as soon as the document is opened. This is useful if you need to register event handlers, pre-load data for the task pane, synchronize UI, or perform other tasks before the add-in is visible.

The following code configures your add-in to load and start running when the document is opened.

Office.addin.setStartupBehavior(Office.StartupBehavior.load);

这正是我打算做的:在用户打开任务窗格以减少其加载时间之前,根据文档内容语言为加载项加载翻译。

但是,文档并未明确说明应将上述代码放在何处。它是在 taskpane.js 上(还是我的代码的入口点在 taskpane.html 中)?

此外,文档指出方法 setStartupBehavior 是异步的。等待它的决议与任何事情相关吗?

最后,这个方法调用显然依赖于 Office 全局。我必须在初始化后放置它 (Office.onReady/Office.initialize) 还是立即允许?

有关如何使用 API 的示例,请参阅此示例:excel-shared-runtime-scenario

Office.onReady/Office.initialize之前不能调用,但是可以在里面调用。但是,该效果要等到下次打开文档时才会发生,无需在每次打开文档时都调用它。一旦使用 load 参数调用它,那么只要文档打开,代码就会 运行,unless/until 它会被(重新)调用 none

由于该功能直到下次打开文档时才会生效,所以我认为等待它没有任何意义。