Office.js Office.context.ui.messageParent 在 Excel 中不工作
Office.js Office.context.ui.messageParent not working in Excel
我们有一个托管在 Azure 中的 excel 加载项,其中 Office.context.ui.messageParent API 消息未 sent/received 在桌面上。没有错误,消息只是没有被事件侦听器捕获。
我们使用 MFA 的对话框,我们让它在桌面和 Web 本地工作,但是当我们部署到 Azure 中托管的阶段加载项时,这个问题只发生在桌面上。
这个有效:
- 对话框url (https://localhost:3000/dist/callback.html)
- 任务窗格 url (https://localhost:3000/dist/taskpane.html)
这不是:
- 对话 url (https://ip-dev-custom-functions.azurewebsites.net/addin/callback.html)
- 任务窗格 url (https://ip-dev-custom-functions.azurewebsites.net/addin/taskpane.html)
令人惊讶的是,正在触发 DialogEventReceived 而不是消息 DialogMessageReceived。
callback.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Cache-Control" content="private, no-cache, no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<title></title>
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>
</head>
<body>
<script>
if (window.opener)
window.opener.postMessage({type: 'interstitial', url: document.location.href}, "*");
Office.initialize = function (reason) {
console.log("Sending auth complete message through dialog: " + document.location.href);
Office.context.ui.messageParent(document.location.href);
}
</script>
</body>
</html>
taskpane.html 的片段事件侦听器在哪里:
Office.context.ui.displayDialogAsync(url, {
height: dim.height,
width: dim.width,
promptBeforeOpen: false
}, async (result) => {
_loginDialog = result.value;
_loginDialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
_loginDialog.addEventHandler(Office.EventType.DialogEventReceived, (ev) => {
console.log("## EVENT RECEIVED ##", ev);
});
});
function processMessage(arg) {
console.log("Message received in processMessage: " + JSON.stringify(arg));
_lastCallback(arg.message);
_loginDialog?.close();
_loginDialog = null;
}
基于Authenticate and authorize with the Office dialog API,正确的流程是:
加载项应在加载项的域中启动对话框中的页面,然后重定向到登录页面,然后再次重定向到与对话框中第一个页面具有相同域的另一个页面。
否则,messageParent API 将不起作用,因为它只信任 displayDialogAsync() API 中使用的页面域。在您的场景中,登录页面(在对话框中首次启动)和回调页面位于不同的域中,这导致了问题。
谢谢。
我们有一个托管在 Azure 中的 excel 加载项,其中 Office.context.ui.messageParent API 消息未 sent/received 在桌面上。没有错误,消息只是没有被事件侦听器捕获。
我们使用 MFA 的对话框,我们让它在桌面和 Web 本地工作,但是当我们部署到 Azure 中托管的阶段加载项时,这个问题只发生在桌面上。
这个有效:
- 对话框url (https://localhost:3000/dist/callback.html)
- 任务窗格 url (https://localhost:3000/dist/taskpane.html)
这不是:
- 对话 url (https://ip-dev-custom-functions.azurewebsites.net/addin/callback.html)
- 任务窗格 url (https://ip-dev-custom-functions.azurewebsites.net/addin/taskpane.html)
令人惊讶的是,正在触发 DialogEventReceived 而不是消息 DialogMessageReceived。
callback.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Cache-Control" content="private, no-cache, no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<title></title>
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>
</head>
<body>
<script>
if (window.opener)
window.opener.postMessage({type: 'interstitial', url: document.location.href}, "*");
Office.initialize = function (reason) {
console.log("Sending auth complete message through dialog: " + document.location.href);
Office.context.ui.messageParent(document.location.href);
}
</script>
</body>
</html>
taskpane.html 的片段事件侦听器在哪里:
Office.context.ui.displayDialogAsync(url, {
height: dim.height,
width: dim.width,
promptBeforeOpen: false
}, async (result) => {
_loginDialog = result.value;
_loginDialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
_loginDialog.addEventHandler(Office.EventType.DialogEventReceived, (ev) => {
console.log("## EVENT RECEIVED ##", ev);
});
});
function processMessage(arg) {
console.log("Message received in processMessage: " + JSON.stringify(arg));
_lastCallback(arg.message);
_loginDialog?.close();
_loginDialog = null;
}
基于Authenticate and authorize with the Office dialog API,正确的流程是:
加载项应在加载项的域中启动对话框中的页面,然后重定向到登录页面,然后再次重定向到与对话框中第一个页面具有相同域的另一个页面。
否则,messageParent API 将不起作用,因为它只信任 displayDialogAsync() API 中使用的页面域。在您的场景中,登录页面(在对话框中首次启动)和回调页面位于不同的域中,这导致了问题。
谢谢。