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

Free Support Forum - aspose.com

Question about named ranges

Hi guys,

i have a question about “ranges”.
In the propery “worksheet” i have the function “GetSelectedRanges”,
to obtain the active ranges and a property “Names” for named ranges.



I’m still looking for a function “GetSelectedNames” to find the active named ranges.
In my case is only “TestNamend2” marked and I need a possibility to get out what named ranges are selected.

I have my test document attached.

Thank you for your support.

Hi,


Thanks for your posting and using Aspose.Cells.

We have checked your requirements and logged them as a New Feature request in our database to support your needs.

This issue has been logged as

  • CELLSNET-45200 - GetSelectedNames() method needed to find the selected named ranges

Hi,

Thanks for using Aspose.Cells.

Please get the defined name of the range by iterating named ranges.

Please see the following code.

C#
private static void Main(string[] args) { using (Workbook workbook = new Workbook(Path.Combine(path, @"NamedRanges.xlsx"))) { Range[] ranges = workbook.Worksheets.GetNamedRanges(); ArrayList selectedRanges = workbook.Worksheets[0].GetSelectedRanges(); Range sRange = (Range)selectedRanges[0]; Console.WriteLine(GetDefinedName(ranges, sRange)); }

}//end main

private static string GetDefinedName(Range[] ranges, Range sRange)
{
for (int i = 0; i < ranges.Length; i++)
{
Range range = ranges[i];
if (range.Worksheet == sRange.Worksheet)
{
if (range.FirstRow == sRange.FirstRow
&& range.FirstColumn == sRange.FirstColumn
&& range.ColumnCount == sRange.ColumnCount
&& range.RowCount == sRange.RowCount)
{

            return range.Name;
        }
    }
}
return null;

}//end getdefinedname



Hi,

thanks for your solution.
It would be nice if this functionality was already included in a method “GetSelectedNamedRanges”. Just like “GetSelectedRanges”.

Thanks for the support
Have a nice day.

Hi,

i still have a problem with more named ranges when i use this solution.
I have selected two named ranges in the excel sheet, but the result is one.

Here my code:

var wbook = GetWorkBook(TestResTemplates.TemplateRanges);
var namedRanges = wbook.Worksheets.GetNamedRanges();
var selectedRanges = wbook.Worksheets[0].GetSelectedRanges();
var results = GetSelectedNamedRanges(namedRanges, selectedRanges);

private List GetSelectedNamedRanges(Range[] namedRanges, ArrayList selectedRanges)
{
var results = new List();
foreach (var range in selectedRanges)
{
var srange = range as Range;
var selectedNamedRanges = namedRanges.Where(n => n.FirstRow == srange.FirstRow && n.FirstColumn == srange.FirstColumn && n.ColumnCount == srange.ColumnCount && n.RowCount == srange.RowCount);
results.AddRange(selectedNamedRanges);
}

return results;
}

My test document is in the appendix.

Thank you for your support.

Hi,


Thanks for your posting and using Aspose.Cells.

We have tested your code and found the issue and reopened it. We have also logged your comment in our database for product team consideration. We will look into it and implement it if possible. Once, there is some news for you, we will let you know asap. I have also attached the screenshot for a reference.

Hi,

thank you for the information and your support.
I’m curious. :slight_smile:

Hi,

Thanks for using Aspose.Cells.

We have looked into this issue further and found Aspose.Cells is working fine.

The selected ranges are "B:F" and "H2:L3".

"H2:L3" is a named range, but "B:F" is not a named range.

So the output of the code is correct.