Word.StyleCollection class

包含 Word 集合。样式对象。

扩展

注解

API 集:WordApi 1.5

使用方

示例

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

// Gets the number of available styles stored with the document.
await Word.run(async (context) => {
  const styles: Word.StyleCollection = context.document.getStyles();
  const count = styles.getCount();
  await context.sync();

  console.log(`Number of styles: ${count.value}`);
});

属性

context

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

items

获取此集合中已加载的子项。

方法

getByName(name)

按对象名称获取 Style 对象。

getByNameOrNullObject(name)

如果相应的样式不存在,则此方法返回其 isNullObject 属性设置为 true的对象。

getCount()

返回集合中的项目数。

getItem(index)

按对象在集合中的索引获取 Style 对象。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.StyleCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.StyleCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

track()

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

untrack()

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

属性详细信息

context

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

context: RequestContext;

属性值

items

获取此集合中已加载的子项。

readonly items: Word.Style[];

属性值

方法详细信息

getByName(name)

按对象名称获取 Style 对象。

getByName(name: string): Word.Style;

参数

name

string

样式名称。

返回

注解

API 集:WordApi 1.5

getByNameOrNullObject(name)

如果相应的样式不存在,则此方法返回其 isNullObject 属性设置为 true的对象。

getByNameOrNullObject(name: string): Word.Style;

参数

name

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

// Adds a new style.
await Word.run(async (context) => {
  const newStyleName = (document.getElementById("new-style-name") as HTMLInputElement).value;
  if (newStyleName == "") {
    console.warn("Enter a style name to add.");
    return;
  }

  const style: Word.Style = context.document.getStyles().getByNameOrNullObject(newStyleName);
  style.load();
  await context.sync();

  if (!style.isNullObject) {
    console.warn(
      `There's an existing style with the same name '${newStyleName}'! Please provide another style name.`
    );
    return;
  }

  const newStyleType = ((document.getElementById("new-style-type") as HTMLSelectElement).value as unknown) as Word.StyleType;
  context.document.addStyle(newStyleName, newStyleType);
  await context.sync();

  console.log(newStyleName + " has been added to the style list.");
});

getCount()

返回集合中的项目数。

getCount(): OfficeExtension.ClientResult<number>;

返回

注解

API 集:WordApi 1.5

示例

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

// Gets the number of available styles stored with the document.
await Word.run(async (context) => {
  const styles: Word.StyleCollection = context.document.getStyles();
  const count = styles.getCount();
  await context.sync();

  console.log(`Number of styles: ${count.value}`);
});

getItem(index)

按对象在集合中的索引获取 Style 对象。

getItem(index: number): Word.Style;

参数

index

number

对象的位置 Style

返回

注解

API 集:WordApi 1.5

load(options)

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

load(options?: Word.Interfaces.StyleCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.StyleCollection;

参数

options

Word.Interfaces.StyleCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Word.StyleCollection;

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

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

返回

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.StyleCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.StyleCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

toJSON(): Word.Interfaces.StyleCollectionData;

返回

track()

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

track(): Word.StyleCollection;

返回

untrack()

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

untrack(): Word.StyleCollection;

返回