Nat*_*ush 7 office-addins excel-addins office365 office-js
我正在尝试使用 Excel Javascript API 创建一个插件:
window.data = "data"
)。据我所知,不可能ShowTaskpane
通过按钮上的多个操作来做到这一点,因为这似乎不适用于共享运行时。
我目前采取的方法是:
<FunctionFile resid="Taskpane.Url"/>
Run Code Online (Sandbox Code Playgroud)
(其中<bt:Url id="Taskpane.Url" DefaultValue="https://localhost:3000/taskpane.html"/>
)
<Control xsi:type="Button" id="FirstButton">
...
<Action xsi:type="ExecuteFunction">
<FunctionName>first</FunctionName>
</Action>
</Control>
<Control xsi:type="Button" id="SecondButton">
...
<Action xsi:type="ExecuteFunction">
<FunctionName>second</FunctionName>
</Action>
</Control>
Run Code Online (Sandbox Code Playgroud)
first
和second
作为函数(并在taskpane.html
文件中定义)。这些函数中的每一个 a) 在 Taskpane 应用程序中设置一些状态(以使其显示不同的内容,具体取决于函数调用),然后调用Office.addin.showAsTaskpane()
. 代码大致如下:但是,这不会按预期工作。也就是说,它在任务窗格一侧的单独 iframe 中打开任务窗格。它看起来像这样:
当我添加Office.addin.showAsTaskpane()
对share
函数的调用时,它看起来像这样:
如何使用执行功能操作但在任务窗格中执行此操作?我想要一个共享的运行时,并且我希望所有的运行时都打开并位于任务窗格中。该文档使 AppDomains 似乎可以实现这一目标,但我不确定我做错了什么。
感谢您提供任何信息 - 我真的很感激!
我认为这里的关键问题是您想根据函数执行更改任务窗格中的某些状态。这实际上很简单,您不需要共享运行时。
在任务窗格的初始化部分,您必须注册事件侦听器以进行存储。
window.addEventListener('storage', () => {
console.log('your local storage has changed');
});
Run Code Online (Sandbox Code Playgroud)
在您的函数中,您可以利用本地存储,在其中写入值,当事件引发时您可以在任务窗格中访问这些值。
// example globally exposed function to be called using ExecuteFunction
function first(event) {
localStorage.setItem('routePath', 'firstRoute');
event.completed();
}
Run Code Online (Sandbox Code Playgroud)
然后在你的任务窗格应用程序中你可以有
window.addEventListener('storage', () => {
const askedPath = localStorage.getItem('routePath');
if (askedPath !== existingPath) {
reRouteApp(localStorage.getItem('routePath')
}
});
Run Code Online (Sandbox Code Playgroud)
根据您选择的框架,您可能需要注意如何注册此事件侦听器。举个例子,如果您使用react
and hash-router
,您可以使用如下所示的重新路由方法:
const reRouteApp = (route) => {
window.location.hash = route;
}
Run Code Online (Sandbox Code Playgroud)
同时确保在 或 上注册侦听器事件,onComponentMount
以app.js
确保useEffect
最终不会有多个侦听器。
归档时间: |
|
查看次数: |
520 次 |
最近记录: |