Free Support Forum - aspose.com

I've got a list of named ranges- how to I set their values (the cells that specify the range)?

Hi, and thank you for your time.

I've got the following code that lists all the named ranges in my workbook.

I need to loop through these and set their value:

i.e. Named Range = xrange

Need to set the value of xrange to =Data!$D$18:$D$26

NameCollection ranges = wd.Workbook.Worksheets.Names;

string blah = string.Empty;

for (int i = 0; i < ranges.Count; i++)

{

if (ranges[i].GetRange() != null)

{

blah = ranges[i].GetRange().GetEnumerator.ToString();

}

lbRecent.Items.Add(new ListItem(ranges[i].Text + " ::: " + blah));

}


This message was posted using Page2Forum from Name.GetRanges Method - Aspose.Cells for .NET

Actually, now that I look at it more closely, it appears that the spreadsheet I was given has many "Names" in it, I don't think they are actually named ranges, because they don't show up in the GetRanges. But, they are used to identify ranges. Therefore, I thought this would work:

for (int i = 0; i < ranges.Count; i++)

{

Name name = ranges[i];

name.RefersTo = "=Data!$A$18:$A$" + view.Count.ToString();

}

Debugging, I can see that the name.RefersTo is being set correctly, however, when I save the document they are not saved?

The variable ranges is misleading, here's its definition:

NameCollection ranges = wd.Workbook.Worksheets.Names;

Hi,


Well, I think you may get the named ranges using GetRange/GetRanges method of Name, you may put values in the range cells iterarting the range cells. You can also input a common value into all the cells of the named range using Range.Value attribute.

E.g


Workbook aWk1 = new Workbook(@“e:\test2\calculationissue\AsposeTestCase.xlsx”);
//…
//Put some sample values
Range[] aRange = aWk1.Worksheets.Names[MyRange].GetRanges();
foreach (Range aArea in aRange)
{
for (int r = aArea.FirstRow, x = 0; x < aArea.RowCount; x++, r++)
for (int c = aArea.FirstColumn, y = 0; y < aArea.ColumnCount; y++, c++)
aWk1.Worksheets[Sheet_Name].Cells[r, c].PutValue(10+x.0+y);
//…
}

For complete reference about using Named Ranges, see the topic:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/named-ranges.html


Thank you.