blob: 55f15b2c963b7da4332a5f3dc2172df1a75a7d58 [file] [log] [blame]
using System.Collections.Generic;
using System.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions;
using OfficeOpenXml.FormulaParsing.Exceptions;
namespace OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionCompilers;
public class IfNaFunctionCompiler : FunctionCompiler {
public IfNaFunctionCompiler(ExcelFunction function)
: base(function) {}
public override CompileResult Compile(IEnumerable<Expression> children, ParsingContext context) {
if (children.Count() != 2) {
throw new ExcelErrorValueException(eErrorType.Value);
}
var args = new List<FunctionArgument>();
Function.BeforeInvoke(context);
var firstChild = children.First();
var lastChild = children.ElementAt(1);
try {
var result = firstChild.Compile();
if (result.DataType == DataType.ExcelError
&& (Equals(result.Result, ExcelErrorValue.Create(eErrorType.Na)))) {
args.Add(new(lastChild.Compile().Result));
} else {
args.Add(new(result.Result));
}
} catch (ExcelErrorValueException) {
args.Add(new(lastChild.Compile().Result));
}
return Function.Execute(args, context);
}
}