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 disabled parameter to "Delete" tag and evaluate values before parsing #358

Merged
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
14 changes: 14 additions & 0 deletions ClosedXML.Report/Options/DeleteTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ OPTION OBJECTS

using ClosedXML.Excel;
using ClosedXML.Report.Excel;
using ClosedXML.Report.Utils;
using System.Linq;

namespace ClosedXML.Report.Options
{
public class DeleteTag : OptionTag
{
public const string DisabledParameter = "disabled";

public override void Execute(ProcessingContext context)
{
var deleteTags = List.GetAll<DeleteTag>()
Expand All @@ -23,6 +26,12 @@ public override void Execute(ProcessingContext context)

foreach (var tag in deleteTags)
{
if (IsDisabled(tag))
{
tag.Enabled = false;
continue;
}

var xlCell = tag.Cell.GetXlCell(context.Range);
var cellAddr = xlCell.Address.ToStringRelative(false);
var ws = Range.Worksheet;
Expand Down Expand Up @@ -52,5 +61,10 @@ public override void Execute(ProcessingContext context)
tag.Enabled = false;
}
}

private bool IsDisabled(DeleteTag tag)
{
return tag.Parameters.ContainsKey(DisabledParameter) && tag.Parameters[DisabledParameter].AsBool();
}
}
}
2 changes: 1 addition & 1 deletion ClosedXML.Report/RangeInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public RangeInterpreter(string alias, TemplateErrors errors)
public void Evaluate(IXLRange range)
{
var rangeName = range.RangeAddress.ToStringRelative(true);
ParseTags(range, rangeName);
EvaluateValues(range);
ParseTags(range, rangeName);
TagsPostprocessing(rangeName, new ProcessingContext(range, null, _evaluator));
}

Expand Down
1 change: 1 addition & 0 deletions ClosedXML.Report/Utils/ConvertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static bool AsBool(this string value)
case "нет":
case "no":
case "not":
case "null":
return false;
default:
return true;
Expand Down
1 change: 0 additions & 1 deletion tests/ClosedXML.Report.Tests/GroupTagTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public GroupTagTests(ITestOutputHelper output) : base(output)
InlineData("tLists1_cell_setting.xlsx"),
InlineData("tLists2_sum.xlsx"),
InlineData("tLists3_options.xlsx"),
InlineData("delete_options.xlsx"),
InlineData("issue#111_autofilter_with_delete.xlsx"),
InlineData("tLists4_complexRange.xlsx"),
InlineData("tLists5_GlobalVars.xlsx"),
Expand Down
24 changes: 22 additions & 2 deletions tests/ClosedXML.Report.Tests/ReportOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using ClosedXML.Excel;
using ClosedXML.Report.Tests.TestModels;
using FluentAssertions;
using LinqToDB;
using Xunit;
using Xunit.Abstractions;

namespace ClosedXML.Report.Tests
{
[Collection("Database")]
public class ReportOptionsTests : XlsxTemplateTestsBase
{
[Fact]
Expand Down Expand Up @@ -72,6 +72,26 @@ public void Sort_option_should_sort_range()
});
}

[Fact]
public void DeleteOptionsWithParameter()
{
XlTemplateTest("delete_options.xlsx",
tpl =>
{
using (var db = new DbDemos())
{
var cust = db.customers.LoadWith(x => x.Orders.First().Items).OrderBy(c => c.CustNo).First(x => x.CustNo == 1356);
tpl.AddVariable(cust);
}
tpl.AddVariable("disableCColumnDeletion", "true");
tpl.AddVariable("disableEColumnDeletion", "false");
},
wb =>
{
CompareWithGauge(wb, "delete_options.xlsx");
});
}

public ReportOptionsTests(ITestOutputHelper output) : base(output)
{
}
Expand Down
Binary file modified tests/Templates/delete_options.xlsx
Binary file not shown.
Loading