word - 在不永久更改文档格式的情况下突出显示搜索结果 [英] word - highlight search results without permanently changing document formatting

查看:45
本文介绍了word - 在不永久更改文档格式的情况下突出显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想突出显示搜索结果,类似于词的本机搜索正在执行的操作.换句话说,我不希望搜索操作在文档中产生副作用,例如通过更改返回的文本范围中的字体颜色.

I would like to highlight the search results similarly to what word's native search is doing. In other words, I don't want the search action to have side effects in the document, e.g. by changing the color of the font in the returned text ranges.

var searchResults = paragraph.search(searchValue);
context.load(searchResults, { select: 'text, font, style' });

推荐答案

现在实现方案的唯一方法是遍历搜索结果集合并更改每个范围的突出显示颜色,就像我在下面的片段中显示的那样.要撤消此操作,您需要再次搜索并将高光恢复为白色.

right now the only way you can achieve your scenario is by traversing the search results collection and change the highlight color for each range like I am showing on the snippet below. To undo this operation you need to do the search again and restore the highlights to white.

Word.run(function (context) {

            // Queue a command to search the document
            var searchResults = context.document.body.search('string you want to search for');
            context.load(searchResults, 'font');
            return context.sync().then(function () {
                console.log('Found count: ' + searchResults.items.length);

                // Queue a set of commands to change the font for each found item.
                for (var i = 0; i < searchResults.items.length; i++) {
                   
                    searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
                   
                }

                return context.sync();
            });
        })
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

这篇关于word - 在不永久更改文档格式的情况下突出显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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