Does the Office.js API support multiple range selection?

强颜欢笑 提交于 2021-02-17 06:58:06

问题


I need to select multiple ranges simultaneously via the Office.js API like you can do in the MSWord UI by holding down the CTRL key and highlight multiple non-contiguous paragraphs, like the screenshot below:

This attempt doesn't work. Rather than highlighting the first two instances of the word "the" in the document, it's highlighting the first, then highlight the second afterwards:

   Word.run(function (context) {
       // Set up the search options.
       var options = Word.SearchOptions.newObject(context);
       options.matchCase = false;
       options.ignoreSpace = true;
       options.ignorePunct = true;
       options.matchWildcards = true;
       var searchText = "the";
       var searchResults = context.document.body.search(searchText, options);
       context.load(searchResults);
       return context.sync().then(function () {
           searchResults.items[0].select();
           searchResults.items[1].select();
       });
   });

回答1:


No, none of the APIs support multiple selections. Even the ability for the user to do so, using Ctrl+select is relatively new. The capability was never carried over to the APIs.

The closest the APIs can do is to highlight (or otherwise format) the Range objects of interest. There is such functionality in Word's dialog box which is also available to the COM APIs, but I don't find an equivalent for the JS APIs...




回答2:


To confirm what Cindy mentioned, Non-continuous selections are not only not supported in Office.js (for Word, we DO support them for Excel though) but also not supported manually on other platforms (i.e. Word Online).




回答3:


It might be possible. I ran across an odd result when using bindings and Office.context.document.goToByIdAsync(). Using this function you can navigate to any binding without having to call Word.Run(), which is nice. There is an option called SelectionMode, which by default does not select the binding, but can be set to select the contents of the binding. Weirdly, selecting the content in this way does not deselect the current selection! Which is not the result I wanted, fwiw; to me it is a nuisance that requires me to "deselect" any current selection before I use goToByIdAsync. But it's possible you could use this to select multiple ranges by wrapping them in contentControls and then creating bindings on them, then calling goToByIdAsync (with SelectionMode set to Select) on each binding. I have not tested this.

Edit Actually, the previous selection is deselected, but it remains highlighted as though it is still selected. This appears to be a display bug.



来源:https://stackoverflow.com/questions/57466511/does-the-office-js-api-support-multiple-range-selection

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