Office js API setDataAsync无法按照文档中的建议工作 [英] Office js API setDataAsync not working as suggested in Document

查看:109
本文介绍了Office js API setDataAsync无法按照文档中的建议工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在添加一个单词,其中有用例可在Binding中插入RTF格式.我的绑定类型是'Text',并且我也将coercionType设置为'Html',但它在绑定中没有替换.我的宿主应用程序是在线单词.但是,这在Word桌面应用程序中有效.但是我希望在线单词中使用它. 请提出建议. 谢谢

    /*
    *addBindingToData
    *@param userSelectedText -- is a string selected in word document
    */
    function addBindingToData(userSelectedText) {
        var bindVariableIdPrefix = userSelectedText + '_Binding';
        Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Text, {
            id: bindVariableIdPrefix + '__' + count
        }, function(asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                console.log('Action failed. Error: ' + asyncResult.error.message);
            } else {
                console.log('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
                getVarArrayOnSelection(asyncResult.value.id,userSelectedText, bindVariableIdPrefix).then(function(bindings) {
                    createInputboxes(bindings);
                });
            }
        });
        count++;
    }
Office.context.document.bindings.getByIdAsync(obj.id,function(asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                console.log('Action failed. Error: ' + asyncResult.error.message);
            } else {
                console.log(asyncResult.value.id+'got Id');
                var BindingId = asyncResult.value;
                BindingId.setDataAsync("<b>Hello</b> World!", {coercionType: "html"}, function (asyncResult) {
                    if (asyncResult.status == "failed") {
                        console.log('Error: ' + asyncResult.error.message);
                    }
                });
            }

        });

问题图片
一张驱动器图片

解决方案

经过进一步调查,我能够重现此问题并了解根本原因.

长话短说:当在文档中创建绑定时,实际上发生的事情是将新的内容控件添加到文档中.根据创建绑定时用户的选择,创建内容控件的类型,它可以是:

  1. BLOCK 内容控件:如果所选内容包含一个或多个完整段落,则会创建 BLOCK 内容控件.在这种情况下,HTML插入成功(这就是我所尝试的).

    1. INLINE 内容控件:另一方面,如果选择是部分选择(在一个段落中未完全选择它),则会创建一个 INLINE 内容控件.在这种情况下,HTML插入将失败.

好消息是,这是一个已知问题,我们正在努力在不久的将来实现这一目标.

我将原始答案留作参考. 谢谢! ================原始答案========================== >

我正在Word Online中尝试您的代码,但无法复制此问题.我可以看到HTML正确地打印在文档中.

有几个问题:

  1. 能否请您添加用于创建绑定的代码?
  2. 您是否还在OneDrive for Consumer或OneDrive for Business中尝试此操作.这对于进一步详细调查确实很有帮助.

谢谢! 胡安.

I am devloping a word add in which has usecase to insert Rich Text Format in Binding.My Binding Type is 'Text' and i have also set coercionType as 'Html' still it is not replacing in binding.My host application is word online.Whereas,this is working in word desktop application.But i want it in word online. Please suggest . Thanks

    /*
    *addBindingToData
    *@param userSelectedText -- is a string selected in word document
    */
    function addBindingToData(userSelectedText) {
        var bindVariableIdPrefix = userSelectedText + '_Binding';
        Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Text, {
            id: bindVariableIdPrefix + '__' + count
        }, function(asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                console.log('Action failed. Error: ' + asyncResult.error.message);
            } else {
                console.log('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
                getVarArrayOnSelection(asyncResult.value.id,userSelectedText, bindVariableIdPrefix).then(function(bindings) {
                    createInputboxes(bindings);
                });
            }
        });
        count++;
    }
Office.context.document.bindings.getByIdAsync(obj.id,function(asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                console.log('Action failed. Error: ' + asyncResult.error.message);
            } else {
                console.log(asyncResult.value.id+'got Id');
                var BindingId = asyncResult.value;
                BindingId.setDataAsync("<b>Hello</b> World!", {coercionType: "html"}, function (asyncResult) {
                    if (asyncResult.status == "failed") {
                        console.log('Error: ' + asyncResult.error.message);
                    }
                });
            }

        });

Picture of problem
One Drive Picture

解决方案

After further investigation I was able to repro this issue and know the root cause.

The long story short is this: when a binding is created in the document what effectively happens is that a new content control is added to the document. Depending on the selection of the user at the time the binding is created, a type of content control is created, it can be either:

  1. BLOCK content control: If the selection includes a complete paragraph or paragraphs a BLOCK content control is created. On this cases the HTML insertion is successful (which is what I was trying).

    1. INLINE content Control: On the other hand, If the selection is a partial selection (within a paragraph without fully selecting it) then an INLINE content control is created. In this case the HTML insertion fails.

The good news is that this is a known issue, we are working on enabling this in the near future.

I am leaving the original answer as reference. thanks! ================= ORIGINAL ANSWER ============================

I was trying your code in Word Online but I was not able to repro this issue. I can see the HTML correctly printed in the document.

Have a couple of questions:

  1. Can you please add the code you used to create the binding?
  2. Also are you trying this in OneDrive for consumer or OneDrive for Business. That will be really helpful to investigate in more detail.

Thanks! Juan.

这篇关于Office js API setDataAsync无法按照文档中的建议工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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