|
| 1 | +using System.Xml.Linq; |
| 2 | +using ClosedXML.Excel; |
| 3 | +using MiniExcelLib.OpenXml.Constants; |
| 4 | +using MiniExcelLib.Tests.Common.Utils; |
| 5 | + |
| 6 | +namespace MiniExcelLib.OpenXml.Tests.Templates; |
| 7 | + |
| 8 | +public class CalcChainAsyncTests |
| 9 | +{ |
| 10 | + private readonly OpenXmlTemplater _templater = MiniExcel.Templaters.GetOpenXmlTemplater(); |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task TemplateWithStaticFormula_DoesNotWriteStaleOrEmptyCalcChain() |
| 14 | + { |
| 15 | + // A template with a static Excel formula (below an IEnumerable row) carries a calcChain |
| 16 | + // pointing at the formula's pre-render address. After rows are inserted the address is |
| 17 | + // stale — the rendered package must not contain a stale or empty calcChain. |
| 18 | + using var template = AutoDeletingPath.Create(); |
| 19 | + using (var wb = new XLWorkbook()) |
| 20 | + { |
| 21 | + var ws = wb.AddWorksheet("Sheet1"); |
| 22 | + ws.Cell("A1").Value = "{{title}}"; |
| 23 | + ws.Cell("A3").Value = "{{items.Name}}"; |
| 24 | + ws.Cell("B3").Value = "{{items.Qty}}"; |
| 25 | + ws.Cell("B5").FormulaA1 = "SUM(B3:B4)"; |
| 26 | + wb.SaveAs(template.FilePath); |
| 27 | + } |
| 28 | + |
| 29 | + using var path = AutoDeletingPath.Create(); |
| 30 | + Dictionary<string, object?> data = new() |
| 31 | + { |
| 32 | + ["title"] = "FooCompany", |
| 33 | + ["items"] = new[] |
| 34 | + { |
| 35 | + new { Name = "A", Qty = 1 }, |
| 36 | + new { Name = "B", Qty = 2 }, |
| 37 | + } |
| 38 | + }; |
| 39 | + await _templater.FillTemplateAsync(path.ToString(), template.FilePath, data); |
| 40 | + |
| 41 | + using var zip = ZipFile.OpenRead(path.ToString()); |
| 42 | + var calcChain = zip.GetEntry("xl/calcChain.xml"); |
| 43 | + if (calcChain != null) |
| 44 | + { |
| 45 | + using var reader = new StreamReader(calcChain.Open()); |
| 46 | + var content = await reader.ReadToEndAsync(); |
| 47 | + Assert.Contains("<c ", content); // an empty calcChain is schema-invalid |
| 48 | + Assert.DoesNotContain(@"r=""B5""", content); // the pre-render address is stale after the row shift |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task DollarFormulaInSparseRow_WritesValidFormulaCellAndCorrectCalcChainRef() |
| 54 | + { |
| 55 | + // The '$=' formula sits in column D of a row whose only other cell is in column A, so the |
| 56 | + // formula cell's child-list position (1) differs from its column (D) — the calcChain ref |
| 57 | + // must come from the cell's own address. The rendered cell must be a real formula element |
| 58 | + // in the spreadsheetml namespace, not an inline string. |
| 59 | + using var template = AutoDeletingPath.Create(); |
| 60 | + using (var wb = new XLWorkbook()) |
| 61 | + { |
| 62 | + var ws = wb.AddWorksheet("Sheet1"); |
| 63 | + // the static formula makes the authoring library emit a calcChain part, so the |
| 64 | + // regeneration path runs; without one the template carries no chain to regenerate |
| 65 | + ws.Cell("F1").FormulaA1 = "1+1"; |
| 66 | + ws.Cell("A5").Value = "{{items.Name}}"; |
| 67 | + ws.Cell("B5").Value = "{{items.Qty}}"; |
| 68 | + ws.Cell("A7").Value = "Total"; |
| 69 | + ws.Cell("D7").Value = "$=SUM(B{{$enumrowstart}}:B{{$enumrowend}})"; |
| 70 | + wb.SaveAs(template.FilePath); |
| 71 | + } |
| 72 | + |
| 73 | + using var path = AutoDeletingPath.Create(); |
| 74 | + Dictionary<string, object?> data = new() |
| 75 | + { |
| 76 | + ["title"] = "FooCompany", |
| 77 | + ["items"] = new[] |
| 78 | + { |
| 79 | + new { Name = "A", Qty = 1 }, |
| 80 | + new { Name = "B", Qty = 2 }, |
| 81 | + } |
| 82 | + }; |
| 83 | + await _templater.FillTemplateAsync(path.ToString(), template.FilePath, data); |
| 84 | + |
| 85 | + using var zip = ZipFile.OpenRead(path.ToString()); |
| 86 | + |
| 87 | + // the formula cell: <c r="D8"> (two items shift row 7 to 8) with a namespaced <f> child and no inlineStr type |
| 88 | + XDocument doc; |
| 89 | + await using (var sheet = zip.GetEntry("xl/worksheets/sheet1.xml")!.Open()) |
| 90 | + { |
| 91 | + doc = await XDocument.LoadAsync(sheet, System.Xml.Linq.LoadOptions.None, CancellationToken.None); |
| 92 | + } |
| 93 | + |
| 94 | + var ns = (XNamespace)Schemas.SpreadsheetmlXmlMain; |
| 95 | + var formulaCell = doc.Descendants(ns + "c").FirstOrDefault(x => x.Attribute("r")?.Value == "D8"); |
| 96 | + Assert.NotNull(formulaCell); |
| 97 | + Assert.Equal("SUM(B5:B6)", formulaCell.Element(ns + "f")?.Value); |
| 98 | + Assert.NotEqual("inlineStr", formulaCell.Attribute("t")?.Value); |
| 99 | + |
| 100 | + // the regenerated calcChain points at the formula's real address, not the one derived |
| 101 | + // from the cell's position in the row (which would be column B here) |
| 102 | + using var chainReader = new StreamReader(zip.GetEntry("xl/calcChain.xml")!.Open()); |
| 103 | + var chain = await chainReader.ReadToEndAsync(); |
| 104 | + Assert.Contains(@"r=""D8""", chain); |
| 105 | + Assert.DoesNotContain(@"r=""B8""", chain); |
| 106 | + } |
| 107 | +} |
0 commit comments