How to operate on selected paragraphs from a JavaS

2019-06-25 05:23发布

I'm trying to set the styles of all selected paragraphs from a JavaScript addin. The code works perfectly in Word Online, but I get an error when I use it in Word Desktop. The code is as follows (yes, my test style is called "Banana", and it's big and yellow):

function updateParStyle(event) {
    Word.run(function (context) {
        var range = context.document.getSelection();
        range.load("paragraphs/items");
        return context.sync().then(function () {
            var items = range.paragraphs.items;
            // console.log(items.length + " items");
            for (var i = 0; i < items.length; i++) {
                items[i].style = "Banana";
            }
            return context.sync();
        });
    }).catch(function (e) {
        console.error(e);
        return window.open("https://urldecode.org/?text=" + JSON.stringify(e));
    });
    event.completed();
}

In desktop Word I get the following error:

{
    "name":"OfficeExtension.Error",
    "code":"ItemNotFound",
    "message":"ItemNotFound",
    "traceMessages":[],
    "debugInfo":{
        "errorLocation":"ParagraphCollection.getItem"
    },
    "stack":"ItemNotFound: ItemNotFound\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8103:6)\n   at lib$es6$promise$$internal$$tryCatch (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8974:8)\n   at lib$es6$promise$$internal$$invokeCallback (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8984:8)\n   at lib$es6$promise$$internal$$publish (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8960:9)\n   at lib$es6$promise$asap$$flush (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8779:8)"
}

Debugging has revealed that the error occurs in the context.sync after setting the paragraph styles. As can be seen in the error message I am using the unminified office.js for debugging, but the error also happens with the default office.js, just with a less helpful stacktrace. If I set range.style = "Banana" instead of working with paragraphs that does work on both Word Online and Word Desktop. The "Banana" style is a linked style (so it should work for both paragraphs and characters).

I get the exact same error when replacing items[i].style = "Banana" with items[i].delete() or items[i].insertText("Hello world", "After"), so the problem is not related to the style itself.

A possible workaround that I've found is that I can set a paragraph-style on a selected range, and it will work as expected (set the style of all selected paragraphs, even those that are partially selected), but I imagine I'll have to work with a ParagraphCollection at some point, so I'd still like to know what I'm doing wrong.

I have tested with Word versions 16.0.7341.2035 and 16.0.7167.2060.

标签: office-js
1条回答
beautiful°
2楼-- · 2019-06-25 05:44

Interesting. I am not sure if I would code it like that. May I suggest you to modify your code to use the paragraph collection appropriately? I think your code will be greatly simplified if you do this:

Word.run(function(context) {
     var pars = context.document.getSelection().paragraphs;
       pars.load();
        return context.sync().then(function () {
            for (var i = 0; i < pars.items.length; i++) {
                pars.items[i].style = "Banana";
            }
           
    return context.sync();
        })
}).catch(function(error) {
    console.log(error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

this code certainly works on all platforms. thx Juan.

查看更多
登录 后发表回答