我一直在开发一个 Office365 应用程序,在该应用程序中我打开了一个对话框,并在进行某种活动后使用dialog.close() 关闭对话框。它工作得很好,但功能区按钮停止工作,下次它不会再显示相同的对话框。
Office.context.ui.displayDialogAsync("https://" + location.host + "/Dialog.html", { width: 90, height: 90, requireHTTPS: true }, function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
return;
}
});
这是我的 processMessage 函数
function processMessage(arg) {
try{
var messageFromDialog = JSON.parse(arg.message);
var base64 = messageFromDialog.image.split(",")[1];
Word.run(function (context) {
var body = context.document.getSelection();
body.insertInlinePictureFromBase64(base64, Word.InsertLocation.replace);
return context.sync();
}).catch(function (error) {
app.showNotification("Error: " + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
app.showNotification("Debug info: " + JSON.stringify(error.debugInfo));
}
});
if (messageFromDialog.messageType === "dialogClosed") {
dialog.close();
}
} catch (ex) {
console.log("Exception " + ex);
}
}
提前致谢 :)
Updated
此问题仅出现在office online中。
很抱歉延迟调查此问题。长话短说,我必须对代码进行一些更改,这样才能工作,它抛出了一些异常(请参阅下面的更改)。我没有您要插入的图像,因此我还假设您发送到该方法的 base64 是有效图像。另外仅供参考,请更新 Office 版本 16.0.7967.2139 已于 4 月 21 日发布,但这也应该适用于您引用的版本。
以下是我所做的更改:
- 我更改了这一行以仅获取属性: var messageFromDialog = arg.message; (为什么要解析 JSON?)
- 我也改变了这一点:if (messageFromDialog === "close"),我们没有 messageType 属性。(我们有类型)
顺便说一句,我的 Dialog.html 页面看起来像这样(我看不到你的页面,但我假设用户正在选择上面的图像)
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.1.min.js"></script>
<!-- For the Office UI Fabric, go to http://aka.ms/office-ui-fabric to learn more. -->
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.min.css">
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css">
<script>
Office.initialize = function () {
$('#button1').click(one);
$('#button2').click(two);
};
function one()
{
Office.context.ui.messageParent("Picked 1");
}
function two() {
Office.context.ui.messageParent("close");
}
</script>
</head>
<body>
<p class="ms-font-xxl ms-fontColor-neutralSecondary ms-fontWeight-semilight">Pick a number</p>
<button class="ms-Button ms-Button--primary" id="button1">
<span class="ms-Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span>
<span class="ms-Button-label" id="button1-text">1</span>
<span class="ms-Button-description" id="button1-desc">Number 1</span>
</button>
<button class="ms-Button ms-Button--primary" id="button2">
<span class="ms-Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span>
<span class="ms-Button-label" id="button2-text">2</span>
<span class="ms-Button-description" id="button2-desc">Number 2</span>
</button>
</body>
</html>
- 我还认为您没有检查对话框是否正确关闭,而是通过消息来执行此操作,但是您需要订阅 DialogEventReceived 事件以了解用户是否关闭了对话框(而不是通过消息)。
function showDialog() {
var url = "https://" + location.host + "/Dialog.html";
Office.context.ui.displayDialogAsync(url, { width: 10, height: 10, requireHTTPS: true }, function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
app.showDialog("OK");
return;
}
else {
app.showDialog("whats up");
}
});
}
function processMessage(arg) {
try {
var messageFromDialog = arg.message;
var base64 = messageFromDialog.image.split(",")[1];
Word.run(function (context) {
var body = context.document.getSelection();
body.insertInlinePictureFromBase64(base64, Word.InsertLocation.replace);
return context.sync();
}).catch(function (error) {
app.showNotification("Error: " + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
app.showNotification("Debug info: " + JSON.stringify(error.debugInfo));
}
});
if (messageFromDialog === "close") {
dialog.close();
}
} catch (ex) {
console.log("Exception " + ex);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)