Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Created Unassigned: XLWorkbook_Load does not load NamedRanges in specific sheets [9446]

$
0
0
When loading an existing workbook that contains named ranges, ranges are only applied at the Workbook level even if the range is for a specific Worksheet. The crux of the problem is the "LoadDefinedNames" function in [XLWorkbook_Load.cs](https://closedxml.codeplex.com/SourceControl/latest#ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs) only assigns named ranges to a specific worksheet if it's a Print_Area name or has a "[LocalSheetId](https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.definedname.localsheetid(v=office.14).aspx)" attribute. However, if it specifies the sheet using the typical reference syntax (e.g., Sheet1!$A$1), it adds it to the Workbook's NamedRanges collection and not the Worksheets' NamedRanges collection.

A possible fix is to add this at line 733:

```
string sheetName, sheetArea;
ParseReference(text, out sheetName, out sheetArea);
if (Worksheet(sheetName) != null &&
Worksheet(sheetName).NamedRanges.Any(nr => nr.Name == name))
{
Worksheet(sheetName).NamedRanges.Add(name, text, comment);
}
else
{
if (!NamedRanges.Any(nr => nr.Name == name))
NamedRanges.Add(name, text, comment);
}
```

This uses the same "parsing" logic that the Print_Area reference uses. TBD is whether to add the full "text" of the reference to Worksheet(sheetName).NamedRanges or whether to add the "sheetArea" similar to what is done in "Print_Area".

If this is a reasonable solution, I can submit a PR.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>