触发dialog.close后,Word加载项停止工作. Office JS [英] Word add-in stops working after dialog.close is fired. Office JS

查看:80
本文介绍了触发dialog.close后,Word加载项停止工作. Office JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Office365应用程序,在其中打开了对话框,并在执行某种活动后使用dialog.close()关闭了对话框.它工作得很好,但是功能区按钮停止工作,下次它将不再显示相同的对话框.

I have been working on an Office365 application where I opened a dialog and after some kind of activity when I close the dialog with dialog.close(). It works perfectly fine but ribbon button stops working and next time it will not show that same dialog again.

  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函数

and here is my processMessage function

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);
}
}

先谢谢您了:)

已更新

此问题仅在在线办公室中出现.

This issue only occurs in office online.

推荐答案

很抱歉,延迟调查此问题.长话短说,我必须对您的代码进行一些更改,以便它可以正常工作,它引发了一些异常(请参见下面的更改).我没有要插入的图像,因此我还假设您要发送给该方法的base64是有效图像.仅供参考,请更新4月21日发布的Office内部版本16.0.7967.2139,但这也适用于您引用的内部版本.

Sorry about the delay investigating this issue. Long story short, I had to do a couple of changes in your code so this can work, it was throwing a couple of exceptions (see changes below). I did not had the images you are inserting so I am also assuming the base64 you are sending to the method is a valid image. Also FYI please update Office build 16.0.7967.2139 was shipped Apr 21st, but this should also work on the build you cited.

这是我所做的更改:

  1. 我将这一行更改为仅获取属性:var messageFromDialog = arg.message; (为什么要解析JSON?)
  2. 我也很想这件事:如果(messageFromDialog ==="close"),我们没有messageType属性.(我们有类型) 顺便说一句,我的Dialog.html页面看起来像这样(我看不到您的页面,但我假设用户正在选择图像)
  1. I changed this line to only get the property: var messageFromDialog = arg.message; (why are you parsing JSON?)
  2. I also chagned this one : if (messageFromDialog === "close"), we dont have a messageType property.(we have type) BTW my Dialog.html page looks like this one ( I cannot see yours but i am assuming the user is selecting an image on it)

<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>

  1. 我还认为您不是在检查对话框是否已正确关闭,而是通过消息来完成操作,但是您需要订阅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);
    }
}

这篇关于触发dialog.close后,Word加载项停止工作. Office JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆