Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,8 @@ private void AddMissingMergeAnchorCells(XElement sheetData)

private static IEnumerable<ConditionalFormatRange> 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 <conditionalFormatting>, the <cfRule> 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)
Expand Down
53 changes: 53 additions & 0 deletions tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object?> { ["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<string, object?> { ["items"] = items });

Assert.Equal([("A2:A4", "\"b\""), ("C5:C5", "1")], GetConditionalFormats(filled));
}
}
Loading