We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Java API for getting the named range of a cell

I have the following code that work using Excel interop:

Range range = <somerange>;
Name name = range.Name;

I am looking for a similar functionality using Aspose cells. What would be the best practice to get a named range under a specific cell?



See the sample code for your reference.
Sample code:

// Instantiating a Workbook object
Workbook workbook = new Workbook("book1.xls");

WorksheetCollection worksheets = workbook.getWorksheets();

// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();

// Creating a named range
Range namedRange = cells.createRange("B4", "G14");
// set its name

Please see the document on Named ranges for your complete reference.

I was looking for a code sample of how to get a named range from an existing cell.

Lets say that I have Excel, that “A1” cells refers to a range named: sample_range.

Now, when I open such Excel, I want to check which range the cell “A1” refers to.

I hope it more clear now.


Since you need to get named range by cell, you have to iterate through named ranges in the spreadsheet. See the code segment on how to get the range by specific cell for your reference:
Sample code:

Workbook workbook = new Workbook("f:\\files\\Book_ranges1.xlsx");
Range[] ranges = workbook.getWorksheets().getNamedRanges();
Range range = getRange(ranges,0,0);
//your code goes here.

public static Range getRange(Range[] ranges,int row, int column)
        for(int i = 0; i < ranges.length; i++)
            Range range = ranges[i];
            if(row >=  range.getFirstRow() && row < range.getFirstRow() + range.getRowCount()
              && column >= range.getFirstColumn() && column < range.getFirstColumn() + range.getColumnCount()
                return range;
        return null;

Hope this helps a bit.