问题
We would like to use the Dialog API for some extra operations which are better suited to a larger work area. We already have the ability via the API to message the parent from the dialog (Office.context.ui.messageParent
), however we would also like the ability to directly message the dialog, like a sendMessage
function, for example:
var dialog;
Office.context.ui.displayDialogAsync('https://myDomain/myDialog.html', function (asyncResult) {
if (asyncResult.status === "failed") {
showNotification(asynceResult.error.code = ": " + asyncResult.error.message);
} else {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
dialog.sendMessage({ /* my payload */ }); //???
}
});
There are a couple of options to pass data from the parent to the child:
- query string:
displayDialogAsync(myUrl + "?myMessage=...", function...)
- localStorage (if on the same domain):
localStorage.setItem("DIALOG_MESSAGE", myMessage)
However, both of these can only really be used on initialization of the dialog, they do not provide a nice way to do asynchronous messaging.
回答1:
That is a popular request (and a good idea), but it is not supported yet. Please go to Office Dev User Voice and vote up the "Improve Custom Dialog" suggestion there or create a new suggestion of your own.
回答2:
You can use this in parrent before usedisplayDialog
:
localStorage.setItem("clientID", "15963ac5-314f-4d9b-b5a1-ccb2f1aea248");
And then in your JavaScript file:
var clientID = localStorage.getItem("clientID");
来源:https://stackoverflow.com/questions/42817868/office-dialog-api-send-message-to-child-dialog