Skip to content

Commit a38eda3

Browse files
authored
Forcibly remove a range representing a table if its data source is empty (#251) (#323)
1 parent 2193707 commit a38eda3

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

ClosedXML.Report/RangeInterpreter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ string EvalString(string str)
177177
{
178178
var growedRange = rng.GrowToMergedRanges();
179179
var items = nr.RangeData as object[] ?? nr.RangeData.Cast<object>().ToArray();
180+
if (!items.Any())
181+
{
182+
if (growedRange.IsOptionsRowEmpty())
183+
{
184+
growedRange.Delete(XLShiftDeletedCells.ShiftCellsUp);
185+
}
186+
else
187+
{
188+
var rangeWithoutOptionsRow = growedRange.Worksheet
189+
.Range(growedRange.FirstCell(), growedRange.LastCell().CellAbove());
190+
rangeWithoutOptionsRow.Delete(XLShiftDeletedCells.ShiftCellsUp);
191+
}
192+
continue;
193+
}
180194
var tplt = RangeTemplate.Parse(nr.NamedRange.Name, growedRange, _errors, _variables);
181195
using (var buff = tplt.Generate(items))
182196
{

tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ public void ShouldNotThrowExceptionIfAccessSomeChildrenNullProp()
104104
ws.Cell("B4").GetString().Should().Be("Material 1");
105105
}
106106

107+
[Fact]
108+
public void ShouldDestroyEmptyTable()
109+
{
110+
//See #251
111+
var template = CreateOrderTemplate();
112+
var ws = template.Workbook.Worksheets.First();
113+
114+
ws.Cell("B4").SetValue("This list is empty");
115+
ws.Cell("B5").SetValue("{{item.Name}}");
116+
ws.Range("A5:B6").AddToNamed("Empty");
117+
118+
ws.Cell("B7").SetValue("This list is populated");
119+
ws.Cell("B8").SetValue("{{item.Name}}");
120+
ws.Range("A8:B9").AddToNamed("Populated");
121+
122+
var model = new
123+
{
124+
Empty = new List<Item>(),
125+
Populated = new[]
126+
{
127+
new Item("It works", null)
128+
}
129+
};
130+
template.AddVariable(model);
131+
template.Generate();
132+
133+
ws.Cell("B4").GetString().Should().Be("This list is empty");
134+
ws.Cell("B5").GetString().Should().Be("This list is populated");
135+
ws.Cell("B6").GetString().Should().Be("It works");
136+
}
137+
107138
private XLTemplate CreateOrderTemplate()
108139
{
109140
var wbTemplate = new XLWorkbook();

tests/ClosedXML.Report.Tests/TempSheetBufferTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void CanRenderRangeForEmptySet()
4141
template.AddVariable("List", new List<string>());
4242
template.Generate();
4343

44-
ws.Cell("B3").GetString().Should().Be("Cell below");
45-
ws.Cell("B4").GetString().Should().Be("");
44+
ws.Cell("B2").GetString().Should().Be("Cell below");
45+
ws.Cell("B3").GetString().Should().Be("");
4646
}
4747
}
4848

0 commit comments

Comments
 (0)