Skip to content

Commit 6772ecc

Browse files
authored
Static pivot tables - (blank) row and column #230 (#232)
1 parent de89a78 commit 6772ecc

File tree

10 files changed

+28
-8
lines changed

10 files changed

+28
-8
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Linq;
2+
using ClosedXML.Excel;
3+
4+
namespace ClosedXML.Report.Options
5+
{
6+
public static class TagExtensions
7+
{
8+
public static bool HasTag(string value)
9+
{
10+
return value.StartsWith("<<") || value.EndsWith(">>");
11+
}
12+
13+
public static bool IsOptionsRowEmpty(this IXLRange range)
14+
{
15+
return !range.LastRow().CellsUsed(XLCellsUsedOptions.AllContents | XLCellsUsedOptions.MergedRanges).Any();
16+
}
17+
}
18+
}

ClosedXML.Report/RangeInterpreter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ParseTags(IXLRange range, string rangeName)
4545
.ToArray();
4646
var cells = from c in cellsUsed
4747
let value = c.GetString()
48-
where (value.StartsWith("<<") || value.EndsWith(">>"))
48+
where TagExtensions.HasTag(value)
4949
select c;
5050

5151
if (!_tags.ContainsKey(rangeName))
@@ -183,6 +183,9 @@ string EvalString(string str)
183183
nr.NamedRange.SetRefersTo(ranges);
184184

185185
tplt.RangeTagsApply(trgtRng, items);
186+
var isOptionsRowEmpty = trgtRng.IsOptionsRowEmpty();
187+
if (isOptionsRowEmpty)
188+
trgtRng.LastRow().Delete(XLShiftDeletedCells.ShiftCellsUp);
186189
}
187190

188191
// refresh ranges for pivot tables

ClosedXML.Report/RangeTemplate.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class RangeTemplate
2727
private TempSheetBuffer _buff;
2828
private IXLRange _rowRange;
2929
private IXLRangeRow _optionsRow;
30-
private bool _optionsRowIsEmpty = true;
30+
private bool _isOptionsRowEmpty = true;
3131
private bool _isSubrange;
3232
private IDictionary<string, object> _globalVariables;
3333

@@ -107,7 +107,7 @@ private static RangeTemplate Parse(string name, IXLRange range, TempSheetBuffer
107107

108108
result._rowRange = range.Offset(0, 0, result._rowCnt, result._colCnt);
109109
result._optionsRow = range.LastRow();
110-
result._optionsRowIsEmpty = !result._optionsRow.CellsUsed(XLCellsUsedOptions.AllContents | XLCellsUsedOptions.MergedRanges).Any();
110+
result._isOptionsRowEmpty = range.IsOptionsRowEmpty();
111111
}
112112

113113
result._subranges = innerRanges.SelectMany(nrng => nrng.Ranges,
@@ -219,7 +219,7 @@ private void VerticalTable(object[] items, FormulaEvaluator evaluator)
219219
}
220220

221221
// Render options row
222-
if (!_optionsRowIsEmpty)
222+
if (!_isOptionsRowEmpty)
223223
{
224224
foreach (var cell in _cells.Where(c => c.Row == _rowCnt + 1).OrderBy(c => c.Column))
225225
{
@@ -230,7 +230,7 @@ private void VerticalTable(object[] items, FormulaEvaluator evaluator)
230230

231231
// Execute range options tags
232232
var resultRange = _buff.GetRange(rangeStart, _buff.PrevAddress);
233-
if (!_optionsRowIsEmpty)
233+
if (!_isOptionsRowEmpty)
234234
{
235235
var optionsRow = resultRange.LastRow().AsRange();
236236
foreach (var mrg in _mergedRanges.Where(r => _optionsRow.Contains(r)))
@@ -425,8 +425,8 @@ private void ParseTags(IXLRange range)
425425
var innerRanges = range.GetContainingNames().ToArray();
426426
var cells = from c in _cells
427427
let value = c.GetString()
428-
where (value.StartsWith("<<") || value.EndsWith(">>"))
429-
&& !innerRanges.Any(nr => nr.Ranges.Contains(c.XLCell.AsRange()))
428+
where TagExtensions.HasTag(value)
429+
&& !innerRanges.Any(nr => nr.Ranges.Contains(c.XLCell.AsRange()))
430430
select c;
431431

432432
foreach (var cell in cells)

tests/ClosedXML.Report.Tests/XlTemplateTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public void Add_enumerable_variable_should_fill_range()
8787
}),
8888
wb =>
8989
{
90-
wb.SaveAs("Output\\4.xlsx");
9190
var sheet = wb.Worksheet(1);
9291
sheet.Cell("G1").GetValue<string>().Should().Be("title from test");
9392
sheet.Cell("B4").GetValue<string>().Should().Be("1");

tests/Gauges/Subranges_Multiple.xlsx

10 Bytes
Binary file not shown.
642 Bytes
Binary file not shown.

tests/Gauges/tPivot1.xlsx

515 Bytes
Binary file not shown.

tests/Gauges/tPivot5_Static.xlsx

72 Bytes
Binary file not shown.
530 Bytes
Binary file not shown.

tests/Templates/tPivot1.xlsx

642 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)