Hi!
Currently ClosedXML is only able to handle the "IF"-function if supplied with 3 arguments, but this function can also be called with only 2 arguments. ie. something like "IF(M49="111";94)" will return 0 if the predicate is false.
Modifications are necessary in the file "Logical.cs", two methods need to be updated:
```
public static void Register(CalcEngine ce)
{
ce.RegisterFunction("AND", 1, int.MaxValue, And);
ce.RegisterFunction("OR", 1, int.MaxValue, Or);
ce.RegisterFunction("NOT", 1, Not);
ce.RegisterFunction("IF", 2, 3, If);
ce.RegisterFunction("TRUE", 0, True);
ce.RegisterFunction("FALSE", 0, False);
}
```
and
```
static object If(List<Expression> p)
{
if (p.Count == 2) {
var val = p[1].Evaluate();
return (bool) p[0]
? val
: val.GetType().IsValueType
? Activator.CreateInstance(val.GetType())
: null;
} else {
return (bool)p[0]
? p[1].Evaluate()
: p[2].Evaluate();
}
}
```
Cheers,
Ted
Comments: It's actually more nuanced than that. Technically you can omit the 2nd parameter by putting a comma after the first param (e.g. "if(true,)". In that case you get 0. The same for the 3rd parameter. ClosedXML's calc engine doesn't support this scenario. If you don't include the 3rd parameter (not even putting a comma) you get FALSE. I changed it so you can omit the 3rd parameter. Pick up the latest source code.
Currently ClosedXML is only able to handle the "IF"-function if supplied with 3 arguments, but this function can also be called with only 2 arguments. ie. something like "IF(M49="111";94)" will return 0 if the predicate is false.
Modifications are necessary in the file "Logical.cs", two methods need to be updated:
```
public static void Register(CalcEngine ce)
{
ce.RegisterFunction("AND", 1, int.MaxValue, And);
ce.RegisterFunction("OR", 1, int.MaxValue, Or);
ce.RegisterFunction("NOT", 1, Not);
ce.RegisterFunction("IF", 2, 3, If);
ce.RegisterFunction("TRUE", 0, True);
ce.RegisterFunction("FALSE", 0, False);
}
```
and
```
static object If(List<Expression> p)
{
if (p.Count == 2) {
var val = p[1].Evaluate();
return (bool) p[0]
? val
: val.GetType().IsValueType
? Activator.CreateInstance(val.GetType())
: null;
} else {
return (bool)p[0]
? p[1].Evaluate()
: p[2].Evaluate();
}
}
```
Cheers,
Ted
Comments: It's actually more nuanced than that. Technically you can omit the 2nd parameter by putting a comma after the first param (e.g. "if(true,)". In that case you get 0. The same for the 3rd parameter. ClosedXML's calc engine doesn't support this scenario. If you don't include the 3rd parameter (not even putting a comma) you get FALSE. I changed it so you can omit the 3rd parameter. Pick up the latest source code.