ClosedXML / ClosedXML/ClosedXML
App crashes when Loading Excel file with external references
- Dominant language
- C#
- Stars
- 5.7k
- Forks
- 933
- Avg merge
- 12h 14m
- Merged PRs (30d)
- 1
Description
## Read and complete the full issue template
Do not randomly delete sections. They are here for a reason.
**Do you want to request a *feature* or report a *bug*?**
- [x] Bug
- [ ] Feature
- [ ] Question
**Did you test against the latest CI build?**
- [ ] Yes
- [x] No
If you answered `No`, please test with [the latest development build](https://ci.appveyor.com/project/ClosedXML/ClosedXML/branch/develop/artifacts) first.
**Version of ClosedXML**
e.g. 0.95.3
**What is the current behavior?**
ClosedXML crashes when loading workbooks containing formulas with external references, such as [29]Overview!$A:$IV or Overview!$A:$IV. The parser fails to handle these formulas, resulting in an unhandled exception.
sample formula from NameManager
= '\\lp.com\data\Users\dummyuser\Dropbox\I:\testfolder\[dummyfile.XLS]Overview'!$A:$IV
**What is the expected behavior or new feature?**
ClosedXML should gracefully skip or ignore unsupported external reference formulas during parsing, so that workbooks with such formulas can be loaded without crashing.
**Is this a regression from the previous version?**
No
Regressions get higher priority. Test against the latest build of the previous minor version. For example, if you experience a problem on v0.95.3, check whether it the problem occurred in v0.94.2 too.
## Reproducibility
**This is an important section. Read it carefully. Failure to do so will cause a 'RTFM' comment.**
Without a code sample, it is unlikely that your issue will get attention. Don't be lazy. Do the effort and assist the developers to reproduce your problem. Code samples should be [minimal complete and verifiable](https://stackoverflow.com/help/mcve). Sample spreadsheets should be attached whenever applicable. Remove sensitive information.
**Code to reproduce problem:**
public void Main()
`{
using (var stream = File.OpenRead("
[workbook_with_external_refs.xlsx](https://github.com/user-attachments/files/24996786/workbook_with_external_refs.xlsx)
.xlsx"))
{
var wb = new XLWorkbook(stream); // Crashes if workbook contains external reference formulas
}
}`
STACKTRACE:
The formula ` [29]Overview!$A:$IV` wasn't parsed correctly. The expression ` [29]` was parsed, but the rest `Overview!$A:$IV` wasn't.
at ClosedXML.Parser.FormulaParser`3.Formula()
at ClosedXML.Parser.FormulaParser`3.CellFormulaA1(String formula, TContext context, IAstFactory`3 factory)
at ClosedXML.Excel.CalcEngine.Visitors.FormulaReferences.ForFormula(String formula) in \src\ClosedXML\ClosedXML\Excel\CalcEngine\Visitors\FormulaReferences.cs:line 45
Suggested fix:
An Option to ignore formula errors when loading excel file using the OpenSettings in the SpreadsheetDocument.Open() OR
Add pre-validation in [FormulaReferences.ForFormula] to detect and skip formulas with unsupported external reference syntax (e.g., [29]Overview!$A:$IV or Overview!$A:$IV) before parsing. This prevents the parser from crashing and allows the workbook to load.
`if (Regex.IsMatch(formula, @"^\\s*\\[\\d+\\][^!]+!.*") ||
Regex.IsMatch(formula, @"^\\s*[^!]+!\\$?[A-Z]+(:\\$?[A-Z]+)?"))
{
return new FormulaReferences(formula);
}`
This change will make ClosedXML will help handling files with external references.
Example:
- [x] I attached a sample spreadsheet. (You can drag files on to this issue)
Contributor guide
Research direction
Start by reproducing the crash with workbook_with_external_refs.xlsx and follow the stack trace into ClosedXML/ClosedXML/Excel/CalcEngine/Visitors/FormulaReferences.cs, especially ForFormula. Read the surrounding FormulaParser behavior for the reported external-reference forms. Done means the workbook loads without an unhandled parsing exception while unsupported external formulas are handled as specified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100