Word.ShapeCollection class

Word のコレクションが含まれています。オブジェクトをシェイプします。 現時点では、テキスト ボックス、幾何学的図形、グループ、画像、キャンバスの図形のみがサポートされています。

Extends

注釈

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()

API オブジェクトが JSON.stringify() に渡されるときに、より有用な出力を提供するために、JavaScript toJSON() メソッドをオーバーライドします。 (次に、JSON.stringify渡されたオブジェクトの toJSON メソッドを呼び出します)。元の Word.ShapeCollection オブジェクトが API オブジェクトであるのに対し、 toJSON メソッドは、コレクションの項目から読み込まれたプロパティの浅いコピーを含む "items" 配列を含むプレーンな JavaScript オブジェクト ( Word.Interfaces.ShapeCollectionData として型指定された) を返します。

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()

API オブジェクトが JSON.stringify() に渡されるときに、より有用な出力を提供するために、JavaScript toJSON() メソッドをオーバーライドします。 (次に、JSON.stringify渡されたオブジェクトの toJSON メソッドを呼び出します)。元の Word.ShapeCollection オブジェクトが API オブジェクトであるのに対し、 toJSON メソッドは、コレクションの項目から読み込まれたプロパティの浅いコピーを含む "items" 配列を含むプレーンな JavaScript オブジェクト ( Word.Interfaces.ShapeCollectionData として型指定された) を返します。

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;

返品