Word.Style class

表示Word文档中的样式。

Extends

注解

[ API 集:WordApi 1.3 ]

属性

baseStyle

指定要用作另一个样式的基本格式的现有样式的名称。

borders

指定一个 BorderCollection 对象,该对象代表指定样式的所有边框。

builtIn

获取指定的样式是否为内置样式。

context

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

description

获取指定样式的说明。

font

获取表示指定样式的字符格式的字体对象。

inUse

获取指定的样式是已在文档中修改或应用的内置样式,还是已在文档中创建的新样式。

linked

获取样式是否为可用于段落和字符格式的链接样式。

listTemplate

获取一个 ListTemplate 对象,该对象代表指定 Style 对象的列表格式。

nameLocal

获取以用户语言表示的样式的名称。

nextParagraphStyle

指定要自动应用于新段落的样式名称,该段落在具有指定样式格式的段落之后插入。

paragraphFormat

获取一个 ParagraphFormat 对象,该对象代表指定样式的段落设置。

priority

指定优先级。

quickStyle

指定样式是否对应于可用的快速样式。

shading

获取一个底纹对象,该对象代表指定样式的底纹。 不适用于列表样式。

tableStyle

获取一个 TableStyle 对象,该对象表示可应用于表的 Style 属性。

type

获取样式类型。

unhideWhenUsed

指定在文档中使用指定样式后,是否在 Microsoft Word的“样式”和“样式”任务窗格中将其显示为推荐样式。

visibility

指定指定样式是否在“样式”库和“样式”任务窗格中显示为推荐样式。

方法

delete()

删除样式。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

set(properties, options)

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

set(properties)

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

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来又调用toJSON传递给它的 对象的 方法。) 而原始Word。Style 对象是一个 API 对象,toJSON该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.StyleData) ,其中包含原始对象中加载的任何子属性的浅表副本。

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果跨 .sync 调用和“.run”批处理的顺序执行外部使用此对象,并在设置属性或调用对象方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将该对象添加到跟踪的对象集合。 如果此对象是集合的一部分,则还应跟踪父集合。

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 在内存发布生效之前,需要调用 context.sync()

属性详细信息

baseStyle

指定要用作另一个样式的基本格式的现有样式的名称。

baseStyle: string;

属性值

string

注解

[ API 集:WordApi 1.5 ]

注意:WordApi 1.6 中引入了设置 baseStyle 功能。

borders

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

指定一个 BorderCollection 对象,该对象代表指定样式的所有边框。

readonly borders: Word.BorderCollection;

属性值

注解

[ API 集:WordApi BETA (仅预览版) ]

builtIn

获取指定的样式是否为内置样式。

readonly builtIn: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

context

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

context: RequestContext;

属性值

description

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

获取指定样式的说明。

readonly description: string;

属性值

string

注解

[ API 集:WordApi BETA (仅预览版) ]

font

获取表示指定样式的字符格式的字体对象。

readonly font: Word.Font;

属性值

注解

[ API 集:WordApi 1.5 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Updates font properties (e.g., color, size) of the specified style.
await Word.run(async (context) => {
  const styleName = $("#style-name").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to update font properties.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    const font = style.font;
    font.color = "#FF0000";
    font.size = 20;
    console.log(`Successfully updated font properties of the '${styleName}' style.`);
  }
});

inUse

获取指定的样式是已在文档中修改或应用的内置样式,还是已在文档中创建的新样式。

readonly inUse: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

linked

获取样式是否为可用于段落和字符格式的链接样式。

readonly linked: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

listTemplate

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

获取一个 ListTemplate 对象,该对象代表指定 Style 对象的列表格式。

readonly listTemplate: Word.ListTemplate;

属性值

注解

[ API 集:WordApi BETA (仅预览版) ]

nameLocal

获取以用户语言表示的样式的名称。

readonly nameLocal: string;

属性值

string

注解

[ API 集:WordApi 1.5 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Applies the specified style to a paragraph.
await Word.run(async (context) => {
  const styleName = $("#style-name-to-use").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to apply.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else if (style.type != Word.StyleType.paragraph) {
    console.log(`The '${styleName}' style isn't a paragraph style.`);
  } else {
    const body = context.document.body;
    body.clear();
    body.insertParagraph(
      "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.",
      "Start"
    );
    const paragraph = body.paragraphs.getFirst();
    paragraph.style = style.nameLocal;
    console.log(`'${styleName}' style applied to first paragraph.`);
  }
});

nextParagraphStyle

指定要自动应用于新段落的样式名称,该段落在具有指定样式格式的段落之后插入。

nextParagraphStyle: string;

属性值

string

注解

[ API 集:WordApi 1.5 ]

注意:WordApi 1.6 中引入了设置 nextParagraphStyle 功能。

paragraphFormat

获取一个 ParagraphFormat 对象,该对象代表指定样式的段落设置。

readonly paragraphFormat: Word.ParagraphFormat;

属性值

注解

[ API 集:WordApi 1.5 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Sets certain aspects of the specified style's paragraph format e.g., the left indent size and the alignment.
await Word.run(async (context) => {
  const styleName = $("#style-name").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to update its paragraph format.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    style.paragraphFormat.leftIndent = 30;
    style.paragraphFormat.alignment = Word.Alignment.centered;
    console.log(`Successfully the paragraph format of the '${styleName}' style.`);
  }
});

priority

指定优先级。

priority: number;

属性值

number

注解

[ API 集:WordApi 1.5 ]

quickStyle

指定样式是否对应于可用的快速样式。

quickStyle: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

shading

获取一个底纹对象,该对象代表指定样式的底纹。 不适用于列表样式。

readonly shading: Word.Shading;

属性值

注解

[ API 集:WordApi 1.6 ]

tableStyle

获取一个 TableStyle 对象,该对象表示可应用于表的 Style 属性。

readonly tableStyle: Word.TableStyle;

属性值

注解

[ API 集:WordApi 1.6 ]

type

获取样式类型。

readonly type: Word.StyleType | "Character" | "List" | "Paragraph" | "Table";

属性值

Word.StyleType | "Character" | "List" | "Paragraph" | "Table"

注解

[ API 集:WordApi 1.5 ]

unhideWhenUsed

指定在文档中使用指定样式后,是否在 Microsoft Word的“样式”和“样式”任务窗格中将其显示为推荐样式。

unhideWhenUsed: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

visibility

指定指定样式是否在“样式”库和“样式”任务窗格中显示为推荐样式。

visibility: boolean;

属性值

boolean

注解

[ API 集:WordApi 1.5 ]

方法详细信息

delete()

删除样式。

delete(): void;

返回

void

注解

[ API 集:WordApi 1.5 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Deletes the custom style.
await Word.run(async (context) => {
  const styleName = $("#style-name-to-delete").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to delete.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    style.delete();
    console.log(`Successfully deleted custom style '${styleName}'.`);
  }
});

load(options)

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

load(options?: Word.Interfaces.StyleLoadOptions): Word.Style;

参数

options
Word.Interfaces.StyleLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

set(properties, options)

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

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

参数

properties
Word.Interfaces.StyleUpdateData

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

options
OfficeExtension.UpdateOptions

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

返回

void

set(properties)

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

set(properties: Word.Style): void;

参数

properties
Word.Style

返回

void

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来又调用toJSON传递给它的 对象的 方法。) 而原始Word。Style 对象是一个 API 对象,toJSON该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.StyleData) ,其中包含原始对象中加载的任何子属性的浅表副本。

toJSON(): Word.Interfaces.StyleData;

返回

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果跨 .sync 调用和“.run”批处理的顺序执行外部使用此对象,并在设置属性或调用对象方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将该对象添加到跟踪的对象集合。 如果此对象是集合的一部分,则还应跟踪父集合。

track(): Word.Style;

返回

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 在内存发布生效之前,需要调用 context.sync()

untrack(): Word.Style;

返回