Skip to content

Commit 7dbb3bb

Browse files
fix: clear table placeholders when data is empty (#344)
1 parent f21cf97 commit 7dbb3bb

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

ClosedXML.Report/RangeInterpreter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ string EvalString(string str)
187187
{
188188
var rangeWithoutOptionsRow = growedRange.Worksheet
189189
.Range(growedRange.FirstCell(), growedRange.LastCell().CellAbove());
190-
rangeWithoutOptionsRow.Delete(XLShiftDeletedCells.ShiftCellsUp);
190+
if (growedRange.Worksheet.Tables.Any(t => t.Contains(rangeWithoutOptionsRow)))
191+
growedRange.Clear();
192+
else
193+
rangeWithoutOptionsRow.Delete(XLShiftDeletedCells.ShiftCellsUp);
191194
}
192195
continue;
193196
}

tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ClosedXML.Excel;
22
using FluentAssertions;
33
using System.Collections.Generic;
4+
using System.Data;
45
using System.Linq;
56
using Xunit;
67
using Xunit.Abstractions;
@@ -104,6 +105,42 @@ public void ShouldNotThrowExceptionIfAccessSomeChildrenNullProp()
104105
ws.Cell("B4").GetString().Should().Be("Material 1");
105106
}
106107

108+
[Fact]
109+
public void ShouldClearPlaceHolders()
110+
{
111+
var wbTemplate = new XLWorkbook();
112+
var ws = wbTemplate.AddWorksheet();
113+
var template = new XLTemplate(wbTemplate);
114+
115+
ws.Cell("B8").InsertTable(new DataTable()
116+
{
117+
Columns =
118+
{
119+
"Id",
120+
"Name"
121+
},
122+
Rows =
123+
{
124+
{ "{{item.Id}}", "{{item.Name}}" }
125+
}
126+
});
127+
ws.Range("B9:C9").AddToNamed("TestRange");
128+
129+
var model = new
130+
{
131+
TestRange = new List<Item>()
132+
};
133+
134+
template.AddVariable(model);
135+
136+
template.Generate();
137+
138+
ws.Cell("B8").GetString().Should().Be("Id");
139+
ws.Cell("C8").GetString().Should().Be("Name");
140+
ws.Cell("B9").GetString().Should().BeEmpty();
141+
ws.Cell("C9").GetString().Should().BeEmpty();
142+
}
143+
107144
[Fact]
108145
public void ShouldDestroyEmptyTable()
109146
{

0 commit comments

Comments
 (0)