Excel.ShapeCollection class
表示工作表中所有形状的集合。
方法
| add |
将几何形状添加到工作表。 返回 |
| add |
将几何形状添加到工作表。 返回 |
| add |
在此集合的工作表中对形状的子集进行分组。 返回 |
| add |
从 Base64 编码的字符串创建图像并将其添加到工作表中。 返回 |
| add |
将线条添加到工作表。 返回 |
| add |
将线条添加到工作表。 返回 |
| add |
为存储在单元格地址中的本地图像创建引用,并将其显示为单元格上的浮动形状。 |
| add |
从 XML 字符串创建可缩放的矢量图形 (SVG) 并将其添加到工作表。 返回 |
| add |
使用提供的文本作为内容,将文本框添加到工作表。 返回 |
| get |
返回工作表中的形状数。 |
| get |
使用形状名称或 ID 获取形状。 |
| get |
使用其在集合中的位置获取形状。 |
| get |
使用形状名称或 ID 获取形状。 如果 shape 对象不存在,则此方法返回其 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
属性详细信息
context
items
方法详细信息
addGeometricShape(geometricShapeType)
将几何形状添加到工作表。 返回 Shape 表示新形状的对象。
addGeometricShape(geometricShapeType: Excel.GeometricShapeType): Excel.Shape;
参数
- geometricShapeType
- Excel.GeometricShapeType
表示几何形状的类型。 有关详细信息,请参阅 Excel.GeometricShapeType。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-create-and-delete.yaml
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const shape = sheet.shapes.addGeometricShape(Excel.GeometricShapeType.hexagon);
shape.left = 5;
shape.top = 5;
shape.height = 175;
shape.width = 200;
await context.sync();
});
addGeometricShape(geometricShapeType)
将几何形状添加到工作表。 返回 Shape 表示新形状的对象。
addGeometricShape(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"): Excel.Shape;
参数
- 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"
表示几何形状的类型。 有关详细信息,请参阅 Excel.GeometricShapeType。
返回
注解
addGroup(values)
在此集合的工作表中对形状的子集进行分组。 返回 Shape 表示新形状组的对象。
addGroup(values: Array<string | Shape>): Excel.Shape;
参数
- values
-
Array<string | Excel.Shape>
形状 ID 或形状对象的数组。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-groups.yaml
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Shapes");
const square = sheet.shapes.getItem("Square");
const pentagon = sheet.shapes.getItem("Pentagon");
const octagon = sheet.shapes.getItem("Octagon");
const shapeGroup = sheet.shapes.addGroup([square, pentagon, octagon]);
shapeGroup.name = "Group";
console.log("Shapes grouped");
await context.sync();
});
addImage(base64ImageString)
从 Base64 编码的字符串创建图像并将其添加到工作表中。 返回 Shape 表示新图像的对象。
addImage(base64ImageString: string): Excel.Shape;
参数
- base64ImageString
-
string
以 JPEG 或 PNG 格式表示图像的 Base64 编码字符串。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-images.yaml
const myFile = document.getElementById("selectedFile") as HTMLInputElement;
const reader = new FileReader();
reader.onload = (event) => {
Excel.run((context) => {
const startIndex = reader.result.toString().indexOf("base64,");
const myBase64 = reader.result.toString().substr(startIndex + 7);
const sheet = context.workbook.worksheets.getItem("Shapes");
const image = sheet.shapes.addImage(myBase64);
image.name = "Image";
return context.sync();
});
};
// Read in the image file as a data URL.
reader.readAsDataURL(myFile.files[0]);
addLine(startLeft, startTop, endLeft, endTop, connectorType)
将线条添加到工作表。 返回 Shape 表示新行的对象。
addLine(startLeft: number, startTop: number, endLeft: number, endTop: number, connectorType?: Excel.ConnectorType): Excel.Shape;
参数
- startLeft
-
number
从线条起点到工作表左侧的距离(以磅为单位)。
- startTop
-
number
从线条起点到工作表顶部的距离(以点为单位)。
- endLeft
-
number
从线的末尾到工作表左侧的距离(以点为单位)。
- endTop
-
number
从线的末尾到工作表顶部的距离(以点为单位)。
- connectorType
- Excel.ConnectorType
表示连接器类型。 有关详细信息,请参阅 Excel.ConnectorType。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-lines.yaml
await Excel.run(async (context) => {
const shapes = context.workbook.worksheets.getItem("Shapes").shapes;
const line = shapes.addLine(200, 50, 300, 150, Excel.ConnectorType.straight);
line.name = "StraightLine";
await context.sync();
});
addLine(startLeft, startTop, endLeft, endTop, connectorType)
将线条添加到工作表。 返回 Shape 表示新行的对象。
addLine(startLeft: number, startTop: number, endLeft: number, endTop: number, connectorType?: "Straight" | "Elbow" | "Curve"): Excel.Shape;
参数
- startLeft
-
number
从线条起点到工作表左侧的距离(以磅为单位)。
- startTop
-
number
从线条起点到工作表顶部的距离(以点为单位)。
- endLeft
-
number
从线的末尾到工作表左侧的距离(以点为单位)。
- endTop
-
number
从线的末尾到工作表顶部的距离(以点为单位)。
- connectorType
-
"Straight" | "Elbow" | "Curve"
表示连接器类型。 有关详细信息,请参阅 Excel.ConnectorType。
返回
注解
addLocalImageReference(address)
注意
此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。
为存储在单元格地址中的本地图像创建引用,并将其显示为单元格上的浮动形状。
addLocalImageReference(address: string): Excel.Shape;
参数
- address
-
string
包含本地图像的单元格的地址。
返回
Shape与浮动形状关联的对象。
注解
addSvg(xml)
注意
此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。
从 XML 字符串创建可缩放的矢量图形 (SVG) 并将其添加到工作表。 返回 Shape 表示新图像的对象。
addSvg(xml: string): Excel.Shape;
参数
- xml
-
string
表示 SVG 的 XML 字符串。
返回
注解
addTextBox(text)
使用提供的文本作为内容,将文本框添加到工作表。 返回 Shape 表示新文本框的对象。
addTextBox(text?: string): Excel.Shape;
参数
- text
-
string
表示将在创建的文本框中显示的文本。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-textboxes.yaml
await Excel.run(async (context) => {
const shapes = context.workbook.worksheets.getItem("Shapes").shapes;
const textbox = shapes.addTextBox("A box with text");
textbox.left = 100;
textbox.top = 100;
textbox.height = 20;
textbox.width = 175;
textbox.name = "Textbox";
await context.sync();
});
getCount()
返回工作表中的形状数。
getCount(): OfficeExtension.ClientResult<number>;
返回
OfficeExtension.ClientResult<number>
注解
getItem(key)
使用形状名称或 ID 获取形状。
getItem(key: string): Excel.Shape;
参数
- key
-
string
要检索的形状的名称或 ID。
返回
注解
getItemAt(index)
使用其在集合中的位置获取形状。
getItemAt(index: number): Excel.Shape;
参数
- index
-
number
要检索的形状的从零开始的索引。
返回
注解
getItemOrNullObject(key)
使用形状名称或 ID 获取形状。 如果 shape 对象不存在,则此方法返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getItemOrNullObject(key: string): Excel.Shape;
参数
- key
-
string
要检索的形状的名称或 ID。
返回
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Excel.Interfaces.ShapeCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.ShapeCollection;
参数
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Excel.ShapeCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.ShapeCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.ShapeCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.ShapeCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): Excel.Interfaces.ShapeCollectionData;