diff --git a/ClosedXML.Report/RangeInterpreter.cs b/ClosedXML.Report/RangeInterpreter.cs index 0f5d657..f9bb253 100644 --- a/ClosedXML.Report/RangeInterpreter.cs +++ b/ClosedXML.Report/RangeInterpreter.cs @@ -187,7 +187,10 @@ string EvalString(string str) { var rangeWithoutOptionsRow = growedRange.Worksheet .Range(growedRange.FirstCell(), growedRange.LastCell().CellAbove()); - rangeWithoutOptionsRow.Delete(XLShiftDeletedCells.ShiftCellsUp); + if (growedRange.Worksheet.Tables.Any(t => t.Contains(rangeWithoutOptionsRow))) + growedRange.Clear(); + else + rangeWithoutOptionsRow.Delete(XLShiftDeletedCells.ShiftCellsUp); } continue; } diff --git a/tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs b/tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs index ea848ca..1ed6651 100644 --- a/tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs +++ b/tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs @@ -1,6 +1,7 @@ using ClosedXML.Excel; using FluentAssertions; using System.Collections.Generic; +using System.Data; using System.Linq; using Xunit; using Xunit.Abstractions; @@ -104,6 +105,42 @@ public void ShouldNotThrowExceptionIfAccessSomeChildrenNullProp() ws.Cell("B4").GetString().Should().Be("Material 1"); } + [Fact] + public void ShouldClearPlaceHolders() + { + var wbTemplate = new XLWorkbook(); + var ws = wbTemplate.AddWorksheet(); + var template = new XLTemplate(wbTemplate); + + ws.Cell("B8").InsertTable(new DataTable() + { + Columns = + { + "Id", + "Name" + }, + Rows = + { + { "{{item.Id}}", "{{item.Name}}" } + } + }); + ws.Range("B9:C9").AddToNamed("TestRange"); + + var model = new + { + TestRange = new List() + }; + + template.AddVariable(model); + + template.Generate(); + + ws.Cell("B8").GetString().Should().Be("Id"); + ws.Cell("C8").GetString().Should().Be("Name"); + ws.Cell("B9").GetString().Should().BeEmpty(); + ws.Cell("C9").GetString().Should().BeEmpty(); + } + [Fact] public void ShouldDestroyEmptyTable() {