You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Great library, however I am having an issue when mixing types in an array when using AddVariable.
For example:
const string outputFile = @"out.xlsx";
var template = new XLTemplate(@"template.xlsx");
var strings = new[] {
"string",
"1" ,
"string",
};
var mixed = new[] {
(object) "string",
(object) 1 ,
(object) "string",
};
template.AddVariable("strings", strings);
template.AddVariable("mixed", mixed);
template.Generate();
The mixed array will generate a System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.String' when the template is generated.
I looked through the source and the issue is in FormulaEvaluator.cs the constructor for the internal Parameter it is setting an expected type: i.e Expression.Parameter(value.GetType(), name)
however by changing it to ParameterExpression = Expression.Parameter(typeof(object), name);
the error no longer occurs.
The reason converting all values to strings is undesirable is, the generated excel file shows a warning: the number in the cell is formatted as text or preceded by a apostrophe.
This also causes a side effect that any number formatting on that cell will be ignored (like currency)
The text was updated successfully, but these errors were encountered:
Did some further testing and looks like simply using typeof(object) does not work if it is a dynamic object because then lambda.DynamicInvoke(pars.Select(p => p.Value).ToArray()); fails in EvalNoCache with InvalidOperationException: Target object is not an ExpandoObject.
So would suggest checking if the value is simple type first and only then using typeof(object)
Great library, however I am having an issue when mixing types in an array when using AddVariable.
For example:
The mixed array will generate a System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.String' when the template is generated.
I looked through the source and the issue is in FormulaEvaluator.cs the constructor for the internal Parameter it is setting an expected type: i.e Expression.Parameter(value.GetType(), name)
however by changing it to ParameterExpression = Expression.Parameter(typeof(object), name);
the error no longer occurs.
The reason converting all values to strings is undesirable is, the generated excel file shows a warning: the number in the cell is formatted as text or preceded by a apostrophe.
This also causes a side effect that any number formatting on that cell will be ignored (like currency)
The text was updated successfully, but these errors were encountered: