Finding a range Name

Laurence,

My code opens a designer file and generates several hundred named ranges, using the API like:

Range range ;
range = w.Cells.CreateRange(startingRow, 2, rangeLength, 1) ;
range.Name = rangeName ;

Then later on, as I'm creating validation lists, I am using the range names, due to missing data on my side, not all ranges exist, so I want to check if the range name exists prior to using it (or the validation will fail when saving the spreadsheet). I tried both:

Range range = excel.Worksheets.GetRangeByName( rangeName );

and

Range[] allRanges = excel.Worksheets.GetNamedRanges() ;

Both approaches seem to only work on the range names that pre-existed in the designer file. Am I doing something wrong, or missing an API? As a workaround I am using an internally managed array, but expected the component to have some way of finding something I just defined programatically.

Kris

Hi Kris,

Yes, these two methods only return pre-defined ranges in the designer file. So you have to manage them by your own code.

I think your suggestion is great. Aspose.Excel will support to find range in all named ranges, whether in designer file or defined by code. Thanks for your patience.

@kris_k,
Aspose.Cells has replaced Aspose.Excel which is no more continued now. The new product Aspose.Cells supports all the functionalities of Aspose.Excel as well as contains the latest features of MS Excel. We can check the named range using the following sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

//Creating a named range
Range range = worksheet.Cells.CreateRange("A1", "B4");

//Setting the name of the named range
range.Name = "Test_Range";
for (int row = 0; row < range.RowCount; row++)
{
    for (int column = 0; column < range.ColumnCount; column++)
    {
        range[row, column].PutValue("Test");
    }
}
worksheet.Cells.AddRange(range);
if(workbook.Worksheets.GetRangeByName("Test_Range") == null)
{
    Console.WriteLine("Test_Range is not found");
}
else
{
    Console.WriteLine("Test_Range is found");
}

You can find more information about ranges in the following artilces:
Tables and Ranges
Creating a Named Range

You may download the free trial version from the following link:
Aspose.Cells for .NET(Latest version)

You may download a runnable solution here for testing different functions of this new product.