Word.Shape class

表示页眉、页脚或文档正文中的形状。 目前,仅支持以下形状:文本框、几何形状、组、图片和画布。

扩展

注解

[ 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);
});

属性

allowOverlap

指定给定的形状是否可以与其他形状重叠。

altTextDescription

指定表示与形状关联的可选文本的字符串。

body

表示 Body 形状的对象。 仅适用于文本框和几何形状。

canvas

获取与形状关联的画布。 如果形状类型不是canvas ,则会返回属性isNullObject设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

context

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

fill

返回形状的填充格式。

geometricShapeType

形状的几何形状类型。 如果 不是几何形状,则它将为 null。

height

形状的高度(以磅为单位)。

heightRelative

形状高度与垂直相对大小的百分比,请参阅 Word。RelativeSize。 对于内联形状或子形状,无法设置它。

id

获取表示形状标识符的整数。

isChild

检查此形状是组形状还是画布形状的子形状。

left

从形状左侧到水平相对位置的距离(以磅为单位),请参阅Word。RelativeHorizontalPosition。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。

leftRelative

相对左侧位置(从形状左侧到水平相对位置的百分比),请参阅 Word。RelativeHorizontalPosition。 对于内联形状或子形状,它将返回 0,并且无法设置。

lockAspectRatio

指定是否锁定此形状的纵横比。

name

形状的名称。

parentCanvas

获取此子形状的顶级父画布形状。 如果不是画布的子形状,则它将为 null。

parentGroup

获取此子形状的顶级父组形状。 如果不是组的子形状,则它将为 null。

relativeHorizontalPosition

形状的相对水平位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

relativeHorizontalSize

形状的相对水平大小。 对于内联形状或子形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

relativeVerticalPosition

形状的相对垂直位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeVerticalPosition

relativeVerticalSize

形状的相对垂直大小。 对于内联形状或子形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

rotation

指定形状的旋转(以度为单位)。 不适用于画布形状。

shapeGroup

获取与形状关联的形状组。 如果形状类型不是groupShape ,则会返回属性isNullObject设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

textFrame

获取 TextFrame 形状的 对象。

textWrap

返回形状的文本换行格式。

top

从形状的上边缘到垂直相对位置的距离(以磅为单位), (查看Word。RelativeVerticalPosition) 。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。

topRelative

相对顶部位置(从形状的上边缘到垂直相对位置的百分比),请参阅 Word。RelativeVerticalPosition。 对于内联形状或子形状,它将返回 0,并且无法设置。

type

获取形状类型。 目前,仅支持以下形状:文本框、几何形状、组、图片和画布。

visible

指定形状是否可见。 不适用于内联形状。

width

形状的宽度(以磅为单位)。

widthRelative

形状宽度与水平相对大小的百分比,请参阅 Word。RelativeSize。 对于内联形状或子形状,无法设置它。

方法

delete()

删除形状及其内容。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

moveHorizontally(distance)

按磅数水平移动形状。

moveVertically(distance)

按磅数垂直移动形状。

scaleHeight(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleHeight(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleWidth(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleWidth(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

select(selectMultipleShapes)

选择形状。

set(properties, options)

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

set(properties)

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

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,依次调用toJSON传递给它的 对象的 方法。) 虽然原始Word.Shape对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.ShapeData) ,其中包含从原始对象加载的任何子属性的浅表副本。

track()

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

untrack()

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

属性详细信息

allowOverlap

指定给定的形状是否可以与其他形状重叠。

allowOverlap: boolean;

属性值

boolean

注解

[ API 集:WordApiDesktop 1.2 ]

altTextDescription

指定表示与形状关联的可选文本的字符串。

altTextDescription: string;

属性值

string

注解

[ API 集:WordApiDesktop 1.2 ]

body

表示 Body 形状的对象。 仅适用于文本框和几何形状。

readonly body: Word.Body;

属性值

注解

[ 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);
});

canvas

获取与形状关联的画布。 如果形状类型不是canvas ,则会返回属性isNullObject设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

readonly canvas: Word.Canvas;

属性值

注解

[ API 集:WordApiDesktop 1.2 ]

示例

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

await Word.run(async (context) => {
  // Inserts a canvas in the document.
  const canvasShape: Word.Shape = context.document.getSelection().insertCanvas();
  canvasShape.load();
  await context.sync();

  canvasShape.select();
  console.log("Inserted canvas:", canvasShape);

  const canvas: Word.Canvas = canvasShape.canvas;
  canvas.load("shape,shapes");
  await context.sync();

  console.log("Canvas object:", canvas);
});

context

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

context: RequestContext;

属性值

fill

返回形状的填充格式。

readonly fill: Word.ShapeFill;

属性值

注解

[ 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 color fill properties of the first moon found in the document body.
  const moon: Word.Shape = context.document.body.shapes
    .getByGeometricTypes([Word.GeometricShapeType.moon])
    .getFirstOrNullObject();
  moon.load("fill");
  await context.sync();

  if (moon.isNullObject) {
    console.log("No moons found in the document body.");
    return;
  }

  const moonFill: Word.ShapeFill = moon.fill;
  const moonFillType = moonFill.type as Word.ShapeFillType;

  console.log("Color fill properties of the first moon found in the document body:");
  console.log(`\tForeground color: ${moonFill.foregroundColor}`);
  console.log(`\tBackground color: ${moonFill.backgroundColor}`);
  console.log(`\tTransparency: ${moonFill.transparency}`);
  console.log(`\tFill type: ${moonFillType}`);
});

geometricShapeType

形状的几何形状类型。 如果 不是几何形状,则它将为 null。

geometricShapeType: Word.GeometricShapeType | "LineInverse" | "Triangle" | "RightTriangle" | "Rectangle" | "Diamond" | "Parallelogram" | "Trapezoid" | "NonIsoscelesTrapezoid" | "Pentagon" | "Hexagon" | "Heptagon" | "Octagon" | "Decagon" | "Dodecagon" | "Star4" | "Star5" | "Star6" | "Star7" | "Star8" | "Star10" | "Star12" | "Star16" | "Star24" | "Star32" | "RoundRectangle" | "Round1Rectangle" | "Round2SameRectangle" | "Round2DiagonalRectangle" | "SnipRoundRectangle" | "Snip1Rectangle" | "Snip2SameRectangle" | "Snip2DiagonalRectangle" | "Plaque" | "Ellipse" | "Teardrop" | "HomePlate" | "Chevron" | "PieWedge" | "Pie" | "BlockArc" | "Donut" | "NoSmoking" | "RightArrow" | "LeftArrow" | "UpArrow" | "DownArrow" | "StripedRightArrow" | "NotchedRightArrow" | "BentUpArrow" | "LeftRightArrow" | "UpDownArrow" | "LeftUpArrow" | "LeftRightUpArrow" | "QuadArrow" | "LeftArrowCallout" | "RightArrowCallout" | "UpArrowCallout" | "DownArrowCallout" | "LeftRightArrowCallout" | "UpDownArrowCallout" | "QuadArrowCallout" | "BentArrow" | "UturnArrow" | "CircularArrow" | "LeftCircularArrow" | "LeftRightCircularArrow" | "CurvedRightArrow" | "CurvedLeftArrow" | "CurvedUpArrow" | "CurvedDownArrow" | "SwooshArrow" | "Cube" | "Can" | "LightningBolt" | "Heart" | "Sun" | "Moon" | "SmileyFace" | "IrregularSeal1" | "IrregularSeal2" | "FoldedCorner" | "Bevel" | "Frame" | "HalfFrame" | "Corner" | "DiagonalStripe" | "Chord" | "Arc" | "LeftBracket" | "RightBracket" | "LeftBrace" | "RightBrace" | "BracketPair" | "BracePair" | "Callout1" | "Callout2" | "Callout3" | "AccentCallout1" | "AccentCallout2" | "AccentCallout3" | "BorderCallout1" | "BorderCallout2" | "BorderCallout3" | "AccentBorderCallout1" | "AccentBorderCallout2" | "AccentBorderCallout3" | "WedgeRectCallout" | "WedgeRRectCallout" | "WedgeEllipseCallout" | "CloudCallout" | "Cloud" | "Ribbon" | "Ribbon2" | "EllipseRibbon" | "EllipseRibbon2" | "LeftRightRibbon" | "VerticalScroll" | "HorizontalScroll" | "Wave" | "DoubleWave" | "Plus" | "FlowChartProcess" | "FlowChartDecision" | "FlowChartInputOutput" | "FlowChartPredefinedProcess" | "FlowChartInternalStorage" | "FlowChartDocument" | "FlowChartMultidocument" | "FlowChartTerminator" | "FlowChartPreparation" | "FlowChartManualInput" | "FlowChartManualOperation" | "FlowChartConnector" | "FlowChartPunchedCard" | "FlowChartPunchedTape" | "FlowChartSummingJunction" | "FlowChartOr" | "FlowChartCollate" | "FlowChartSort" | "FlowChartExtract" | "FlowChartMerge" | "FlowChartOfflineStorage" | "FlowChartOnlineStorage" | "FlowChartMagneticTape" | "FlowChartMagneticDisk" | "FlowChartMagneticDrum" | "FlowChartDisplay" | "FlowChartDelay" | "FlowChartAlternateProcess" | "FlowChartOffpageConnector" | "ActionButtonBlank" | "ActionButtonHome" | "ActionButtonHelp" | "ActionButtonInformation" | "ActionButtonForwardNext" | "ActionButtonBackPrevious" | "ActionButtonEnd" | "ActionButtonBeginning" | "ActionButtonReturn" | "ActionButtonDocument" | "ActionButtonSound" | "ActionButtonMovie" | "Gear6" | "Gear9" | "Funnel" | "MathPlus" | "MathMinus" | "MathMultiply" | "MathDivide" | "MathEqual" | "MathNotEqual" | "CornerTabs" | "SquareTabs" | "PlaqueTabs" | "ChartX" | "ChartStar" | "ChartPlus";

属性值

Word.GeometricShapeType | "LineInverse" | "Triangle" | "RightTriangle" | "Rectangle" | "Diamond" | "Parallelogram" | "Trapezoid" | "NonIsoscelesTrapezoid" | "Pentagon" | "Hexagon" | "Heptagon" | "Octagon" | "Decagon" | "Dodecagon" | "Star4" | "Star5" | "Star6" | "Star7" | "Star8" | "Star10" | "Star12" | "Star16" | "Star24" | "Star32" | "RoundRectangle" | "Round1Rectangle" | "Round2SameRectangle" | "Round2DiagonalRectangle" | "SnipRoundRectangle" | "Snip1Rectangle" | "Snip2SameRectangle" | "Snip2DiagonalRectangle" | "Plaque" | "Ellipse" | "Teardrop" | "HomePlate" | "Chevron" | "PieWedge" | "Pie" | "BlockArc" | "Donut" | "NoSmoking" | "RightArrow" | "LeftArrow" | "UpArrow" | "DownArrow" | "StripedRightArrow" | "NotchedRightArrow" | "BentUpArrow" | "LeftRightArrow" | "UpDownArrow" | "LeftUpArrow" | "LeftRightUpArrow" | "QuadArrow" | "LeftArrowCallout" | "RightArrowCallout" | "UpArrowCallout" | "DownArrowCallout" | "LeftRightArrowCallout" | "UpDownArrowCallout" | "QuadArrowCallout" | "BentArrow" | "UturnArrow" | "CircularArrow" | "LeftCircularArrow" | "LeftRightCircularArrow" | "CurvedRightArrow" | "CurvedLeftArrow" | "CurvedUpArrow" | "CurvedDownArrow" | "SwooshArrow" | "Cube" | "Can" | "LightningBolt" | "Heart" | "Sun" | "Moon" | "SmileyFace" | "IrregularSeal1" | "IrregularSeal2" | "FoldedCorner" | "Bevel" | "Frame" | "HalfFrame" | "Corner" | "DiagonalStripe" | "Chord" | "Arc" | "LeftBracket" | "RightBracket" | "LeftBrace" | "RightBrace" | "BracketPair" | "BracePair" | "Callout1" | "Callout2" | "Callout3" | "AccentCallout1" | "AccentCallout2" | "AccentCallout3" | "BorderCallout1" | "BorderCallout2" | "BorderCallout3" | "AccentBorderCallout1" | "AccentBorderCallout2" | "AccentBorderCallout3" | "WedgeRectCallout" | "WedgeRRectCallout" | "WedgeEllipseCallout" | "CloudCallout" | "Cloud" | "Ribbon" | "Ribbon2" | "EllipseRibbon" | "EllipseRibbon2" | "LeftRightRibbon" | "VerticalScroll" | "HorizontalScroll" | "Wave" | "DoubleWave" | "Plus" | "FlowChartProcess" | "FlowChartDecision" | "FlowChartInputOutput" | "FlowChartPredefinedProcess" | "FlowChartInternalStorage" | "FlowChartDocument" | "FlowChartMultidocument" | "FlowChartTerminator" | "FlowChartPreparation" | "FlowChartManualInput" | "FlowChartManualOperation" | "FlowChartConnector" | "FlowChartPunchedCard" | "FlowChartPunchedTape" | "FlowChartSummingJunction" | "FlowChartOr" | "FlowChartCollate" | "FlowChartSort" | "FlowChartExtract" | "FlowChartMerge" | "FlowChartOfflineStorage" | "FlowChartOnlineStorage" | "FlowChartMagneticTape" | "FlowChartMagneticDisk" | "FlowChartMagneticDrum" | "FlowChartDisplay" | "FlowChartDelay" | "FlowChartAlternateProcess" | "FlowChartOffpageConnector" | "ActionButtonBlank" | "ActionButtonHome" | "ActionButtonHelp" | "ActionButtonInformation" | "ActionButtonForwardNext" | "ActionButtonBackPrevious" | "ActionButtonEnd" | "ActionButtonBeginning" | "ActionButtonReturn" | "ActionButtonDocument" | "ActionButtonSound" | "ActionButtonMovie" | "Gear6" | "Gear9" | "Funnel" | "MathPlus" | "MathMinus" | "MathMultiply" | "MathDivide" | "MathEqual" | "MathNotEqual" | "CornerTabs" | "SquareTabs" | "PlaqueTabs" | "ChartX" | "ChartStar" | "ChartPlus"

注解

[ 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 first geometric shape found in the document body.
  const geometricShape: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.geometricShape])
    .getFirstOrNullObject();
  geometricShape.load();
  await context.sync();

  if (geometricShape.isNullObject) {
    console.log("No geometric shapes found in the document body.");
    return;
  }

  console.log(
    `First geometric shape found in the document body is of type ${geometricShape.geometricShapeType}:`,
    geometricShape,
  );
});

height

形状的高度(以磅为单位)。

height: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

heightRelative

形状高度与垂直相对大小的百分比,请参阅 Word。RelativeSize。 对于内联形状或子形状,无法设置它。

heightRelative: number;

属性值

number

注解

[ 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) => {
  // Changes the relative size of the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log(
    "About to change the relative size of the first shape group found in the document body:",
    shapeGroup.shapes,
  );
  firstShapeGroup.relativeHorizontalSize = Word.RelativeSize.insideMargin;
  firstShapeGroup.relativeVerticalSize = Word.RelativeSize.bottomMargin;
  firstShapeGroup.heightRelative = 50;

  console.log("Changed the relative size of the first shape group.");
});

id

获取表示形状标识符的整数。

readonly id: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

isChild

检查此形状是组形状还是画布形状的子形状。

readonly isChild: boolean;

属性值

boolean

注解

[ API 集:WordApiDesktop 1.2 ]

left

从形状左侧到水平相对位置的距离(以磅为单位),请参阅Word。RelativeHorizontalPosition。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。

left: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

leftRelative

相对左侧位置(从形状左侧到水平相对位置的百分比),请参阅 Word。RelativeHorizontalPosition。 对于内联形状或子形状,它将返回 0,并且无法设置。

leftRelative: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

lockAspectRatio

指定是否锁定此形状的纵横比。

lockAspectRatio: boolean;

属性值

boolean

注解

[ API 集:WordApiDesktop 1.2 ]

name

形状的名称。

name: string;

属性值

string

注解

[ API 集:WordApiDesktop 1.2 ]

parentCanvas

获取此子形状的顶级父画布形状。 如果不是画布的子形状,则它将为 null。

readonly parentCanvas: Word.Shape;

属性值

注解

[ API 集:WordApiDesktop 1.2 ]

parentGroup

获取此子形状的顶级父组形状。 如果不是组的子形状,则它将为 null。

readonly parentGroup: Word.Shape;

属性值

注解

[ API 集:WordApiDesktop 1.2 ]

relativeHorizontalPosition

形状的相对水平位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

relativeHorizontalPosition: Word.RelativeHorizontalPosition | "Margin" | "Page" | "Column" | "Character" | "LeftMargin" | "RightMargin" | "InsideMargin" | "OutsideMargin";

属性值

Word.RelativeHorizontalPosition | "Margin" | "Page" | "Column" | "Character" | "LeftMargin" | "RightMargin" | "InsideMargin" | "OutsideMargin"

注解

[ 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) => {
  // Changes the position of the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to change the position of the first shape group found in document body:", shapeGroup.shapes);
  firstShapeGroup.relativeVerticalPosition = Word.RelativeVerticalPosition.insideMargin;
  firstShapeGroup.relativeHorizontalPosition = Word.RelativeHorizontalPosition.margin;

  console.log("Changed the position of the first shape group.");
});

relativeHorizontalSize

形状的相对水平大小。 对于内联形状或子形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

relativeHorizontalSize: Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin";

属性值

Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin"

注解

[ 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) => {
  // Changes the relative size of the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log(
    "About to change the relative size of the first shape group found in the document body:",
    shapeGroup.shapes,
  );
  firstShapeGroup.relativeHorizontalSize = Word.RelativeSize.insideMargin;
  firstShapeGroup.relativeVerticalSize = Word.RelativeSize.bottomMargin;
  firstShapeGroup.heightRelative = 50;

  console.log("Changed the relative size of the first shape group.");
});

relativeVerticalPosition

形状的相对垂直位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeVerticalPosition

relativeVerticalPosition: Word.RelativeVerticalPosition | "Margin" | "Page" | "Paragraph" | "Line" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin";

属性值

Word.RelativeVerticalPosition | "Margin" | "Page" | "Paragraph" | "Line" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin"

注解

[ 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) => {
  // Changes the position of the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to change the position of the first shape group found in document body:", shapeGroup.shapes);
  firstShapeGroup.relativeVerticalPosition = Word.RelativeVerticalPosition.insideMargin;
  firstShapeGroup.relativeHorizontalPosition = Word.RelativeHorizontalPosition.margin;

  console.log("Changed the position of the first shape group.");
});

relativeVerticalSize

形状的相对垂直大小。 对于内联形状或子形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition

relativeVerticalSize: Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin";

属性值

Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin"

注解

[ 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) => {
  // Changes the relative size of the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log(
    "About to change the relative size of the first shape group found in the document body:",
    shapeGroup.shapes,
  );
  firstShapeGroup.relativeHorizontalSize = Word.RelativeSize.insideMargin;
  firstShapeGroup.relativeVerticalSize = Word.RelativeSize.bottomMargin;
  firstShapeGroup.heightRelative = 50;

  console.log("Changed the relative size of the first shape group.");
});

rotation

指定形状的旋转(以度为单位)。 不适用于画布形状。

rotation: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

shapeGroup

获取与形状关联的形状组。 如果形状类型不是groupShape ,则会返回属性isNullObject设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

readonly shapeGroup: Word.ShapeGroup;

属性值

注解

[ 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();
});

textFrame

获取 TextFrame 形状的 对象。

readonly textFrame: Word.TextFrame;

属性值

注解

[ 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 the text frame of the first text box in the main document.
  const shape: Word.Shape = context.document.body.shapes.getByTypes([Word.ShapeType.textBox]).getFirstOrNullObject();
  shape.load("textFrame");
  await context.sync();

  if (shape.isNullObject) {
    console.log("No shapes with text boxes found in the main document.");
    return;
  }

  console.log("Text frame of first text box:", shape.textFrame);
});

textWrap

返回形状的文本换行格式。

readonly textWrap: Word.ShapeTextWrap;

属性值

注解

[ 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 wrap properties of the first text box in the main document.
  const shape: Word.Shape = context.document.body.shapes.getByTypes([Word.ShapeType.textBox]).getFirstOrNullObject();
  shape.load("textWrap");
  await context.sync();

  if (shape.isNullObject) {
    console.log("No shapes with text boxes found in the main document.");
    return;
  }

  console.log("Text wrap properties of first text box:", shape.textWrap);
});

top

从形状的上边缘到垂直相对位置的距离(以磅为单位), (查看Word。RelativeVerticalPosition) 。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。

top: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

topRelative

相对顶部位置(从形状的上边缘到垂直相对位置的百分比),请参阅 Word。RelativeVerticalPosition。 对于内联形状或子形状,它将返回 0,并且无法设置。

topRelative: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

type

获取形状类型。 目前,仅支持以下形状:文本框、几何形状、组、图片和画布。

readonly type: Word.ShapeType | "Unsupported" | "TextBox" | "GeometricShape" | "Group" | "Picture" | "Canvas";

属性值

Word.ShapeType | "Unsupported" | "TextBox" | "GeometricShape" | "Group" | "Picture" | "Canvas"

注解

[ 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.");
  }
});

visible

指定形状是否可见。 不适用于内联形状。

visible: boolean;

属性值

boolean

注解

[ API 集:WordApiDesktop 1.2 ]

width

形状的宽度(以磅为单位)。

width: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

widthRelative

形状宽度与水平相对大小的百分比,请参阅 Word。RelativeSize。 对于内联形状或子形状,无法设置它。

widthRelative: number;

属性值

number

注解

[ API 集:WordApiDesktop 1.2 ]

方法详细信息

delete()

删除形状及其内容。

delete(): void;

返回

void

注解

[ 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) => {
  // Deletes the first text box.
  context.document.body.shapes.getByTypes([Word.ShapeType.textBox]).getFirst().delete();
  await context.sync();

  console.log("The first text box in document was deleted.");
});

load(options)

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

load(options?: Word.Interfaces.ShapeLoadOptions): Word.Shape;

参数

options
Word.Interfaces.ShapeLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths

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

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

返回

moveHorizontally(distance)

按磅数水平移动形状。

moveHorizontally(distance: number): void;

参数

distance

number

指定形状水平移动的距离,以磅为单位。 使用正值向右移动形状。 使用负值向左移动形状。

返回

void

注解

[ 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) => {
  // Moves the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to move the first shape group found in the document body:", shapeGroup.shapes);
  firstShapeGroup.moveHorizontally(-10);
  firstShapeGroup.moveVertically(50);

  console.log("Moved the first shape group.");
});

moveVertically(distance)

按磅数垂直移动形状。

moveVertically(distance: number): void;

参数

distance

number

以磅为单位指定形状的垂直移动距离。 使用正值向上移动形状。 使用负值将形状向下移动。

返回

void

注解

[ 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) => {
  // Moves the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to move the first shape group found in the document body:", shapeGroup.shapes);
  firstShapeGroup.moveHorizontally(-10);
  firstShapeGroup.moveVertically(50);

  console.log("Moved the first shape group.");
});

scaleHeight(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleHeight(scaleFactor: number, scaleType: Word.ShapeScaleType, scaleFrom?: Word.ShapeScaleFrom): void;

参数

scaleFactor

number

指定形状调整后的高度与当前或原始高度的比例。

scaleType
Word.ShapeScaleType

指定是相对于其原始大小还是当前大小缩放形状。 原始大小缩放选项仅适用于图像。

scaleFrom
Word.ShapeScaleFrom

可选。 指定在缩放形状时,该形状的哪一部分将保持在原有的位置。 如果省略,则表示形状的左上角保留其位置。

返回

void

注解

[ 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) => {
  // Scales the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to scale the first shape group found in the document body:", shapeGroup.shapes);
  firstShapeGroup.scaleHeight(0.75, Word.ShapeScaleType.currentSize);
  firstShapeGroup.scaleWidth(0.5, Word.ShapeScaleType.currentSize, Word.ShapeScaleFrom.scaleFromBottomRight);

  console.log("Scaled the first shape group.");
});

scaleHeight(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleHeight(scaleFactor: number, scaleType: "CurrentSize" | "OriginalSize", scaleFrom?: "ScaleFromTopLeft" | "ScaleFromMiddle" | "ScaleFromBottomRight"): void;

参数

scaleFactor

number

指定形状调整后的高度与当前或原始高度的比例。

scaleType

"CurrentSize" | "OriginalSize"

指定是相对于其原始大小还是当前大小缩放形状。 原始大小缩放选项仅适用于图像。

scaleFrom

"ScaleFromTopLeft" | "ScaleFromMiddle" | "ScaleFromBottomRight"

可选。 指定在缩放形状时,该形状的哪一部分将保持在原有的位置。 如果省略,则表示形状的左上角保留其位置。

返回

void

注解

[ API 集:WordApiDesktop 1.2 ]

scaleWidth(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleWidth(scaleFactor: number, scaleType: Word.ShapeScaleType, scaleFrom?: Word.ShapeScaleFrom): void;

参数

scaleFactor

number

指定形状调整后的宽度与当前或原始宽度的比例。

scaleType
Word.ShapeScaleType

指定是相对于其原始大小还是当前大小缩放形状。 原始大小缩放选项仅适用于图像。

scaleFrom
Word.ShapeScaleFrom

可选。 指定在缩放形状时,该形状的哪一部分将保持在原有的位置。 如果省略,则表示形状的左上角保留其位置。

返回

void

注解

[ 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) => {
  // Scales the first set of grouped shapes (including text boxes) found in the document body.
  const firstShapeGroup: Word.Shape = context.document.body.shapes
    .getByTypes([Word.ShapeType.group])
    .getFirstOrNullObject();
  firstShapeGroup.load("shapeGroup/shapes");
  await context.sync();

  if (firstShapeGroup.isNullObject) {
    console.log("No shape groups found in the document body.");
    return;
  }

  let shapeGroup: Word.ShapeGroup = firstShapeGroup.shapeGroup;
  console.log("About to scale the first shape group found in the document body:", shapeGroup.shapes);
  firstShapeGroup.scaleHeight(0.75, Word.ShapeScaleType.currentSize);
  firstShapeGroup.scaleWidth(0.5, Word.ShapeScaleType.currentSize, Word.ShapeScaleFrom.scaleFromBottomRight);

  console.log("Scaled the first shape group.");
});

scaleWidth(scaleFactor, scaleType, scaleFrom)

按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。

scaleWidth(scaleFactor: number, scaleType: "CurrentSize" | "OriginalSize", scaleFrom?: "ScaleFromTopLeft" | "ScaleFromMiddle" | "ScaleFromBottomRight"): void;

参数

scaleFactor

number

指定形状调整后的宽度与当前或原始宽度的比例。

scaleType

"CurrentSize" | "OriginalSize"

指定是相对于其原始大小还是当前大小缩放形状。 原始大小缩放选项仅适用于图像。

scaleFrom

"ScaleFromTopLeft" | "ScaleFromMiddle" | "ScaleFromBottomRight"

可选。 指定在缩放形状时,该形状的哪一部分将保持在原有的位置。 如果省略,则表示形状的左上角保留其位置。

返回

void

注解

[ API 集:WordApiDesktop 1.2 ]

select(selectMultipleShapes)

选择形状。

select(selectMultipleShapes?: boolean): void;

参数

selectMultipleShapes

boolean

可选。 是否选择多个浮动形状。 默认值 false为 。

返回

void

注解

[ 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();
});

set(properties, options)

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

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

参数

properties
Word.Interfaces.ShapeUpdateData

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

options
OfficeExtension.UpdateOptions

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

返回

void

set(properties)

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

set(properties: Word.Shape): void;

参数

properties
Word.Shape

返回

void

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,依次调用toJSON传递给它的 对象的 方法。) 虽然原始Word.Shape对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.ShapeData) ,其中包含从原始对象加载的任何子属性的浅表副本。

toJSON(): Word.Interfaces.ShapeData;

返回

track()

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

track(): Word.Shape;

返回

untrack()

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

untrack(): Word.Shape;

返回