如何以编程方式(C#)在SpreadsheetGear中选择形状(或图表)?
我试过了:
1. IShape.select(false); // failed
2. ShapeSelection = ...; // failed
答案 0 :(得分:1)
IShape。Select(...)是选择形状所需的正确API。我确实看到你传递了false
的“替换”参数,这意味着你要将这个新形状添加到已经选中的任何其他形状(即IWorksheetWindowInfo。ShapeSelection。{ {3}}将为2或更高)。如果要替换当前的形状选择,则需要传入true
。
下面是一些示例代码,演示了如何在工作表上选择一个或多个形状并使用某些Console.WriteLine(...)来验证此行为,但我还在SpreadsheetGear的WorkbookView UI控件上查看这些操作时验证了此行为:
// Create a workbook and a couple shapes on the active worksheet.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IShape shape1 = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 5, 5, 50, 50);
IShape shape2 = worksheet.Shapes.AddShape(AutoShapeType.Oval, 75, 57, 50, 50);
// Ensure no shapes are selected.
IShapeRange shapeSelection = worksheet.WindowInfo.ShapeSelection;
Console.WriteLine($"ShapeSelection is null? {shapeSelection == null}");
// OUTPUT: ShapeSelection is null? True
// Select shape1 ("Rectangle 1")
shape1.Select(true);
shapeSelection = worksheet.WindowInfo.ShapeSelection;
Console.WriteLine($"ShapeSelection: Count={shapeSelection.Count}, Name[0]={shapeSelection[0].Name}");
// OUTPUT: ShapeSelection: Count=1, Name[0]=Rectangle 1
// Select shape2 ("Oval 2")
shape2.Select(true);
shapeSelection = worksheet.WindowInfo.ShapeSelection;
Console.WriteLine($"ShapeSelection: Count={shapeSelection.Count}, Name[0]={shapeSelection[0].Name}");
// OUTPUT: ShapeSelection: Count=1, Name[0]=Oval 2
// Select both shapes (false passed into IShape.Select(...))
shape1.Select(false);
shapeSelection = worksheet.WindowInfo.ShapeSelection;
Console.WriteLine($"ShapeSelection: Count={shapeSelection.Count}, Name[0]={shapeSelection[0].Name}, Name[1]={shapeSelection[1].Name}");
// OUTPUT: ShapeSelection: Count=2, Name[0]=Oval 2, Name[1]=Rectangle 1