Word.SearchOptions class

指定要包括在搜索操作中的选项。 若要详细了解如何在 Word JavaScript API 中使用搜索选项,请阅读使用搜索选项查找Word加载项中的文本。

扩展

注解

[ API 集:WordApi 1.1 ]

示例

// Search using a wildcard
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Queue a command to search the document with a wildcard
    // for any string of characters that starts with 'to' and ends with 'n'.
    const searchResults = context.document.body.search('to*n', {matchWildcards: true});

    // Queue a command to load the search results and get the font property values.
    searchResults.load('font');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    console.log('Found count: ' + searchResults.items.length);

    // Queue a set of commands to change the font for each found item.
    for (let i = 0; i < searchResults.items.length; i++) {
        searchResults.items[i].font.color = 'purple';
        searchResults.items[i].font.highlightColor = 'pink';
        searchResults.items[i].font.bold = true;
    }
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
}); 

属性

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

ignorePunct

如果提供,则指定是否忽略单词之间的所有标点符号。 默认值为 false。 对应于“查找和替换”对话框中_Ignore标点符号characters_ 检查框。

ignoreSpace

如果提供,则指定是否忽略单词之间的所有空格。 默认值为 false。 对应于“查找和替换”对话框中_Ignore空白characters_ 检查框。

matchCase

如果提供,则指定是否执行区分大小写的搜索。 默认值为 false。 对应于“查找和替换”对话框中的“_Match case_ 检查”框。

matchPrefix

如果提供,则指定是否匹配以搜索字符串开头的单词。 默认值为 false。 对应于“查找和替换”对话框中的“_Match prefix_ 检查”框。

matchSuffix

如果提供,则指定是否匹配以搜索字符串结尾的单词。 默认值为 false。 对应于“查找和替换”对话框中的“_Match suffix_ 检查”框。

matchWholeWord

如果提供,则指定是否仅查找整个单词,而不查找作为较大单词的一部分的文本。 默认值为 false。 对应于“查找和替换”对话框中_Find整个单词only_ 检查框。

matchWildcards

如果提供,则指定是否将使用特殊搜索运算符执行搜索。 默认值为 false。 对应于“查找和替换”对话框中的“_Use wildcards_ 检查”框。

方法

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

newObject(context)

创建 对象的新实例 Word.SearchOptions

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯对象,也可以传递同一类型的另一个 API 对象。

set(properties)

基于现有的已加载对象,同时对对象设置多个属性。

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,依次调用toJSON传递给它的 对象的 方法。) 虽然原始Word.SearchOptions对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.SearchOptionsData) ,其中包含从原始对象加载的任何子属性的浅表副本。

属性详细信息

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

ignorePunct

如果提供,则指定是否忽略单词之间的所有标点符号。 默认值为 false。 对应于“查找和替换”对话框中_Ignore标点符号characters_ 检查框。

ignorePunct: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

ignoreSpace

如果提供,则指定是否忽略单词之间的所有空格。 默认值为 false。 对应于“查找和替换”对话框中_Ignore空白characters_ 检查框。

ignoreSpace: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

matchCase

如果提供,则指定是否执行区分大小写的搜索。 默认值为 false。 对应于“查找和替换”对话框中的“_Match case_ 检查”框。

matchCase: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

matchPrefix

如果提供,则指定是否匹配以搜索字符串开头的单词。 默认值为 false。 对应于“查找和替换”对话框中的“_Match prefix_ 检查”框。

matchPrefix: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

matchSuffix

如果提供,则指定是否匹配以搜索字符串结尾的单词。 默认值为 false。 对应于“查找和替换”对话框中的“_Match suffix_ 检查”框。

matchSuffix: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

matchWholeWord

如果提供,则指定是否仅查找整个单词,而不查找作为较大单词的一部分的文本。 默认值为 false。 对应于“查找和替换”对话框中_Find整个单词only_ 检查框。

matchWholeWord: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

matchWildcards

如果提供,则指定是否将使用特殊搜索运算符执行搜索。 默认值为 false。 对应于“查找和替换”对话框中的“_Use wildcards_ 检查”框。

matchWildcards: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.1 ]

方法详细信息

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Word.Interfaces.SearchOptionsLoadOptions): Word.SearchOptions;

参数

options
Word.Interfaces.SearchOptionsLoadOptions

提供要加载对象的属性的选项。

返回

示例

// Ignore punctuation search
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Queue a command to search the document and ignore punctuation.
    const searchResults = context.document.body.search('video you', {ignorePunct: true});

    // Queue a command to load the search results and get the font property values.
    searchResults.load('font');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    console.log('Found count: ' + searchResults.items.length);

    // Queue a set of commands to change the font for each found item.
    for (let i = 0; i < searchResults.items.length; i++) {
        searchResults.items[i].font.color = 'purple';
        searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
        searchResults.items[i].font.bold = true;
    }
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
});  
// Search based on a prefix
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Queue a command to search the document based on a prefix.
    const searchResults = context.document.body.search('vid', {matchPrefix: true});

    // Queue a command to load the search results and get the font property values.
    searchResults.load('font');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();

    // Queue a set of commands to change the font for each found item.
    for (let i = 0; i < searchResults.items.length; i++) {
        searchResults.items[i].font.color = 'purple';
        searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
        searchResults.items[i].font.bold = true;
    }
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
}); 
// Search based on a suffix
// Run a batch operation against the Word object model.
await Word.run(async (context) => {

    // Queue a command to search the document for any string of characters after 'ly'.
    const searchResults = context.document.body.search('ly', {matchSuffix: true});

    // Queue a command to load the search results and get the font property values.
    searchResults.load('font');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    console.log('Found count: ' + searchResults.items.length);

    // Queue a set of commands to change the font for each found item.
    for (let i = 0; i < searchResults.items.length; i++) {
        searchResults.items[i].font.color = 'orange';
        searchResults.items[i].font.highlightColor = 'black';
        searchResults.items[i].font.bold = true;
    }
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
});  
// Search using a wildcard
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Queue a command to search the document with a wildcard
    // for any string of characters that starts with 'to' and ends with 'n'.
    const searchResults = context.document.body.search('to*n', {matchWildcards: true});

    // Queue a command to load the search results and get the font property values.
    searchResults.load('font');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    console.log('Found count: ' + searchResults.items.length);

    // Queue a set of commands to change the font for each found item.
    for (let i = 0; i < searchResults.items.length; i++) {
        searchResults.items[i].font.color = 'purple';
        searchResults.items[i].font.highlightColor = 'pink';
        searchResults.items[i].font.bold = true;
    }
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
}); 

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Word.SearchOptions;

参数

propertyNames

string | string[]

逗号分隔的字符串或指定要加载的属性的字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Word.SearchOptions;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是一个逗号分隔的字符串,指定要加载的属性,是 propertyNamesAndPaths.expand 一个逗号分隔的字符串,指定要加载的导航属性。

返回

newObject(context)

创建 对象的新实例 Word.SearchOptions

static newObject(context: OfficeExtension.ClientRequestContext): Word.SearchOptions;

参数

返回

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯对象,也可以传递同一类型的另一个 API 对象。

set(properties: Interfaces.SearchOptionsUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Word.Interfaces.SearchOptionsUpdateData

一个 JavaScript 对象,其属性按同构方式构造为调用方法的对象的属性。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止显示错误。

返回

void

set(properties)

基于现有的已加载对象,同时对对象设置多个属性。

set(properties: Word.SearchOptions): void;

参数

properties
Word.SearchOptions

返回

void

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,依次调用toJSON传递给它的 对象的 方法。) 虽然原始Word.SearchOptions对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.SearchOptionsData) ,其中包含从原始对象加载的任何子属性的浅表副本。

toJSON(): Word.Interfaces.SearchOptionsData;

返回