Free Support Forum - aspose.com

Get sheet index of sheet where the shape is located

Hi, I have a question regarding Aspose.cells for Java, about Shape class.

Is there a way to get a sheet index or sheet name of sheet in which the Shape object is located?

Regards,
Milorad

@Milorad,

There is no specific command for it, therefore you may write your own logic similar to the following piece of code:

Workbook wb = new Workbook("Book1.xlsx");
int totalSheets = wb.getWorksheets().getCount();
for(int i = 0 ; i < totalSheets; i++)
{
	Worksheet ws =  wb.getWorksheets().get(i);
	if(ws.getShapes().getCount() > 0)
	{
		System.out.println(ws.getName());
	}
}

Hi, thanks for your answer.
I actually don’t need this, I needed a sheet index of a specific Shape object (so, a sheet where the shape is located). There are similar methods for Cell and Chart classes. There is cell.getWorksheet() method for Cell class, and for Chart class there is chart.getWorksheet() method, but I couldn’t find anything similar for Shape class. Is there a way to do this?

Regards

@Milorad,

You may easily do it to get sheet’s name or index where your particular shape is located. You may try the following sample code for your needs:
e.g.
Sample code:

 Workbook workbook = new Workbook("f:\\files\\excelTemplate.xlsx");

        for (Object sheetObj : workbook.getWorksheets()) {
            Worksheet sheet = (Worksheet) sheetObj;

            for (Object shapeObj : sheet.getShapes()) {
                com.aspose.cells.Shape shape = (com.aspose.cells.Shape) shapeObj;

                if (shape.getName()== "MyShape") {
                    System.out.println(shape.getName() + "exists in " + sheet.getName() + " worksheet");   
                }
            }
        }

Hope, this helps a bit.

Hi,

thank you very much for answering, I can use this code to solve my problem.

Besides, is it expected to have this thing simplified in some near future, like having a new method getWorksheet() of Shape class similarly like it is in Cell and Chart classes?

@Milorad,
We have logged you requirement in our database for investigation and implementation (if possible). You will be notified here once any update is ready for sharing.

This requirement is logged as:
CELLSJAVA-43322 - Shape.getWorksheet() property required

@Milorad,
This is to inform you that we have fixed your issue now. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.