如何通过桌面Word中的JavaScript插件处理选定的段落? [英] How to operate on selected paragraphs from a JavaScript addin in desktop word?

查看:102
本文介绍了如何通过桌面Word中的JavaScript插件处理选定的段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JavaScript插件设置所有选定段落的样式.该代码在Word Online中可以完美运行,但是在Word Desktop中使用它时出现错误.代码如下(是的,我的测试样式称为香蕉",又大又黄):

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();
}

在桌面Word中,出现以下错误:

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)"
}

调试发现,设置段落样式后,context.sync中会发生错误.从错误消息中可以看出,我正在使用未缩小的office.js进行调试,但是默认的office.js也会发生此错误,只是堆栈跟踪的帮助较小.如果我设置range.style = "Banana"而不是使用在Word Online和Word Desktop上都可以使用的段落. 香蕉"样式是一种链接样式(因此,它对于段落和字符均适用).

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).

items[i].style = "Banana"替换为items[i].delete()items[i].insertText("Hello world", "After")时,我得到完全相同的错误,因此问题与样式本身无关.

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.

我发现一种可能的解决方法是,我可以在选定范围内设置段落样式,并且可以按预期工作(设置所有选定段落的样式,甚至是部分选定的段落的样式),但是我想象在某个时候我必须使用ParagraphCollection,所以我仍然想知道我做错了什么.

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.

我已经用Word版本16.0.7341.2035和16.0.7167.2060进行了测试.

I have tested with Word versions 16.0.7341.2035 and 16.0.7167.2060.

推荐答案

有趣.我不确定是否要这样编码.我可以建议您修改代码以适当地使用段落集合吗?我认为,如果您这样做的话,您的代码将会大大简化:

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.

这篇关于如何通过桌面Word中的JavaScript插件处理选定的段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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