Excel.ShapeCollection class
表示工作表中所有形状的集合。
- 扩展
注解
属性
context | 与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。 |
items | 获取此集合中已加载的子项。 |
方法
add |
将几何形状添加到工作表。 返回表示 |
add |
将几何形状添加到工作表。 返回表示 |
add |
在此集合的工作表中对形状的子集进行分组。 返回一个 |
add |
从 Base64 编码的字符串创建图像,并将其添加到工作表。 返回 |
add |
将线条添加到工作表。 返回表示 |
add |
将线条添加到工作表。 返回表示 |
add |
使用提供的文本作为内容,将文本框添加到工作表。 返回表示 |
get |
返回工作表中的形状数。 |
get |
使用形状的名称或 ID 获取形状。 |
get |
使用其在集合中的位置获取形状。 |
get |
使用形状的名称或 ID 获取形状。 如果形状对象不存在,则此方法返回一个对象,其 |
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(geometricShapeTypeString)
将几何形状添加到工作表。 返回表示 Shape
新形状的 对象。
addGeometricShape(geometricShapeTypeString: "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;
参数
- geometricShapeTypeString
-
"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
一个 Base64 编码的字符串,表示 JPEG 或 PNG 格式的图像。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-images.yaml
const myFile = <HTMLInputElement>document.getElementById("selectedFile");
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, connectorTypeString)
将线条添加到工作表。 返回表示 Shape
新行的 对象。
addLine(startLeft: number, startTop: number, endLeft: number, endTop: number, connectorTypeString?: "Straight" | "Elbow" | "Curve"): Excel.Shape;
参数
- startLeft
-
number
从行开始到工作表左侧的距离(以磅为单位)。
- startTop
-
number
从行开始到工作表顶部的距离(以磅为单位)。
- endLeft
-
number
从行尾到工作表左侧的距离(以磅为单位)。
- endTop
-
number
从行尾到工作表顶部的距离(以磅为单位)。
- connectorTypeString
-
"Straight" | "Elbow" | "Curve"
表示连接器类型。 有关详细信息,请参阅 Excel.ConnectorType
。
返回
注解
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 获取形状。 如果形状对象不存在,则此方法返回一个对象,其 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”数组,其中包含集合项中任何已加载属性的浅表副本。
toJSON(): Excel.Interfaces.ShapeCollectionData;