Word.ShapeCollection class

包含 Word 集合。形状对象。 目前仅支持以下形状:文本框、几何形状、组、图片和画布。

扩展

注解

API 集:WordApiDesktop 1.2

使用方

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/manage-shapes-text-boxes.yaml

await Word.run(async (context) => {
  // Gets text boxes in the main document.
  const shapes: Word.ShapeCollection = context.document.body.shapes;
  shapes.load();
  await context.sync();

  if (shapes.items.length > 0) {
    console.log(`Number of shapes found in the main document: ${shapes.items.length}`);
    shapes.items.forEach(function (shape, index) {
      if (shape.type === Word.ShapeType.textBox) {
        console.log(`Shape ${index} in the main document has a text box. Properties:`, shape);
      } else {
        console.log(`Shape ${index} in the main document doesn't have a text box.`);
      }
    });
  } else {
    console.log("No shapes found in the main document.");
  }
});

属性

context

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

items

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

方法

getByGeometricTypes(types)

获取具有指定几何类型的形状。 仅应用于几何形状。

getById(id)

按标识符获取形状。 如果此集合中没有具有标识符的形状,则引发 ItemNotFound 错误。

getByIdOrNullObject(id)

按标识符获取形状。 如果此集合中没有具有标识符的形状,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getByIds(ids)

按标识符获取形状。

getByNames(names)

获取具有指定名称的形状。

getByTypes(types)

获取具有指定类型的形状。

getFirst()

获取此集合中的第一个形状。 如果此集合为空,则引发 ItemNotFound 错误。

getFirstOrNullObject()

获取此集合中的第一个形状。 如果此集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

group()

对此集合中的浮动形状进行分组,将跳过内联形状。 返回 Shape 表示新形状组的对象。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.ShapeCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.ShapeCollectionData) ,其中包含一个“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.Shape[];

属性值

方法详细信息

getByGeometricTypes(types)

获取具有指定几何类型的形状。 仅应用于几何形状。

getByGeometricTypes(types: Word.GeometricShapeType[]): Word.ShapeCollection;

参数

types

Word.GeometricShapeType[]

几何形状子类型的数组。

返回

注解

API 集:WordApiDesktop 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/manage-geometric-shapes.yaml

await Word.run(async (context) => {
  // Gets the moon geometric shapes from the document body.
  const moons: Word.ShapeCollection = context.document.body.shapes.getByGeometricTypes([
    Word.GeometricShapeType.moon,
  ]);
  moons.load();
  await context.sync();

  console.log("Moons found in the document body:", moons);
});

getById(id)

按标识符获取形状。 如果此集合中没有具有标识符的形状,则引发 ItemNotFound 错误。

getById(id: number): Word.Shape;

参数

id

number

形状标识符。

返回

注解

API 集:WordApiDesktop 1.2

getByIdOrNullObject(id)

按标识符获取形状。 如果此集合中没有具有标识符的形状,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getByIdOrNullObject(id: number): Word.Shape;

参数

id

number

形状标识符。

返回

注解

API 集:WordApiDesktop 1.2

getByIds(ids)

按标识符获取形状。

getByIds(ids: number[]): Word.ShapeCollection;

参数

ids

number[]

形状标识符的数组。

返回

注解

API 集:WordApiDesktop 1.2

getByNames(names)

获取具有指定名称的形状。

getByNames(names: string[]): Word.ShapeCollection;

参数

names

string[]

形状名称的数组。

返回

注解

API 集:WordApiDesktop 1.2

getByTypes(types)

获取具有指定类型的形状。

getByTypes(types: Word.ShapeType[]): Word.ShapeCollection;

参数

types

Word.ShapeType[]

形状类型的数组。

返回

注解

API 集:WordApiDesktop 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/manage-shapes-text-boxes.yaml

await Word.run(async (context) => {
  // Sets the properties of the first text box.
  const firstShapeWithTextBox: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.textBox])
    .getFirst();
  firstShapeWithTextBox.top = 115;
  firstShapeWithTextBox.left = 0;
  firstShapeWithTextBox.width = 50;
  firstShapeWithTextBox.height = 50;
  await context.sync();

  console.log("The first text box's properties were updated:", firstShapeWithTextBox);
});

getFirst()

获取此集合中的第一个形状。 如果此集合为空,则引发 ItemNotFound 错误。

getFirst(): Word.Shape;

返回

注解

API 集:WordApiDesktop 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/manage-shapes-text-boxes.yaml

await Word.run(async (context) => {
  // Inserts a content control into the first paragraph in the first text box.
  const firstShapeWithTextBox: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.textBox])
    .getFirst();
  firstShapeWithTextBox.load("type/body");
  await context.sync();

  const firstParagraphInTextBox: Word.Paragraph = firstShapeWithTextBox.body.paragraphs.getFirst();
  const newControl: Word.ContentControl = firstParagraphInTextBox.insertContentControl();
  newControl.load();
  await context.sync();

  console.log("New content control properties:", newControl);
});

getFirstOrNullObject()

获取此集合中的第一个形状。 如果此集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getFirstOrNullObject(): Word.Shape;

返回

注解

API 集:WordApiDesktop 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/manage-shapes-text-boxes.yaml

await Word.run(async (context) => {
  // Gets text from the first text box in the main document.
  const shape: Word.Shape = context.document.body.shapes.getByTypes([Word.ShapeType.textBox]).getFirstOrNullObject();
  shape.load("body/text");
  await context.sync();

  console.log(
    shape.isNullObject
      ? "No shapes with text boxes found in the main document."
      : `Text in first text box: ${shape.body.text}`
  );
});

group()

对此集合中的浮动形状进行分组,将跳过内联形状。 返回 Shape 表示新形状组的对象。

group(): Word.Shape;

返回

注解

API 集:WordApiDesktop 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/45-shapes/group-ungroup.yaml

await Word.run(async (context) => {
  // Groups the shapes (including text boxes and pictures) found in the document body.
  const shapes: Word.ShapeCollection = context.document.body.shapes.getByTypes([
    Word.ShapeType.geometricShape,
    Word.ShapeType.textBox,
    Word.ShapeType.picture,
  ]);
  shapes.load("items");
  await context.sync();

  const numShapes = shapes.items.length;
  if (numShapes === 0) {
    console.log("No shapes found in the document body.");
    return;
  }

  console.log(`Number of shapes to group: ${numShapes}`);

  const groupedShape: Word.Shape = shapes.group();
  groupedShape.load("shapeGroup/shapes");
  await context.sync();

  const shapeGroup: Word.ShapeGroup = groupedShape.shapeGroup;
  console.log("Shapes grouped:", shapeGroup.shapes);
  groupedShape.select();
});

load(options)

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

load(options?: Word.Interfaces.ShapeCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.ShapeCollection;

参数

options

Word.Interfaces.ShapeCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

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

返回

toJSON()

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

toJSON(): Word.Interfaces.ShapeCollectionData;

返回

track()

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

track(): Word.ShapeCollection;

返回

untrack()

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

untrack(): Word.ShapeCollection;

返回