locked
setSelectedDataAsync methods specification? RRS feed

  • Question

  • Hi,

    I tried to insert HTML using setSelectedDataAsync methods,
    the following code is OK,

    Office.context.document.setSelectedDataAsync(
        '<span style="color:red;">abcde</span>',
        {coercionType:Office.CoercionType.Html}
    );

    but following is NG(not raise an error).

    Office.context.document.setSelectedDataAsync(
        '<span style="color:red;">あいうえお</span>',
        {coercionType:Office.CoercionType.Html}
    );

    Can not insert non-English Is it specification?


    Thanks,

    kinuasa
    Saturday, June 22, 2013 1:46 PM

Answers

  • Hi Kinuasa!

    very good question. Word do not support that character set directly when using the HTML coercionType, this is something we need to fix.  The good news is that  you have a couple of options.

     

    1. Write your style first, then write the plain text.

    basically you need to write 2 times to the document. the first one to setup the style you need (using the html coercionType), and the second one with the actual text you want to write (using the text coercionType).

     

    here is an example of how you can accomplish this:

    note that first I am writing a ";" character with the style I need, then since after writing to the document the selection expands whatever text was inserted, then you can send a second setDataAsync in the callback of the first operation with the characters you want to write (あいうえお  on this case), that will replace the '';" with the ";" with the characters you need keeping the first call style. (note that by default the coercionType is 'text' so I don't need to specify this on the second call)

     function setData() {
        html = "<span style='color:red;'>; </span>";
        Office.context.document.setSelectedDataAsync(html, { coercionType: 'html' }, function (result) {
            if (result.status == "succeeded")
                Office.context.document.setSelectedDataAsync("あいうえお");
                ;
        });
    }

     

     

    2. Send the UTF character set directly on the HTML. if you use the getSelectedDataAsync as HTML with those character sets, you will see that word is actually getting the literal UTF character codes for the selection. If you write what you get from the getSelectedData method you'll see if works. the downside of this approach is that you need to do a conversion to the codes before writing so my advice is to use the first option.

    Hope this helps!

    -Juan.

    Monday, August 5, 2013 6:13 PM