[]
GcExcel允许您对工作表中的形状进行组合或取消组合。当需要对一堆形状执行某些操作时,可以将形状组合在一起。例如:为形状添加相似样式、对齐、旋转、复制或粘贴组合形状。它不仅节省了大量的时间和精力,而且有助于确保在所有形状中保持所需的一致性。
可以使用IShapeRange接口的Group方法将多个形状组合在一起。IShapeRange接口表示需要组合在一起的形状的范围。组合的形状表现为单个形状。
请参阅以下示例代码以对形状进行组合。
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);
// Creating shapes collection for activeSheet
IShapes shapes = worksheet.getShapes();
// Adding Shapes to shapes collection
IShape ShapeBegin = shapes.addShape(AutoShapeType.Wave, 10, 10, 100, 100);
IShape EndBegin = shapes.addShape(AutoShapeType.RoundedRectangle, 200, 200, 100, 100);
// Adding Connector Shape to shapes collection
IShape ConnectorShape = shapes.addConnector(ConnectorType.Straight, 10, 10, 101, 101);
// Connecting ShapeBegin & EndBegin shapes by connector shape
ConnectorShape.getConnectorFormat().beginConnect(ShapeBegin, 3);
ConnectorShape.getConnectorFormat().endConnect(EndBegin, 0);
// Adding IsoscelesTriangle shape to shapes collection
shapes.addShape(AutoShapeType.IsoscelesTriangle, 370.8, 50.8, 81.6, 102.0);
// Creating shpRange collection to group certain shapes as given in array
IShapeRange shpRange = shapes
.getRange(new String[] { shapes.get(0).getName(), shapes.get(1).getName(), shapes.get(2).getName() });
// Grouping Shapes
IShape grouped = shpRange.group();
// Setting Style for Grouped shape together
grouped.getLine().getColor().setRGB(Color.GetDarkOrange());
grouped.getFill().getColor().setRGB(Color.GetLightGreen());
System.out.println("Group Name is: " + grouped.getName());
// Saving workbook to Xlsx
workbook.save("9-GroupedShapes.xlsx", SaveFileFormat.Xlsx);
可以使用IShape接口的Ungroup方法对指定范围内的一组形状进行取消组合操作。
请参阅以下示例代码将组合形状取消组合。
// Initialize workbook
Workbook workbook = new Workbook();
// Open workbook
workbook.open("9-GroupedShapes.xlsx");
IShapes shapes = workbook.getWorksheets().get(0).getShapes();
// UnGroup Shapes
for (int i = 0; i < shapes.getCount(); i++) {
if (shapes.get(i).getType() == ShapeType.Group) // Or, if (shapes[i].Name == "Group 1")
shapes.get(i).ungroup();
}
// Or, we can just pass GroupName to Ungroup it
// shapes["Group 1"].Ungroup();
// Saving workbook to Xlsx
workbook.save("10-UnGroupedShapes.xlsx", SaveFileFormat.Xlsx);