【问题标题】:extra line on insertHtml in contentControlcontentControl 中 insertHtml 上的额外行
【发布时间】:2016-02-06 00:57:24
【问题描述】:

在 Word 2016 的 office API 中,我使用 contentControl 的 insertHtml 函数插入 html 格式的文本。文本作为块元素插入,并在末尾添加了额外的行。 Word 2013 API 中的类似功能,带有 coercionType“html”的 setDataAsync 工作正常并插入了一个内联元素。有没有办法指定我需要一个内联元素。

当我尝试插入(text <i>inserted</i> with <b>insertHtml</b>) 时,基本上我需要的是:

这是现有文本(文本 insertedinsertHtml)后跟 更多文字

我得到的是:

这是现有的文本

(文本插入insertHtml

后面是更多的文字

谢谢。

【问题讨论】:

    标签: ms-office office-js word-addins


    【解决方案1】:

    这是 Word JavaScript API 上的一个已知问题,我们正在努力解决这个问题。同时,请使用带有 coercionType HTML 的 setSelectedDataAsync 来实现您所需要的。 insertHtml 将具有相同的行为。

    这里是一些代码示例,用于添加问题的一些上下文。基本上打算是获取当前选择并插入一些 HTML。 API 中有两种方法可以做到这一点(见下文)。现在 insertHtml 正在末尾添加一个段落标记,这是我们很快会修复的错误。

    function insertHtmlSample() {
        Word.run(function (context) {
            var mySelection = context.document.getSelection();
            mySelection.insertHtml("text <i>inserted</i> with <b>insertHtml</b>", "before");
            return  context.sync();
    
        }).catch(function (error) {
            app.showNotification(error.message);
        })
    }
    
    
    function InsertHtmlOld() {
        Office.context.document.setSelectedDataAsync("text <i>inserted</i> <p> with </p> <b>insert  <p>Html</b> </p> Hello!!!  ", { coercionType: 'html' });
    }
    

    【讨论】:

    • 谢谢你,这很好用。详细说明一下,由于我要插入 contentControl,所以我必须先调用 contentControl.select()。
    • 是的,另一种方法是您创建与该内容控件的绑定(我假设您正在创建 cc,对吗?)您可以为此使用 bindings.createFromNamedItemAsync () 方法(确保当您创建内容控件,提供标题,用于标识内容控件,然后可以使用binding.setData方法在CC上写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2017-10-03
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多