Skip to content

Commit dbdd97b

Browse files
authored
Add disabled parameter to "Delete" tag and evaluate values before parsing (#358)
* Evaluate values before parsing tests and add "enabled" parameter to Delete tag
1 parent 7dbb3bb commit dbdd97b

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

ClosedXML.Report/Options/DeleteTag.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ OPTION OBJECTS
88

99
using ClosedXML.Excel;
1010
using ClosedXML.Report.Excel;
11+
using ClosedXML.Report.Utils;
1112
using System.Linq;
1213

1314
namespace ClosedXML.Report.Options
1415
{
1516
public class DeleteTag : OptionTag
1617
{
18+
public const string DisabledParameter = "disabled";
19+
1720
public override void Execute(ProcessingContext context)
1821
{
1922
var deleteTags = List.GetAll<DeleteTag>()
@@ -23,6 +26,12 @@ public override void Execute(ProcessingContext context)
2326

2427
foreach (var tag in deleteTags)
2528
{
29+
if (IsDisabled(tag))
30+
{
31+
tag.Enabled = false;
32+
continue;
33+
}
34+
2635
var xlCell = tag.Cell.GetXlCell(context.Range);
2736
var cellAddr = xlCell.Address.ToStringRelative(false);
2837
var ws = Range.Worksheet;
@@ -52,5 +61,10 @@ public override void Execute(ProcessingContext context)
5261
tag.Enabled = false;
5362
}
5463
}
64+
65+
private bool IsDisabled(DeleteTag tag)
66+
{
67+
return tag.Parameters.ContainsKey(DisabledParameter) && tag.Parameters[DisabledParameter].AsBool();
68+
}
5569
}
5670
}

ClosedXML.Report/RangeInterpreter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public RangeInterpreter(string alias, TemplateErrors errors)
3232
public void Evaluate(IXLRange range)
3333
{
3434
var rangeName = range.RangeAddress.ToStringRelative(true);
35-
ParseTags(range, rangeName);
3635
EvaluateValues(range);
36+
ParseTags(range, rangeName);
3737
TagsPostprocessing(rangeName, new ProcessingContext(range, null, _evaluator));
3838
}
3939

ClosedXML.Report/Utils/ConvertExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static bool AsBool(this string value)
5959
case "нет":
6060
case "no":
6161
case "not":
62+
case "null":
6263
return false;
6364
default:
6465
return true;

tests/ClosedXML.Report.Tests/GroupTagTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public GroupTagTests(ITestOutputHelper output) : base(output)
2121
InlineData("tLists1_cell_setting.xlsx"),
2222
InlineData("tLists2_sum.xlsx"),
2323
InlineData("tLists3_options.xlsx"),
24-
InlineData("delete_options.xlsx"),
2524
InlineData("issue#111_autofilter_with_delete.xlsx"),
2625
InlineData("tLists4_complexRange.xlsx"),
2726
InlineData("tLists5_GlobalVars.xlsx"),

tests/ClosedXML.Report.Tests/ReportOptionsTests.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System;
2-
using System.Globalization;
32
using System.Linq;
4-
using System.Threading;
53
using ClosedXML.Excel;
64
using ClosedXML.Report.Tests.TestModels;
75
using FluentAssertions;
6+
using LinqToDB;
87
using Xunit;
98
using Xunit.Abstractions;
109

1110
namespace ClosedXML.Report.Tests
1211
{
12+
[Collection("Database")]
1313
public class ReportOptionsTests : XlsxTemplateTestsBase
1414
{
1515
[Fact]
@@ -72,6 +72,26 @@ public void Sort_option_should_sort_range()
7272
});
7373
}
7474

75+
[Fact]
76+
public void DeleteOptionsWithParameter()
77+
{
78+
XlTemplateTest("delete_options.xlsx",
79+
tpl =>
80+
{
81+
using (var db = new DbDemos())
82+
{
83+
var cust = db.customers.LoadWith(x => x.Orders.First().Items).OrderBy(c => c.CustNo).First(x => x.CustNo == 1356);
84+
tpl.AddVariable(cust);
85+
}
86+
tpl.AddVariable("disableCColumnDeletion", "true");
87+
tpl.AddVariable("disableEColumnDeletion", "false");
88+
},
89+
wb =>
90+
{
91+
CompareWithGauge(wb, "delete_options.xlsx");
92+
});
93+
}
94+
7595
public ReportOptionsTests(ITestOutputHelper output) : base(output)
7696
{
7797
}

tests/Templates/delete_options.xlsx

111 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)