Word.Shape class
表示页眉、页脚或文档正文中的形状。 目前仅支持以下形状:文本框、几何形状、组、图片和画布。
注解
使用方
- Word.画布:形状
- Word.文档:后台
- Word.段落:insertCanvas、insertGeometricShape、insertPictureFromBase64、insertTextBox
- Word.范围:insertCanvas、insertGeometricShape、insertPictureFromBase64、insertTextBox
- Word.ShapeCollection: getById, getByIdOrNullObject, getFirst, getFirstOrNullObject, group, items
- Word.ShapeGroup: shape
示例
// 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);
});
属性
| allow |
指定给定形状能否与其他形状重叠。 |
| alt |
指定表示与形状关联的替代文本的字符串。 |
| body | 获取 |
| canvas | 获取与形状关联的画布。 如果形状类型不是 |
| context | 与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。 |
| fill | 返回形状的填充格式。 |
| geometric |
指定形状的几何形状类型。 如果不是几何形状,则为 null。 |
| height | 指定形状的高度(以磅为单位)。 |
| height |
指定形状高度与相对垂直大小的百分比 (即属性 |
| id | 获取表示形状标识符的整数。 |
| is |
检查此形状是组形状的子级还是画布形状。 |
| left | 指定从形状左侧到相对水平位置 (即属性) 的距离(以磅为 |
| left |
指定从形状左侧到相对水平位置的百分比 ( |
| lock |
指定是否锁定此形状的纵横比。 |
| name | 指定形状的名称。 |
| parent |
获取此子形状的顶层父画布形状。 如果它不是画布的子形状,则它将为 null。 |
| parent |
获取此子形状的顶层父组形状。 如果不是组的子形状,则为 null。 |
| relative |
指定形状的相对水平位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition. |
| relative |
指定形状的相对水平大小。 与该属性一起使用 |
| relative |
指定形状的相对垂直位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeVerticalPosition. |
| relative |
指定形状的相对垂直大小。 与该属性一起使用 |
| rotation | 指定形状的旋转(以度为单位)。 不适用于画布形状。 |
| shape |
获取与形状关联的形状组。 如果形状类型不是 |
| text |
获取 |
| text |
返回形状的文字环绕格式。 |
| top | 指定从形状的顶部边缘到相对垂直位置 (即属性) 的距离(以点为 |
| top |
指定从形状顶部边缘到相对垂直位置(即属性) )的百分比 ( |
| type | 获取形状类型。 目前仅支持以下形状:文本框、几何形状、组、图片和画布。 |
| visible | 指定形状是否可见。 不适用于内联形状。 |
| width | 指定形状的宽度(以磅为单位)。 |
| width |
指定形状宽度与相对水平大小 ((即 |
方法
| delete() | 删除形状及其内容。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| move |
按点数水平移动形状。 |
| move |
按点数垂直移动形状。 |
| scale |
按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。 |
| scale |
按指定因子缩放形状的高度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。 |
| scale |
按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。 |
| scale |
按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。 |
| select(select |
选择形状。 |
| set(properties, options) | 同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。 |
| set(properties) | 基于现有加载的对象,同时在对象上设置多个属性。 |
| toJSON() | 覆盖 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 |
属性详细信息
allowOverlap
altTextDescription
body
获取 Body 形状的对象。 仅适用于文本框和几何形状。
readonly body: Word.Body;
属性值
注解
重要提示: 如果形状尚未包含文本,则获取此属性会在形状上创建文本故事。 这会修改文档,将其标记为具有未保存的更改,并将条目添加到撤消堆栈中。 若要确定形状是否包含文本而不修改文档,请获取 hasText Word 的属性。TextFrame 进行部署。
示例
// 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;
属性值
注解
示例
// 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
fill
返回形状的填充格式。
readonly fill: Word.ShapeFill;
属性值
注解
示例
// 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"
注解
示例
// 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
heightRelative
指定形状高度与相对垂直大小的百分比 (即属性 relativeVerticalSize) 。 对于内联或子形状,无法设置它。
heightRelative: number;
属性值
number
注解
示例
// 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
isChild
left
指定从形状左侧到相对水平位置 (即属性) 的距离(以磅为 relativeHorizontalPosition 单位)。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。
left: number;
属性值
number
注解
leftRelative
指定从形状左侧到相对水平位置的百分比 (relativeHorizontalalPosition 即属性) 。 对于内联或子形状,它将返回 0 且无法设置。
leftRelative: number;
属性值
number
注解
lockAspectRatio
name
parentCanvas
获取此子形状的顶层父画布形状。 如果它不是画布的子形状,则它将为 null。
readonly parentCanvas: Word.Shape;
属性值
注解
parentGroup
获取此子形状的顶层父组形状。 如果不是组的子形状,则为 null。
readonly parentGroup: Word.Shape;
属性值
注解
relativeHorizontalPosition
指定形状的相对水平位置。 对于内联形状,无法设置它。 有关详细信息,请参阅 Word。RelativeHorizontalPosition.
relativeHorizontalPosition: Word.RelativeHorizontalPosition | "Margin" | "Page" | "Column" | "Character" | "LeftMargin" | "RightMargin" | "InsideMargin" | "OutsideMargin";
属性值
Word.RelativeHorizontalPosition | "Margin" | "Page" | "Column" | "Character" | "LeftMargin" | "RightMargin" | "InsideMargin" | "OutsideMargin"
注解
示例
// 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
指定形状的相对水平大小。 与该属性一起使用 widthRelative 。 对于内联或子形状,无法设置它。
relativeHorizontalSize: Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin";
属性值
Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin"
注解
示例
// 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"
注解
示例
// 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
指定形状的相对垂直大小。 与该属性一起使用 heightRelative 。 对于内联或子形状,无法设置它。
relativeVerticalSize: Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin";
属性值
Word.RelativeSize | "Margin" | "Page" | "TopMargin" | "BottomMargin" | "InsideMargin" | "OutsideMargin"
注解
示例
// 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
shapeGroup
获取与形状关联的形状组。 如果形状类型不是 groupShape,则将返回其isNullObject属性设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
readonly shapeGroup: Word.ShapeGroup;
属性值
注解
示例
// 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;
属性值
注解
示例
// 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;
属性值
注解
示例
// 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
指定从形状的顶部边缘到相对垂直位置 (即属性) 的距离(以点为 relativeVerticalPosition 单位)。 对于内联形状,它将返回 0,无法设置。 对于画布或组中的子形状,它相对于左上角。
top: number;
属性值
number
注解
topRelative
指定从形状顶部边缘到相对垂直位置(即属性) )的百分比 (relativeVerticalPosition 。 对于内联或子形状,它将返回 0 且无法设置。
topRelative: number;
属性值
number
注解
type
获取形状类型。 目前仅支持以下形状:文本框、几何形状、组、图片和画布。
readonly type: Word.ShapeType | "Unsupported" | "TextBox" | "GeometricShape" | "Group" | "Picture" | "Canvas";
属性值
Word.ShapeType | "Unsupported" | "TextBox" | "GeometricShape" | "Group" | "Picture" | "Canvas"
注解
示例
// 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
width
widthRelative
指定形状宽度与相对水平大小 ((即 relativeHorizontalSize 属性) )的百分比。 对于内联或子形状,无法设置它。
widthRelative: number;
属性值
number
注解
方法详细信息
delete()
删除形状及其内容。
delete(): void;
返回
void
注解
示例
// 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
注解
示例
// 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
注解
示例
// 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
注解
示例
// 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
注解
scaleWidth(scaleFactor, scaleType, scaleFrom)
按指定因子缩放形状的宽度。 对于图像,你可以说明是相对于原始尺寸还是当前尺寸缩放形状。 对于除图片以外的其他形状来说,缩放总是相对于其当前高度而言。
scaleWidth(scaleFactor: number, scaleType: Word.ShapeScaleType, scaleFrom?: Word.ShapeScaleFrom): void;
参数
- scaleFactor
-
number
指定形状调整后的宽度与当前或原始宽度的比例。
- scaleType
- Word.ShapeScaleType
指定形状是相对于其原始大小还是当前大小进行缩放。 原始大小缩放选项仅适用于图像。
- scaleFrom
- Word.ShapeScaleFrom
可选。 指定在缩放形状时,该形状的哪一部分将保持在原有的位置。 如果省略,则表示形状的左上角保留其位置。
返回
void
注解
示例
// 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
注解
select(selectMultipleShapes)
选择形状。
select(selectMultipleShapes?: boolean): void;
参数
- selectMultipleShapes
-
boolean
可选。 是否选择多个浮动形状。 默认值为 false。
返回
void
注解
示例
// 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)
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;