Skip to content

Commit

Permalink
fix: clear table placeholders when data is empty (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
smakaiberriesandco authored Apr 11, 2024
1 parent f21cf97 commit 7dbb3bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ClosedXML.Report/RangeInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/ClosedXML.Report.Tests/RangeInterpreterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ClosedXML.Excel;
using FluentAssertions;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -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<Item>()
};

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()
{
Expand Down

0 comments on commit 7dbb3bb

Please sign in to comment.