-
-
Notifications
You must be signed in to change notification settings - Fork 423
Fix template calcChain regeneration and $= formula cells (MiniExcel.OpenXml) #993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michelebastione
merged 8 commits into
mini-software:master
from
martin-vitous:fix/template-calcchain-formula-cells-v2
Aug 4, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
12d7dc0
Fix template calcChain regeneration and $= formula cells (v2 / MiniEx…
martin-vitous 494c02c
Removed obsolete FluentMapping folder from project
michelebastione a76a751
Converted usage of some raw strings to typed constants
michelebastione b44a99b
Changed the ProcessFormulas method to calculate cell references based…
michelebastione ef1abba
Added regression tests for new handling of calcChain data
michelebastione 0b732a8
Minor fix in the generation of the path/name map for the sheets
michelebastione 5924ead
Removed ghost calcChain references across the generated package
michelebastione 28dfcdb
Fixed minor issue with the extrapolation of calcChain references
michelebastione File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/MiniExcel.OpenXml.Tests/Templates/CalcChainAsyncTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System.Xml.Linq; | ||
| using ClosedXML.Excel; | ||
| using MiniExcelLib.OpenXml.Constants; | ||
| using MiniExcelLib.Tests.Common.Utils; | ||
|
|
||
| namespace MiniExcelLib.OpenXml.Tests.Templates; | ||
|
|
||
| public class CalcChainAsyncTests | ||
| { | ||
| private readonly OpenXmlTemplater _templater = MiniExcel.Templaters.GetOpenXmlTemplater(); | ||
|
|
||
| [Fact] | ||
| public async Task TemplateWithStaticFormula_DoesNotWriteStaleOrEmptyCalcChain() | ||
| { | ||
| // A template with a static Excel formula (below an IEnumerable row) carries a calcChain | ||
| // pointing at the formula's pre-render address. After rows are inserted the address is | ||
| // stale — the rendered package must not contain a stale or empty calcChain. | ||
| using var template = AutoDeletingPath.Create(); | ||
| using (var wb = new XLWorkbook()) | ||
| { | ||
| var ws = wb.AddWorksheet("Sheet1"); | ||
| ws.Cell("A1").Value = "{{title}}"; | ||
| ws.Cell("A3").Value = "{{items.Name}}"; | ||
| ws.Cell("B3").Value = "{{items.Qty}}"; | ||
| ws.Cell("B5").FormulaA1 = "SUM(B3:B4)"; | ||
| wb.SaveAs(template.FilePath); | ||
| } | ||
|
|
||
| using var path = AutoDeletingPath.Create(); | ||
| Dictionary<string, object?> data = new() | ||
| { | ||
| ["title"] = "FooCompany", | ||
| ["items"] = new[] | ||
| { | ||
| new { Name = "A", Qty = 1 }, | ||
| new { Name = "B", Qty = 2 }, | ||
| } | ||
| }; | ||
| await _templater.FillTemplateAsync(path.ToString(), template.FilePath, data); | ||
|
|
||
| using var zip = ZipFile.OpenRead(path.ToString()); | ||
| var calcChain = zip.GetEntry("xl/calcChain.xml"); | ||
| if (calcChain != null) | ||
| { | ||
| using var reader = new StreamReader(calcChain.Open()); | ||
| var content = await reader.ReadToEndAsync(); | ||
| Assert.Contains("<c ", content); // an empty calcChain is schema-invalid | ||
| Assert.DoesNotContain(@"r=""B5""", content); // the pre-render address is stale after the row shift | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DollarFormulaInSparseRow_WritesValidFormulaCellAndCorrectCalcChainRef() | ||
| { | ||
| // The '$=' formula sits in column D of a row whose only other cell is in column A, so the | ||
| // formula cell's child-list position (1) differs from its column (D) — the calcChain ref | ||
| // must come from the cell's own address. The rendered cell must be a real formula element | ||
| // in the spreadsheetml namespace, not an inline string. | ||
| using var template = AutoDeletingPath.Create(); | ||
| using (var wb = new XLWorkbook()) | ||
| { | ||
| var ws = wb.AddWorksheet("Sheet1"); | ||
| // the static formula makes the authoring library emit a calcChain part, so the | ||
| // regeneration path runs; without one the template carries no chain to regenerate | ||
| ws.Cell("F1").FormulaA1 = "1+1"; | ||
| ws.Cell("A5").Value = "{{items.Name}}"; | ||
| ws.Cell("B5").Value = "{{items.Qty}}"; | ||
| ws.Cell("A7").Value = "Total"; | ||
| ws.Cell("D7").Value = "$=SUM(B{{$enumrowstart}}:B{{$enumrowend}})"; | ||
| wb.SaveAs(template.FilePath); | ||
| } | ||
|
|
||
| using var path = AutoDeletingPath.Create(); | ||
| Dictionary<string, object?> data = new() | ||
| { | ||
| ["title"] = "FooCompany", | ||
| ["items"] = new[] | ||
| { | ||
| new { Name = "A", Qty = 1 }, | ||
| new { Name = "B", Qty = 2 }, | ||
| } | ||
| }; | ||
| await _templater.FillTemplateAsync(path.ToString(), template.FilePath, data); | ||
|
|
||
| using var zip = ZipFile.OpenRead(path.ToString()); | ||
|
|
||
| // the formula cell: <c r="D8"> (two items shift row 7 to 8) with a namespaced <f> child and no inlineStr type | ||
| XDocument doc; | ||
| await using (var sheet = zip.GetEntry("xl/worksheets/sheet1.xml")!.Open()) | ||
| { | ||
| doc = await XDocument.LoadAsync(sheet, System.Xml.Linq.LoadOptions.None, CancellationToken.None); | ||
| } | ||
|
|
||
| var ns = (XNamespace)Schemas.SpreadsheetmlXmlMain; | ||
| var formulaCell = doc.Descendants(ns + "c").FirstOrDefault(x => x.Attribute("r")?.Value == "D8"); | ||
| Assert.NotNull(formulaCell); | ||
| Assert.Equal("SUM(B5:B6)", formulaCell.Element(ns + "f")?.Value); | ||
| Assert.NotEqual("inlineStr", formulaCell.Attribute("t")?.Value); | ||
|
|
||
| // the regenerated calcChain points at the formula's real address, not the one derived | ||
| // from the cell's position in the row (which would be column B here) | ||
| using var chainReader = new StreamReader(zip.GetEntry("xl/calcChain.xml")!.Open()); | ||
| var chain = await chainReader.ReadToEndAsync(); | ||
| Assert.Contains(@"r=""D8""", chain); | ||
| Assert.DoesNotContain(@"r=""B8""", chain); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.