向 Office 加载项发送请求
Send a request to an Office add-in
我正在用 JavaScript API for Excel 制作一个 Excel 插件。插件入口为addin.html
,调用JS,CSS个文件。所有文件都托管在 www.myexample.com
.
当我们点击插件中的一个按钮时,它会弹出一个浏览器 window by window.open("https://www.myexample.com/post/", "popup")
。以post.html
为模板的页面是由angularjs
和ui-router
建立的。服务器和客户端也托管在 www.myexample.com
.
post.html
的控制器需要不时向addin.html
发送请求。例如,post.html
发送一个地址A1
给addin.html
,期望收到Cell的当前值A1
。 addin.html
通过 JavaScript API 对于 Excel 使用一些异步函数 从 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
中实现监听器,它们是反应式的。我无法想象如何主动发送请求并等待响应。
有人能帮忙吗?
您可以创建一个 DispalyDialog 并打开它,而不是在我们单击插件中的按钮时打开 window。从那里您可以使用 messageParent()
方法向插件发送请求。为了处理收到的消息,你可以做类似
的事情
Office.context.document.addHandlerAsync(Office.EventType.DialogMessageReceived, myHandler);
function myHandler(eventArgs) {
//Do some processing
}
处理完成后,您可以将结果存储在 localStorage
中,以便在 DisplayDialog 中可用。
这不是完成您需要的明确方法,但我相信它会完成任务。如果您发现任何其他解决方案,请在此处更新。
如果我们按照 OP 中的描述通过 window.open
打开弹出窗口浏览器,我们可以通过 postMessage
在弹出窗口和加载项之间发送数据。
如果我们打开@MohamedShakeel 建议的对话框,我们可以通过 messageParent
和 localStorage
发送数据。
这两种方式都有效。
I can NOT imagine how to send actively a request and wait for a
response.
我们可以通过像 这样手动做出承诺来等待响应,例如来自加载项的消息。因此,我们将能够定义
var getValue = function (address) {
return sendRequestByPostMessage()
.then(function () {
return waitForResponse()
.then(function (res) {
// treatTheResponse
})
})
}
我正在用 JavaScript API for Excel 制作一个 Excel 插件。插件入口为addin.html
,调用JS,CSS个文件。所有文件都托管在 www.myexample.com
.
当我们点击插件中的一个按钮时,它会弹出一个浏览器 window by window.open("https://www.myexample.com/post/", "popup")
。以post.html
为模板的页面是由angularjs
和ui-router
建立的。服务器和客户端也托管在 www.myexample.com
.
post.html
的控制器需要不时向addin.html
发送请求。例如,post.html
发送一个地址A1
给addin.html
,期望收到Cell的当前值A1
。 addin.html
通过 JavaScript API 对于 Excel 使用一些异步函数 从 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
中实现监听器,它们是反应式的。我无法想象如何主动发送请求并等待响应。
有人能帮忙吗?
您可以创建一个 DispalyDialog 并打开它,而不是在我们单击插件中的按钮时打开 window。从那里您可以使用 messageParent()
方法向插件发送请求。为了处理收到的消息,你可以做类似
Office.context.document.addHandlerAsync(Office.EventType.DialogMessageReceived, myHandler);
function myHandler(eventArgs) {
//Do some processing
}
处理完成后,您可以将结果存储在 localStorage
中,以便在 DisplayDialog 中可用。
这不是完成您需要的明确方法,但我相信它会完成任务。如果您发现任何其他解决方案,请在此处更新。
如果我们按照 OP 中的描述通过 window.open
打开弹出窗口浏览器,我们可以通过 postMessage
在弹出窗口和加载项之间发送数据。
如果我们打开@MohamedShakeel 建议的对话框,我们可以通过 messageParent
和 localStorage
发送数据。
这两种方式都有效。
I can NOT imagine how to send actively a request and wait for a response.
我们可以通过像
var getValue = function (address) {
return sendRequestByPostMessage()
.then(function () {
return waitForResponse()
.then(function (res) {
// treatTheResponse
})
})
}