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;

返回