向 Office 加载项发送请求

软时间

我正在使用JavaScript API for Excel制作 Excel 插件addin 的入口是addin.html,它调用了 JS、CSS 文件。所有文件都托管在www.myexample.com.

当我们点击插件中的一个按钮时,它会弹出一个浏览器窗口window.open("https://www.myexample.com/post/", "popup")带有post.htmlas 模板的页面angularjs构建ui-router服务器和客户端也托管在www.myexample.com.

的控制器post.html需要不时地向 发送请求addin.html例如,post.html发送一个地址A1addin.html,并期望接收单元的电流值A1使用JavaScript API for Excel 的一些异步函数可以很容易addin.html地从 Excel 获取单元格值而我的问题是如何在.post.html

如果我们使用$http.post,是否可以向addin.html没有服务器的发送请求

var getValue = function (address) {
    return $http.post('...', { address: address })
        .then(function (res) {
             // maybe do something with res
             return res
        }
}

如果我们使用postMessage,我们可以在 中实现监听器post.html,它们是反应式的。我无法想象如何主动发送请求并等待响应。

有人可以帮忙吗?

软时间

如果我们window.open按照 OP 中的描述打开弹出式浏览器,我们可以在弹出式窗口和加载项之间通过 发送数据postMessage

如果我们打开@MohamedShakeel 建议的对话框,我们可以通过messageParent发送数据localStorage

这两种方式都有效。

我无法想象如何主动发送请求并等待响应。

我们可以通过手动做出像这里这样的承诺来等待响应,例如来自加载项的消息因此,我们将能够定义

var getValue = function (address) {
    return sendRequestByPostMessage()
        .then(function () {
             return waitForResponse()
                 .then(function (res) {
                     // treatTheResponse
                 })
        })
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章