From dc49f7fcbc86f7470bd7cab4ac3cd88e4a83cc47 Mon Sep 17 00:00:00 2001 From: vtxe02023274 <344562942+vtxe02023274@users.noreply.github.com> Date: Fri, 4 Dec 2026 02:41:10 +0800 Subject: [PATCH] Keep conditional formatting when filling a template ParseConditionalFormatRanges read the children of the first (the elements) and looked for sqref on them, so no range was found and every conditional format of the template was dropped. Read sqref from each as before the move to XDocument (#946). Fixes #1082 --- .../Templates/OpenXmlTemplate.Impl.cs | 7 +-- .../Issues/MiniExcelGithubIssuesTests.cs | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs b/src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs index ef47c999..62c0acd3 100644 --- a/src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs +++ b/src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs @@ -219,11 +219,8 @@ private void AddMissingMergeAnchorCells(XElement sheetData) private static IEnumerable ParseConditionalFormatRanges(XElement worksheet) { - var conditionalFormatting = worksheet.Element(SpreadsheetNs + "conditionalFormatting"); - if (conditionalFormatting is null) - yield break; - - foreach (var format in conditionalFormatting.Elements()) + // sqref is on each , the elements inside it only hold the rules + foreach (var format in worksheet.Elements(SpreadsheetNs + "conditionalFormatting")) { var ranges = format.Attribute("sqref")?.Value.Split(' '); if (ranges is null) diff --git a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs index 6187f8bd..f0f0c07b 100644 --- a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs +++ b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs @@ -3638,4 +3638,57 @@ public void TestIssue1078_FillTemplateShouldNotAddRowsInsideGroup(int mergeRow, var names = _excelImporter.Query(filled).Select(r => (string?)r.A).Where(v => !string.IsNullOrEmpty(v)); Assert.Equal(["a", "b"], names); } + + private static List<(string Sqref, string Formula)> GetConditionalFormats(Stream filled) + { + filled.Seek(0, SeekOrigin.Begin); + using var zip = new ZipArchive(filled, ZipArchiveMode.Read, leaveOpen: true); + using var stream = zip.GetEntry("xl/worksheets/sheet1.xml")!.Open(); + XNamespace ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; + return XDocument.Load(stream).Descendants(ns + "conditionalFormatting") + .Select(cf => (cf.Attribute("sqref")!.Value, cf.Element(ns + "cfRule")!.Element(ns + "formula")!.Value)) + .OrderBy(cf => cf.Item1) + .ToList(); + } + + [Fact] + public void TestIssue1082_FillTemplateShouldKeepConditionalFormatting() + { + using var template = new MemoryStream(); + using (var wb = new XLWorkbook()) + { + var ws = wb.AddWorksheet("Sheet1"); + ws.Cell("A1").Value = "{{name}}"; + ws.Cell("B1").Value = "{{answer}}"; + ws.Range("B1:B10").AddConditionalFormat().WhenEquals("\"Yes\"").Fill.SetBackgroundColor(XLColor.Red); + ws.Range("C1:D2").AddConditionalFormat().WhenEquals("1").Fill.SetBackgroundColor(XLColor.Blue); + wb.SaveAs(template); + } + + using var filled = new MemoryStream(); + _excelTemplater.FillTemplate(filled, template, new Dictionary { ["name"] = "Jack", ["answer"] = "Yes" }); + + Assert.Equal([("B1:B10", "\"Yes\""), ("C1:D2", "1")], GetConditionalFormats(filled)); + } + + [Fact] + public void TestIssue1082_FillTemplateShouldExtendConditionalFormattingOverListRows() + { + using var template = new MemoryStream(); + using (var wb = new XLWorkbook()) + { + var ws = wb.AddWorksheet("Sheet1"); + ws.Cell("A1").Value = "Name"; + ws.Cell("A2").Value = "{{items.name}}"; + ws.Range("A2").AddConditionalFormat().WhenEquals("\"b\"").Fill.SetBackgroundColor(XLColor.Red); + ws.Range("C5").AddConditionalFormat().WhenEquals("1").Fill.SetBackgroundColor(XLColor.Blue); + wb.SaveAs(template); + } + + using var filled = new MemoryStream(); + var items = new[] { new { name = "a" }, new { name = "b" }, new { name = "c" } }; + _excelTemplater.FillTemplate(filled, template, new Dictionary { ["items"] = items }); + + Assert.Equal([("A2:A4", "\"b\""), ("C5:C5", "1")], GetConditionalFormats(filled)); + } }