我目前正在编写使用Excel Javascript API的Excel加载项。此加载项将两个按钮添加到Excel的“主页”选项卡。这两个按钮都打开相同的任务窗格,并且此代码的代码>
<Control xsi:type="Button" id="ShareProjectButton"> ... <Action xsi:type="ShowTaskpane"> <TaskpaneId>ButtonId1</TaskpaneId> <SourceLocation resid="Taskpane.Side.Url"/> </Action> <Enabled>true</Enabled> </Control> <Control xsi:type="Button" id="TaskpaneButton"> ... <Action xsi:type="ShowTaskpane"> <TaskpaneId>ButtonId1</TaskpaneId> <SourceLocation resid="Taskpane.Main.Url"/> </Action> <Enabled>true</Enabled> </Control>
此外,我正在从React Router提供定义为
Taskpane.Main.Url
(定义为https://localhost:3000/#/main
)和Taskpane.Side.Url
(定义为https://localhost:3000/#/side
)的URL。此代码看起来像:
import React from "react"; import { HashRouter as Router, Switch, Route } from "react-router-dom"; class App extends React.Component { constructor(props) { super(props); this.state = { count: 0 } this.inc = this.inc.bind(this); } inc() { console.log("Incing"); this.setState(state => {return {count: state.count + 1};}); } render() { return ( <Router> <div> <Switch> <Route exact path="/main" render={(props) => <Main {...props} count={this.state.count} inc={this.inc} />} /> <Route exact path="/side" render={(props) => <Side {...props} count={this.state.count} inc={this.inc} />} /> </Switch> </div> </Router> ); } } const Main = (props) => {return (<div><button onClick={props.inc}>Inc</button><p> Main {props.count} </p></div>);}; const Side = (props) => {return (<div><button onClick={props.inc}>Inc</button><p> Side {props.count} </p></div>);}; export default App;
当我单击按钮时,路线将正确呈现。但是,如果我单击加载主路径的按钮,增加计数,然后单击另一个按钮以加载支路,则计数重置为0。
是否有可能在Excel Javascript环境中正常共享全局状态并使用React Router?如果不是,用共享状态的多个页面构建复杂的任务窗格的最佳方法是什么?谢谢!
我目前正在编写使用Excel Javascript API的Excel加载项。此加载项将两个按钮添加到Excel的“主页”选项卡。两个按钮都打开相同的任务窗格,并且此<...>
是,与COM加载项不同,Office加载项不涉及在用户设备或Office客户端中运行的代码。对于Office加载项,宿主应用程序(例如Excel)读取加载项清单,并在UI中挂接加载项的自定义功能区按钮和菜单命令。在需要时,它将加载加载项的JavaScript和HTML代码,这些代码在沙箱中的浏览器上下文中执行。
因此,我认为尽管它是相同的任务窗格,但清单中有2个不同的URL,这可以认为您的加载项与2个任务窗格一起运行,并且任务窗格在单独的JavaScript运行时环境中运行。