How should range.expandTo be used in the word javascript api

梦想与她 提交于 2019-12-08 03:36:54

问题


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

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

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


回答1:


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.

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.

thanks!



来源:https://stackoverflow.com/questions/39154604/how-should-range-expandto-be-used-in-the-word-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!