如何在word api API中使用range.expand [英] How should range.expandTo be used in the word javascript api

查看:138
本文介绍了如何在word api API中使用range.expand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为单词创建了一个任务窗格插件,该单词可运行搜索并在两个搜索结果之间选择文本. 直到几天前,以下代码已成功运行:

I have created a taskpane addin for word that runs a search and will select the text between two search results. Until a couple of days ago the following code was running successfully:

function onExpandTestClick() {

        var textToFind = "Word",
            range;
        return Word.run(function(context) {

            var searchResults = context.document.body.search(textToFind, { matchWildCards: false });
            context.load(searchResults, "text");
            return context.sync()
                .then(function() {
                    range = searchResults.items[0].getRange("End");
                    var rangeEnd = searchResults.items[1].getRange("Start");
                    range.expandTo(rangeEnd);
                    context.load(range, 'text');
                    return context.sync();
                })
                .then(function() {
                    range.select();
                    return context.sync();
                });
        })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            }
        });
    }

但是现在会引发以下错误:

However now the following error is being thrown:

Error: {"name":"OfficeExtension.Error","code":"InvalidArgument","message":"InvalidArgument","traceMessages":[],"debugInfo":{"errorLocation":""},"stack":"InvalidArgument: InvalidArgument\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:19:183512)\n   at pi (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:19:198624)\n   at ht (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:19:198711)\n   at g (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:19:198531)\n   at l (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:19:197117)"}

我正在按照此处 https://github的建议使用PreviewCDN. com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec 并且正在运行Office版本16.0.7167.2040

I am using the PreviewCDN as recommended here https://github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec and am running office version 16.0.7167.2040

这是使用range.expandTo方法的正确方法吗?还是api中有更改?

Is this the correct way to use the range.expandTo method? Or has something changed in the api?

推荐答案

您在正确使用该方法的同时,尽管设计会稍有变化. ExpandTo的语义(如您在最新文档中所见)是,它不会修改调用范围,而是返回新扩展的范围.

you are using the method correctly although there will be a slight change in the design. The semantic of ExpandTo (as you can see on the latest documentation) is that it does NOT modify the calling range, but returns a newly expanded range.

此更改需要更新Office.js库,似乎Beta CDN目前存在问题,我们正在努力对其进行更新,以使其与当前公开可用的版本匹配.

this change requires an update to the Office.js library, it seems to be that there is an issue with the Beta CDN right now, we are working on updating it so that it matches the currently publicly available build.

所以在这一点上,我的建议是等待此修复程序.

So at this point my recommendation is to wait for this fix.

谢谢!

这篇关于如何在word api API中使用range.expand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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