Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Height and HeightRange option. #360

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ClosedXML.Report/Options/HeightRangeTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Linq;
using ClosedXML.Report.Utils;

namespace ClosedXML.Report.Options
{
public class HeightRangeTag : RangeOptionTag
{
public int Height => Parameters.Any() ? Parameters.First().Key.AsInt() : 0;

public override void Execute(ProcessingContext context)
{
var firstRow = context.Range.FirstRowUsed();
var lastRow = context.Range.LastRowUsed();

Range.Worksheet.Rows(firstRow.WorksheetRow().RowNumber(), lastRow.WorksheetRow().RowNumber()).Height = Height;
}
}
}
17 changes: 17 additions & 0 deletions ClosedXML.Report/Options/HeightTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq;
using ClosedXML.Report.Utils;

namespace ClosedXML.Report.Options;

public class HeightTag : OptionTag
{
public int Height => Parameters.Any() ? Parameters.First().Key.AsInt() : 0;

public override void Execute(ProcessingContext context)
{
var xlCell = Cell.GetXlCell(context.Range);
var cellRow = xlCell.WorksheetRow().RowNumber();

Range.Worksheet.Row(cellRow).Height = Height;
}
}
17 changes: 13 additions & 4 deletions ClosedXML.Report/RangeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ private static IEnumerable<IXLNamedRange> GetInnerRanges(IXLRange prng)
public IReportBuffer Generate(object[] items)
{
_evaluator.AddVariable("items", items);

foreach (var v in _globalVariables)
{
_evaluator.AddVariable("@" + v.Key, v.Value);
}
_rangeTags.Reset();


if (IsHorizontal)
{
HorizontalTable(items, _evaluator);
Expand All @@ -160,6 +162,7 @@ public IReportBuffer Generate(object[] items)
{
VerticalTable(items, _evaluator);
}

return _buff;
}

Expand Down Expand Up @@ -482,10 +485,16 @@ where TagExtensions.HasTag(value)
tags = _tagsEvaluator.Parse(cell.GetString(), range, cell, out newValue);
cell.Value = newValue;
}
if (cell.Row > 1 && cell.Row == _rowCnt)
_rangeTags.AddRange(tags);
else
_tags.AddRange(tags);

foreach (var optionTag in tags)
{
if (cell.Row > 1 && cell.Row == _rowCnt)
_rangeTags.Add(optionTag);
else if (optionTag is RangeOptionTag)
_rangeTags.Add(optionTag);
else
_tags.Add(optionTag);
}
}

_rangeOption = _rangeTags.GetAll<RangeOptionTag>().Union(_tags.GetAll<RangeOptionTag>()).FirstOrDefault();
Expand Down
2 changes: 2 additions & 0 deletions ClosedXML.Report/XLTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ static XLTemplate()
TagsRegister.Add<HiddenTag>("Hide", 0);
TagsRegister.Add<PageOptionsTag>("PageOptions", 0);
TagsRegister.Add<ProtectedTag>("Protected", 0);
TagsRegister.Add<HeightTag>("Height", 0);
TagsRegister.Add<HeightRangeTag>("HeightRange", 0);
}

public XLTemplate(string fileName) : this(new XLWorkbook(fileName))
Expand Down
52 changes: 52 additions & 0 deletions tests/ClosedXML.Report.Tests/HeightTagTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Linq;
using ClosedXML.Report.Tests.TestModels;
using Xunit;
using Xunit.Abstractions;

namespace ClosedXML.Report.Tests;

[Collection("Database")]
public class HeightTagTests : XlsxTemplateTestsBase
{
public HeightTagTests(ITestOutputHelper output) : base(output)
{
}

[Theory,
InlineData("HeightTag.xlsx")
]
public void Height(string templateFile)
{
XlTemplateTest(templateFile,
tpl =>
{

},
wb =>
{
CompareWithGauge(wb, templateFile);
});
}

[Theory,
InlineData("HeightRangeTag.xlsx")
]
public void HeightRange(string templateFile)
{
XlTemplateTest(templateFile,
tpl =>
{
using var db = new DbDemos();
var cust = db.employees.Take(2).ToList();
var dataTable = new
{
Table = cust
};
tpl.AddVariable(dataTable);
},
wb =>
{
CompareWithGauge(wb, templateFile);
});
}
}
Binary file added tests/Gauges/HeightRangeTag.xlsx
Binary file not shown.
Binary file added tests/Gauges/HeightTag.xlsx
Binary file not shown.
Binary file added tests/Templates/HeightRangeTag.xlsx
Binary file not shown.
Binary file added tests/Templates/HeightTag.xlsx
Binary file not shown.
Loading