Skip to content
Open
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
32 changes: 31 additions & 1 deletion ClosedXML.Report/RangeInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using ClosedXML.Report.Options;
using ClosedXML.Report.Utils;
using System.Linq.Dynamic.Core.Exceptions;
using System.Text.RegularExpressions;


namespace ClosedXML.Report
Expand Down Expand Up @@ -248,7 +249,36 @@ private BoundRange BindToVariable(IXLNamedRange namedRange)
variableValue is IEnumerable data1)
return new BoundRange(namedRange, data1);

var expression = "{{" + namedRange.Name.Replace("_", ".") +"}}";
// Get the name of the named range
var input = namedRange.Name;

// Define a regular expression pattern to match parts of the string that are enclosed in backslashes
string pattern = @"\\(.*?)\\";

// Use the regular expression to find all matches in the input string
var matches = Regex.Matches(input, pattern);

// Iterate over each match found
foreach (Match match in matches)
{
// Replace underscores in the matched string with caret symbols
string replacement = match.Value.Replace("_", "^");

// Replace the matched part in the input string with the modified string
input = input.Replace(match.Value, replacement);
}

// Remove all backslashes from the input string
input = input.Replace("\\", "");

// Replace all remaining underscores in the input string with dots
input = input.Replace("_", ".");

// Replace all caret symbols in the input string back to underscores
input = input.Replace("^", "_");

// Create an expression by enclosing the modified input string in double curly braces
var expression = "{{" + input +"}}";

if (_evaluator.TryEvaluate(expression, out var res) &&
res is IEnumerable data2)
Expand Down
24 changes: 22 additions & 2 deletions ClosedXML.Report/RangeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Linq.Dynamic.Core.Exceptions;
using System.Reflection;

using System.Text.RegularExpressions;
using ClosedXML.Excel;
using ClosedXML.Report.Excel;
using ClosedXML.Report.Options;
Expand Down Expand Up @@ -368,7 +368,27 @@ private void RenderSubrange(RangeTemplate ownRng, object item, FormulaEvaluator
{
var start = _buff.NextAddress;
// the child template to which the cell belongs
var formula = ownRng.Source.ReplaceLast("_", ".");
// Define a regular expression pattern to match parts of the string that are enclosed in backslashes
string pattern = @"\\(.*?)\\";
var formula = ownRng.Source;
var matches = Regex.Matches(formula, pattern);
// Iterate over each match found
foreach (Match match in matches)
{
// Replace underscores in the matched string with caret symbols
string replacement = match.Value.Replace("_", "^");

// Replace the matched part in the input string with the modified string
formula = formula.Replace(match.Value, replacement);
}

// Remove all backslashes from the input string
formula = formula.Replace("\\", "");

formula = formula.ReplaceLast("_", ".");
formula = formula.Replace("^", "_");

//var formula = ownRng.Source.ReplaceLast("_", ".");

if (evaluator.Evaluate(formula, new Parameter(Name, item)) is IEnumerable value)
{
Expand Down