Skip to content

Commit a428fa4

Browse files
masterworgenMalov Gleb
and
Malov Gleb
authored
Add Height and HeightRange option. (#360)
Co-authored-by: Malov Gleb <gleb.malov@arsis.ru>
1 parent dbdd97b commit a428fa4

File tree

9 files changed

+102
-4
lines changed

9 files changed

+102
-4
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.Report.Utils;
3+
4+
namespace ClosedXML.Report.Options
5+
{
6+
public class HeightRangeTag : RangeOptionTag
7+
{
8+
public int Height => Parameters.Any() ? Parameters.First().Key.AsInt() : 0;
9+
10+
public override void Execute(ProcessingContext context)
11+
{
12+
var firstRow = context.Range.FirstRowUsed();
13+
var lastRow = context.Range.LastRowUsed();
14+
15+
Range.Worksheet.Rows(firstRow.WorksheetRow().RowNumber(), lastRow.WorksheetRow().RowNumber()).Height = Height;
16+
}
17+
}
18+
}

ClosedXML.Report/Options/HeightTag.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Linq;
2+
using ClosedXML.Report.Utils;
3+
4+
namespace ClosedXML.Report.Options;
5+
6+
public class HeightTag : OptionTag
7+
{
8+
public int Height => Parameters.Any() ? Parameters.First().Key.AsInt() : 0;
9+
10+
public override void Execute(ProcessingContext context)
11+
{
12+
var xlCell = Cell.GetXlCell(context.Range);
13+
var cellRow = xlCell.WorksheetRow().RowNumber();
14+
15+
Range.Worksheet.Row(cellRow).Height = Height;
16+
}
17+
}

ClosedXML.Report/RangeTemplate.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,14 @@ private static IEnumerable<IXLNamedRange> GetInnerRanges(IXLRange prng)
146146
public IReportBuffer Generate(object[] items)
147147
{
148148
_evaluator.AddVariable("items", items);
149+
149150
foreach (var v in _globalVariables)
150151
{
151152
_evaluator.AddVariable("@" + v.Key, v.Value);
152153
}
153154
_rangeTags.Reset();
154155

156+
155157
if (IsHorizontal)
156158
{
157159
HorizontalTable(items, _evaluator);
@@ -160,6 +162,7 @@ public IReportBuffer Generate(object[] items)
160162
{
161163
VerticalTable(items, _evaluator);
162164
}
165+
163166
return _buff;
164167
}
165168

@@ -482,10 +485,16 @@ where TagExtensions.HasTag(value)
482485
tags = _tagsEvaluator.Parse(cell.GetString(), range, cell, out newValue);
483486
cell.Value = newValue;
484487
}
485-
if (cell.Row > 1 && cell.Row == _rowCnt)
486-
_rangeTags.AddRange(tags);
487-
else
488-
_tags.AddRange(tags);
488+
489+
foreach (var optionTag in tags)
490+
{
491+
if (cell.Row > 1 && cell.Row == _rowCnt)
492+
_rangeTags.Add(optionTag);
493+
else if (optionTag is RangeOptionTag)
494+
_rangeTags.Add(optionTag);
495+
else
496+
_tags.Add(optionTag);
497+
}
489498
}
490499

491500
_rangeOption = _rangeTags.GetAll<RangeOptionTag>().Union(_tags.GetAll<RangeOptionTag>()).FirstOrDefault();

ClosedXML.Report/XLTemplate.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static XLTemplate()
5858
TagsRegister.Add<HiddenTag>("Hide", 0);
5959
TagsRegister.Add<PageOptionsTag>("PageOptions", 0);
6060
TagsRegister.Add<ProtectedTag>("Protected", 0);
61+
TagsRegister.Add<HeightTag>("Height", 0);
62+
TagsRegister.Add<HeightRangeTag>("HeightRange", 0);
6163
}
6264

6365
public XLTemplate(string fileName) : this(new XLWorkbook(fileName))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Linq;
2+
using ClosedXML.Report.Tests.TestModels;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace ClosedXML.Report.Tests;
7+
8+
[Collection("Database")]
9+
public class HeightTagTests : XlsxTemplateTestsBase
10+
{
11+
public HeightTagTests(ITestOutputHelper output) : base(output)
12+
{
13+
}
14+
15+
[Theory,
16+
InlineData("HeightTag.xlsx")
17+
]
18+
public void Height(string templateFile)
19+
{
20+
XlTemplateTest(templateFile,
21+
tpl =>
22+
{
23+
24+
},
25+
wb =>
26+
{
27+
CompareWithGauge(wb, templateFile);
28+
});
29+
}
30+
31+
[Theory,
32+
InlineData("HeightRangeTag.xlsx")
33+
]
34+
public void HeightRange(string templateFile)
35+
{
36+
XlTemplateTest(templateFile,
37+
tpl =>
38+
{
39+
using var db = new DbDemos();
40+
var cust = db.employees.Take(2).ToList();
41+
var dataTable = new
42+
{
43+
Table = cust
44+
};
45+
tpl.AddVariable(dataTable);
46+
},
47+
wb =>
48+
{
49+
CompareWithGauge(wb, templateFile);
50+
});
51+
}
52+
}

tests/Gauges/HeightRangeTag.xlsx

7.92 KB
Binary file not shown.

tests/Gauges/HeightTag.xlsx

8.26 KB
Binary file not shown.

tests/Templates/HeightRangeTag.xlsx

8.41 KB
Binary file not shown.

tests/Templates/HeightTag.xlsx

8.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)