I noticed the following behavior regarding apostrophes in Office Excel.
When I manually add a named range with worksheet scope to a sheet with a name containing an apostrophe, e.g. "C 09.02(LAO PEOPLE'S DEMOCRATI)"
the named range is created like this:
Scope: "C 09.02(LAO PEOPLE'S DEMOCRA)"
Refers to: "='C 09.02(LAO PEOPLE''S DEMOCRATI)'!$D$8"
Excel is adding an additional apostrophe in the "Refers to:" field. NOTE: Two apostrophes in PEOPLE''S
ClosedXML doesn’t seem to behave in a similar manner when adding named ranges in this particular case.
When I try to open the Excel file I get a message saying that it is a problem with the document and the named ranges are removed.
I have made a workaround in a private fork of ClosedXML to deal with this issue.
In the class XLRangeAddress I have changed
Worksheet.Name
to
Worksheet.Name.Replace("'","''")
Like this:
```
public String ToStringFixed(XLReferenceStyle referenceStyle, Boolean includeSheet)
{
if (includeSheet)
return String.Format("'{0}'!{1}:{2}",
Worksheet.Name.Replace("'","''"),
_firstAddress.ToStringFixed(referenceStyle),
_lastAddress.ToStringFixed(referenceStyle));
return _firstAddress.ToStringFixed(referenceStyle) + ":" + _lastAddress.ToStringFixed(referenceStyle);
}
```
When I manually add a named range with worksheet scope to a sheet with a name containing an apostrophe, e.g. "C 09.02(LAO PEOPLE'S DEMOCRATI)"
the named range is created like this:
Scope: "C 09.02(LAO PEOPLE'S DEMOCRA)"
Refers to: "='C 09.02(LAO PEOPLE''S DEMOCRATI)'!$D$8"
Excel is adding an additional apostrophe in the "Refers to:" field. NOTE: Two apostrophes in PEOPLE''S
ClosedXML doesn’t seem to behave in a similar manner when adding named ranges in this particular case.
When I try to open the Excel file I get a message saying that it is a problem with the document and the named ranges are removed.
I have made a workaround in a private fork of ClosedXML to deal with this issue.
In the class XLRangeAddress I have changed
Worksheet.Name
to
Worksheet.Name.Replace("'","''")
Like this:
```
public String ToStringFixed(XLReferenceStyle referenceStyle, Boolean includeSheet)
{
if (includeSheet)
return String.Format("'{0}'!{1}:{2}",
Worksheet.Name.Replace("'","''"),
_firstAddress.ToStringFixed(referenceStyle),
_lastAddress.ToStringFixed(referenceStyle));
return _firstAddress.ToStringFixed(referenceStyle) + ":" + _lastAddress.ToStringFixed(referenceStyle);
}
```