3

嗨,下面是我试图在单元格中将一些 html 显示为普通文本的代码,但奇怪的是,所有内容都显示为项目符号,如果我将文本放在表格中,它会显示项目符号,但如果我将其移出而不是项目符号显示出来,当我调试代码时,我可以看到项目符号在那里但没有显示在文档上。

  function addNewTable() {
    var fruitsUnderfilled = [["<b><i>Apple</i></b>", "red", "", ""], ["Banana", "yellow", "long", "mushy"], ["Pear", "green", "oblong", ""]];

    var document = Office.context.document;
    document.setSelectedDataAsync(fruitsUnderfilled, function (result) {
        console.log(result);

        document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
            console.log(result);

            var binding = result.value;
            binding.addHandlerAsync(Office.EventType.BindingSelectionChanged, onBindingSelectionChanged);
        });
    });
}

var onBindingSelectionChanged = function (results) {
    Word.run(function (context) {
        if (!isExceuted) {

            isExceuted = true;
            var tableCell = context.document.getSelection().parentTableCell;
            context.load(tableCell);

            return context.sync()
                .then(function () {
                    if (tableCell.isNull == true) {
                        //selection is not within a cell..... 
                        console.log("selection not in a header");
                    }
                    else {
                        // the selection is inside a cell! lets get the content....
                        var body = tableCell.body;

                        var html = tableCell.body.getHtml();

                        var tableHtml = tableCell.body.getHtml();

                        context.sync()
                           .then(function () {
                               var cellHtml = html.value;
                               $("#resultDiv").html(cellHtml);
                               $("#resultDiv table").remove();
                               //$("#resultDiv p.MsoNormal").html("<table><tr><td><ul><li>yellow</li></ul></td></tr></table>");
                               $("#resultDiv p.MsoNormal").html("<ul><li>yellow</li></ul>");
                               var value = "<html>" + $("#resultDiv").html() + "</body>";
                               // Option 1
                               body.insertHtml(value, Word.InsertLocation.replace);

                               // Option 2
                               //body.clear();
                               //body.insertHtml(newHtml, Word.InsertLocation.end);

                               return context.sync().then(function () {
                                   console.log('HTML was successfully replaced.');
                                   return;
                               }).catch(function (e) {
                                   console.log(e.message);
                               });
                           }).catch(function (e) {
                               console.log(e.message);
                           });

                    }
                }).catch(function (e) {
                    console.log(e.message);
                });
        }
        else {
            isExceuted = false;
        }

    });
};
4

0 回答 0