public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE) { double serialNum = 0.0; try { serialNum = NumericFunction.SingleOperandEvaluate(serialNumVE, srcRowIndex, srcColumnIndex); } catch (EvaluationException) { return(ErrorEval.VALUE_INVALID); } //Calendar serialNumCalendar = new GregorianCalendar(); //serialNumCalendar.setTime(DateUtil.GetJavaDate(serialNum, false)); DateTime serialNumCalendar = DateUtil.GetJavaDate(serialNum, false); int returnType = 0; try { ValueEval ve = OperandResolver.GetSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex); returnType = OperandResolver.CoerceValueToInt(ve); } catch (EvaluationException) { return(ErrorEval.NUM_ERROR); } if (returnType != 1 && returnType != 2) { return(ErrorEval.NUM_ERROR); } return(new NumberEval(getWeekNo(serialNumCalendar, returnType))); }
/** * Dereferences a single value from any AreaEval or RefEval evaluation * result. If the supplied evaluationResult is just a plain value, it is * returned as-is. * * @return a {@link NumberEval}, {@link StringEval}, {@link BoolEval}, or * {@link ErrorEval}. Never <code>null</code>. {@link BlankEval} is * converted to {@link NumberEval#ZERO} */ public static ValueEval DereferenceResult(ValueEval evaluationResult, OperationEvaluationContext ec) { ValueEval value; if (ec == null) { throw new ArgumentNullException("OperationEvaluationContext ec is null"); } if (ec.GetWorkbook() == null) { throw new ArgumentNullException("OperationEvaluationContext ec.getWorkbook() is null"); } IEvaluationSheet evalSheet = ec.GetWorkbook().GetSheet(ec.SheetIndex); IEvaluationCell evalCell = evalSheet.GetCell(ec.RowIndex, ec.ColumnIndex); if (evalCell.IsPartOfArrayFormulaGroup && evaluationResult is AreaEval) { value = OperandResolver.GetElementFromArray((AreaEval)evaluationResult, evalCell); } else { value = DereferenceResult(evaluationResult, ec.RowIndex, ec.ColumnIndex); } return(value); }
/** * Evaluate a generic {@link ValueEval} argument to an array of double values that represents dates in POI. * * @param arg {@link ValueEval} an argument. * @param srcCellRow number cell row. * @param srcCellCol number cell column. * @return an array of doubles representing dates in POI. * @throws EvaluationException exception upon argument evaluation. */ public double[] EvaluateDatesArg(ValueEval arg, int srcCellRow, int srcCellCol) { if (arg == null) { return(new double[0]); } if (arg is StringEval) { return(new double[] { EvaluateDateArg(arg, srcCellRow, srcCellCol) }); } else if (arg is AreaEvalBase) { List <Double> valuesList = new List <Double>(); AreaEvalBase area = (AreaEvalBase)arg; for (int i = area.FirstRow; i <= area.LastRow; i++) { for (int j = area.FirstColumn; j <= area.LastColumn; j++) { // getValue() is replaced with getAbsoluteValue() because loop variables i, j are // absolute indexes values, but getValue() works with relative indexes values valuesList.Add(EvaluateDateArg(area.GetAbsoluteValue(i, j), i, j)); } } double[] values = new double[valuesList.Count]; for (int i = 0; i < valuesList.Count; i++) { values[i] = valuesList[(i)]; } return(values); } return(new double[] { OperandResolver.CoerceValueToDouble(arg) }); }
/** * For a given database returns the column number for a column heading. * * @param db Database. * @param name Column heading. * @return Corresponding column number. * @If it's not possible to turn all headings into strings. */ private static int GetColumnForString(AreaEval db, String name) { int resultColumn = -1; int width = db.Width; for (int column = 0; column < width; ++column) { ValueEval columnNameValueEval = ResolveReference(db, 0, column); if (columnNameValueEval is BlankEval) { continue; } if (columnNameValueEval is ErrorEval) { continue; } String columnName = OperandResolver.CoerceValueToString(columnNameValueEval); if (name.Equals(columnName)) { resultColumn = column; break; } } return(resultColumn); }
/** * When the second argument Is a string, many things are possible */ private static I_MatchPredicate CreateGeneralMatchPredicate(StringEval stringEval) { String value = stringEval.StringValue; CmpOp optr = CmpOp.GetOperator(value); value = value.Substring(optr.Length); bool?boolVal = ParseBoolean(value); if (boolVal != null) { return(new BooleanMatcher(boolVal == true?true:false, optr)); } Double doubleVal = OperandResolver.ParseDouble(value); if (!double.IsNaN(doubleVal)) { return(new NumberMatcher(doubleVal, optr)); } //else - just a plain string with no interpretation. return(new StringMatcher(value, optr)); }
public bool Matches(ValueEval x) { double testValue; if (x is StringEval) { // if the tarGet(x) Is a string, but Parses as a number // it may still Count as a match StringEval se = (StringEval)x; double val = OperandResolver.ParseDouble(se.StringValue); if (double.IsNaN(val)) { // x Is text that Is not a number return(false); } testValue = val; } else if (x is NumberEval) { NumberEval ne = (NumberEval)x; testValue = ne.NumberValue; } else { return(false); } return(_operator.Evaluate(testValue.CompareTo(_value))); }
public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { DateTime date; double numberOfMonths, result; if (args.Length != 2) { return(ErrorEval.VALUE_INVALID); } try { // resolve the arguments date = DateUtil.GetJavaDate(OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[0], ec.RowIndex, ec.ColumnIndex))); numberOfMonths = OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[1], ec.RowIndex, ec.ColumnIndex)); // calculate the result date (Excel rounds the second argument always to zero; but we have be careful about negative numbers) DateTime resultDate = date.AddMonths((int)Math.Floor(Math.Abs(numberOfMonths)) * Math.Sign(numberOfMonths)); result = DateUtil.GetExcelDate(resultDate); NumericFunction.CheckValue(result); return(new NumberEval(result)); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) { ValueEval veText = OperandResolver.GetSingleValue(arg, srcRowIndex, srcColumnIndex); String strText1 = OperandResolver.CoerceValueToString(veText); return(OperandResolver.ParseDouble(strText1)); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { AreaEval aeRange; double result; try { ValueEval ve = OperandResolver.GetSingleValue(arg0, srcRowIndex, srcColumnIndex); result = OperandResolver.CoerceValueToDouble(ve); if (Double.IsNaN(result) || Double.IsInfinity(result)) { throw new EvaluationException(ErrorEval.NUM_ERROR); } if (arg1 is RefListEval) { return(eval(result, ((RefListEval)arg1), true)); } aeRange = ConvertRangeArg(arg1); return(eval(result, aeRange, true)); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
private static double EvaluateDoubleArg(ValueEval eval, int srcCellRow, int srcCellCol) { ValueEval ve = OperandResolver.GetSingleValue(eval, srcCellRow, srcCellCol); if (ve is NumericValueEval) { return(((NumericValueEval)ve).NumberValue); } if (ve is StringEval) { StringEval se = (StringEval)ve; double d = OperandResolver.ParseDouble(se.StringValue); if (double.IsNaN(d)) { throw new EvalEx(ErrorEval.VALUE_INVALID); } return(d); } if (ve is BoolEval) { // in the context of OFFSet, bools resolve to 0 and 1. if (((BoolEval)ve).BooleanValue) { return(1); } return(0); } throw new Exception("Unexpected eval type (" + ve.GetType().Name + ")"); }
/** * When the second argument is a string, many things are possible */ private static IMatchPredicate CreateGeneralMatchPredicate(StringEval stringEval) { String value = stringEval.StringValue; CmpOp operator1 = CmpOp.GetOperator(value); value = value.Substring(operator1.Length); bool?booleanVal = ParseBoolean(value); if (booleanVal != null) { return(new BooleanMatcher(booleanVal.Value, operator1)); } Double doubleVal = OperandResolver.ParseDouble(value); if (!double.IsNaN(doubleVal)) { return(new NumberMatcher(doubleVal, operator1)); } ErrorEval ee = ParseError(value); if (ee != null) { return(new ErrorMatcher(ee.ErrorCode, operator1)); } //else - just a plain string with no interpretation. return(new StringMatcher(value, operator1)); }
private ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval lookupEval, ValueEval indexEval, ValueEval returnEval, String notFound, LookupUtils.MatchMode matchMode, LookupUtils.SearchMode searchMode, bool isSingleValue) { try { ValueEval lookupValue = OperandResolver.GetSingleValue(lookupEval, srcRowIndex, srcColumnIndex); TwoDEval tableArray = LookupUtils.ResolveTableArrayArg(indexEval); int matchedRow; try { matchedRow = LookupUtils.XlookupIndexOfValue(lookupValue, LookupUtils.CreateColumnVector(tableArray, 0), matchMode, searchMode); } catch (EvaluationException e) { if (ErrorEval.NA.Equals(e.GetErrorEval())) { if (string.IsNullOrEmpty(notFound)) { if (returnEval is AreaEval) { AreaEval area = (AreaEval)returnEval; int width = area.Width; if (isSingleValue || width <= 1) { return(new StringEval(notFound)); } return(new NotFoundAreaEval(notFound, width)); } else { return(new StringEval(notFound)); } } return(ErrorEval.NA); } else { return(e.GetErrorEval()); } } if (returnEval is AreaEval) { AreaEval area = (AreaEval)returnEval; if (isSingleValue) { return(area.GetRelativeValue(matchedRow, 0)); } return(area.Offset(matchedRow, matchedRow, 0, area.Width - 1)); } else { return(returnEval); } } catch (EvaluationException e) { return(e.GetErrorEval()); } }
protected override double Eval(ValueEval[] args, int srcCellRow, int srcCellCol) { if (args.Length != 4) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } double result; ValueEval v1 = OperandResolver.GetSingleValue(args[0], srcCellRow, srcCellCol); ValueEval v2 = OperandResolver.GetSingleValue(args[1], srcCellRow, srcCellCol); ValueEval v3 = OperandResolver.GetSingleValue(args[2], srcCellRow, srcCellCol); ValueEval v4 = OperandResolver.GetSingleValue(args[3], srcCellRow, srcCellCol); double interestRate = OperandResolver.CoerceValueToDouble(v1); int period = OperandResolver.CoerceValueToInt(v2); int numberPayments = OperandResolver.CoerceValueToInt(v3); double PV = OperandResolver.CoerceValueToDouble(v4); result = Finance.IPMT(interestRate, period, numberPayments, PV); CheckValue(result); return(result); }
public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) { ValueEval arg3 = null; switch (args.Length) { case 4: arg3 = args[3]; // important: assumed array element Is never null break; case 3: break; default: // wrong number of arguments return(ErrorEval.VALUE_INVALID); } try { // Evaluation order: // arg0 lookup_value, arg1 table_array, arg3 range_lookup, Find lookup value, arg2 row_index, fetch result ValueEval lookupValue = OperandResolver.GetSingleValue(args[0], srcCellRow, srcCellCol); AreaEval tableArray = LookupUtils.ResolveTableArrayArg(args[1]); bool IsRangeLookup = LookupUtils.ResolveRangeLookupArg(arg3, srcCellRow, srcCellCol); int colIndex = LookupUtils.LookupIndexOfValue(lookupValue, LookupUtils.CreateRowVector(tableArray, 0), IsRangeLookup); int rowIndex = LookupUtils.ResolveRowOrColIndexArg(args[2], srcCellRow, srcCellCol); ValueVector resultCol = CreateResultColumnVector(tableArray, rowIndex); return(resultCol.GetItem(colIndex)); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval venumerator, ValueEval vedenominator) { double enumerator = 0; try { ValueEval ve = OperandResolver.GetSingleValue(venumerator, srcRowIndex, srcColumnIndex); enumerator = OperandResolver.CoerceValueToDouble(ve); } catch (EvaluationException) { return(ErrorEval.VALUE_INVALID); } double denominator = 0; try { ValueEval ve = OperandResolver.GetSingleValue(vedenominator, srcRowIndex, srcColumnIndex); denominator = OperandResolver.CoerceValueToDouble(ve); } catch (EvaluationException) { return(ErrorEval.VALUE_INVALID); } if (denominator == 0) { return(ErrorEval.DIV_ZERO); } return(new NumberEval((int)(enumerator / denominator))); }
private static double EvaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol) { ValueEval match_type = OperandResolver.GetSingleValue(arg, srcCellRow, srcCellCol); if (match_type is ErrorEval) { throw new EvaluationException((ErrorEval)match_type); } if (match_type is NumericValueEval) { NumericValueEval ne = (NumericValueEval)match_type; return(ne.NumberValue); } if (match_type is StringEval) { StringEval se = (StringEval)match_type; double d = OperandResolver.ParseDouble(se.StringValue); if (double.IsNaN(d)) { // plain string throw new EvaluationException(ErrorEval.VALUE_INVALID); } // if the string Parses as a number, it Is OK return(d); } throw new Exception("Unexpected match_type type (" + match_type.GetType().Name + ")"); }
public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { int nInnerArgs = args.Length - 1; // -1: first arg is used to select from a basic aggregate function if (nInnerArgs < 1) { return(ErrorEval.VALUE_INVALID); } Function innerFunc; try { ValueEval ve = OperandResolver.GetSingleValue(args[0], srcRowIndex, srcColumnIndex); int functionCode = OperandResolver.CoerceValueToInt(ve); innerFunc = FindFunction(functionCode); } catch (EvaluationException e) { return(e.GetErrorEval()); } ValueEval[] innerArgs = new ValueEval[nInnerArgs]; Array.Copy(args, 1, innerArgs, 0, nInnerArgs); return(innerFunc.Evaluate(innerArgs, srcRowIndex, srcColumnIndex)); }
/** * Evaluate a generic {@link ValueEval} argument to an array of double values that represents dates in POI. * * @param arg {@link ValueEval} an argument. * @param srcCellRow number cell row. * @param srcCellCol number cell column. * @return an array of doubles representing dates in POI. * @throws EvaluationException exception upon argument evaluation. */ public double[] EvaluateDatesArg(ValueEval arg, int srcCellRow, int srcCellCol) { if (arg == null) { return(new double[0]); } if (arg is StringEval) { return(new double[] { EvaluateDateArg(arg, srcCellRow, srcCellCol) }); } else if (arg is AreaEvalBase) { List <Double> valuesList = new List <Double>(); AreaEvalBase area = (AreaEvalBase)arg; for (int i = area.FirstRow; i <= area.LastRow; i++) { for (int j = area.FirstColumn; j <= area.LastColumn; j++) { valuesList.Add(EvaluateDateArg(area.GetValue(i, j), i, j)); } } double[] values = new double[valuesList.Count]; for (int i = 0; i < valuesList.Count; i++) { values[i] = valuesList[(i)]; } return(values); } return(new double[] { OperandResolver.CoerceValueToDouble(arg) }); }
/** * Evaluate for RANDBETWEEN(). Must be given two arguments. Bottom must be greater than top. * Bottom is rounded up and top value is rounded down. After rounding top has to be set greater * than top. * * @see org.apache.poi.ss.formula.functions.FreeRefFunction#evaluate(org.apache.poi.ss.formula.eval.ValueEval[], org.apache.poi.ss.formula.OperationEvaluationContext) */ public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { double bottom, top; if (args.Length != 2) { return(ErrorEval.VALUE_INVALID); } try { bottom = OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[0], ec.RowIndex, ec.ColumnIndex)); top = OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[1], ec.RowIndex, ec.ColumnIndex)); if (bottom > top) { return(ErrorEval.NUM_ERROR); } } catch (EvaluationException) { return(ErrorEval.VALUE_INVALID); } bottom = Math.Ceiling(bottom); top = Math.Floor(top); if (bottom > top) { top = bottom; } Random rnd = new Random(); return(new NumberEval((bottom + (int)(rnd.NextDouble() * ((top - bottom) + 1))))); }
public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { double number, multiple, result; if (args.Length != 2) { return(ErrorEval.VALUE_INVALID); } try { number = OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[0], ec.RowIndex, ec.ColumnIndex)); multiple = OperandResolver.CoerceValueToDouble(OperandResolver.GetSingleValue(args[1], ec.RowIndex, ec.ColumnIndex)); if (multiple == 0.0) { result = 0.0; } else { if (number * multiple < 0) { // Returns #NUM! because the number and the multiple have different signs throw new EvaluationException(ErrorEval.NUM_ERROR); } result = multiple * Math.Round(number / multiple, MidpointRounding.AwayFromZero); } NumericFunction.CheckValue(result); return(new NumberEval(result)); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { if (args.Length < 2) { return(ErrorEval.VALUE_INVALID); } try { int ix = EvaluateFirstArg(args[0], srcRowIndex, srcColumnIndex); if (ix < 1 || ix >= args.Length) { return(ErrorEval.VALUE_INVALID); } ValueEval result = OperandResolver.GetSingleValue(args[ix], srcRowIndex, srcColumnIndex); if (result == MissingArgEval.instance) { return(BlankEval.instance); } return(result); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { if (args.Length != 2) { return(ErrorEval.VALUE_INVALID); } try { return(OperandResolver.GetSingleValue(args[0], ec.RowIndex, ec.ColumnIndex)); } catch (EvaluationException e) { ValueEval error = e.GetErrorEval(); if (error != ErrorEval.NA) { return(error); } } try { return(OperandResolver.GetSingleValue(args[1], ec.RowIndex, ec.ColumnIndex)); } catch (EvaluationException e) { return(e.GetErrorEval()); } }
public static double SingleOperandEvaluate(ValueEval arg, int srcCellRow, int srcCellCol) { ValueEval ve = OperandResolver.GetSingleValue(arg, srcCellRow, srcCellCol); double result = OperandResolver.CoerceValueToDouble(ve); CheckValue(result); return(result); }
public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { if (args.Length < 3) { //First 3 parameters are mandatory return(ErrorEval.VALUE_INVALID); } double periods, payment, present_val, future_val = 0, type = 0, estimate = 0.1, rate; try { ValueEval v1 = OperandResolver.GetSingleValue(args[0], srcRowIndex, srcColumnIndex); ValueEval v2 = OperandResolver.GetSingleValue(args[1], srcRowIndex, srcColumnIndex); ValueEval v3 = OperandResolver.GetSingleValue(args[2], srcRowIndex, srcColumnIndex); ValueEval v4 = null; if (args.Length >= 4) { v4 = OperandResolver.GetSingleValue(args[3], srcRowIndex, srcColumnIndex); } ValueEval v5 = null; if (args.Length >= 5) { v5 = OperandResolver.GetSingleValue(args[4], srcRowIndex, srcColumnIndex); } ValueEval v6 = null; if (args.Length >= 6) { v6 = OperandResolver.GetSingleValue(args[5], srcRowIndex, srcColumnIndex); } periods = OperandResolver.CoerceValueToDouble(v1); payment = OperandResolver.CoerceValueToDouble(v2); present_val = OperandResolver.CoerceValueToDouble(v3); if (args.Length >= 4) { future_val = OperandResolver.CoerceValueToDouble(v4); } if (args.Length >= 5) { type = OperandResolver.CoerceValueToDouble(v5); } if (args.Length >= 6) { estimate = OperandResolver.CoerceValueToDouble(v6); } rate = CalculateRate(periods, payment, present_val, future_val, type, estimate); CheckValue(rate); } catch (EvaluationException e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); return(e.GetErrorEval()); } return(new NumberEval(rate)); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { double dn; try { ValueEval ve1 = OperandResolver.GetSingleValue(arg1, srcRowIndex, srcColumnIndex); dn = OperandResolver.CoerceValueToDouble(ve1); } catch (EvaluationException e1) { // all errors in the second arg translate to #VALUE! return(ErrorEval.VALUE_INVALID); } if (dn < 0 || dn > 1) { // has to be percentage return(ErrorEval.NUM_ERROR); } double result; try { double[] ds = NPOI.SS.Formula.Functions.AggregateFunction.ValueCollector.CollectValues(arg0); int N = ds.Length; if (N == 0 || N > 8191) { return(ErrorEval.NUM_ERROR); } double n = (N - 1) * dn + 1; if (n == 1d) { result = StatsLib.kthSmallest(ds, 1); } else if (n == N) { result = StatsLib.kthLargest(ds, 1); } else { int k = (int)n; double d = n - k; result = StatsLib.kthSmallest(ds, k) + d * (StatsLib.kthSmallest(ds, k + 1) - StatsLib.kthSmallest(ds, k)); } NumericFunction.CheckValue(result); } catch (EvaluationException e) { return(e.GetErrorEval()); } return(new NumberEval(result)); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval inumberVE) { ValueEval veText1; try { veText1 = OperandResolver.GetSingleValue(inumberVE, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return(e.GetErrorEval()); } string iNumber = OperandResolver.CoerceValueToString(veText1); //Matcher m = COMPLEX_NUMBER_PATTERN.matcher(iNumber); //bool result = m.matches(); System.Text.RegularExpressions.Match m = COMPLEX_NUMBER_PATTERN.Match(iNumber); bool result = m.Success && m.Groups[0].Length > 0; string imaginary = ""; if (result == true) { string imaginaryGroup = m.Groups[5].Value; bool hasImaginaryPart = imaginaryGroup.Equals("i") || imaginaryGroup.Equals("j"); if (imaginaryGroup.Length == 0) { return(new StringEval(Convert.ToString(0))); } if (hasImaginaryPart) { string sign = ""; string imaginarySign = m.Groups[(GROUP3_IMAGINARY_SIGN)].Value; if (imaginarySign.Length != 0 && !(imaginarySign.Equals("+"))) { sign = imaginarySign; } string groupImaginaryNumber = m.Groups[(GROUP4_IMAGINARY_INTEGER_OR_DOUBLE)].Value; if (groupImaginaryNumber.Length != 0) { imaginary = sign + groupImaginaryNumber; } else { imaginary = sign + "1"; } } } else { return(ErrorEval.NUM_ERROR); } return(new StringEval(imaginary)); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval inumberVE) { ValueEval veText1; try { veText1 = OperandResolver.GetSingleValue(inumberVE, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return(e.GetErrorEval()); } string iNumber = OperandResolver.CoerceValueToString(veText1); System.Text.RegularExpressions.Match m = Imaginary.COMPLEX_NUMBER_PATTERN.Match(iNumber); //bool result = m.matches(); bool result = m.Success && !string.IsNullOrEmpty(m.Groups[0].Value); string real = ""; if (result == true) { string realGroup = m.Groups[(2)].Value; bool hasRealPart = realGroup.Length != 0; if (realGroup.Length == 0) { return(new StringEval(Convert.ToString(0))); } if (hasRealPart) { string sign = ""; string realSign = m.Groups[(Imaginary.GROUP1_REAL_SIGN)].Value; if (realSign.Length != 0 && !(realSign.Equals("+"))) { sign = realSign; } string groupRealNumber = m.Groups[(Imaginary.GROUP2_IMAGINARY_INTEGER_OR_DOUBLE)].Value; if (groupRealNumber.Length != 0) { real = sign + groupRealNumber; } else { real = sign + "1"; } } } else { return(ErrorEval.NUM_ERROR); } return(new StringEval(real)); }
/** * Evaluate a generic {@link ValueEval} argument to a double value. * * @param arg {@link ValueEval} an argument. * @param srcCellRow number cell row. * @param srcCellCol number cell column. * @return a double value. * @throws EvaluationException exception upon argument evaluation. */ public double EvaluateNumberArg(ValueEval arg, int srcCellRow, int srcCellCol) { if (arg == null) { return(0f); } return(OperandResolver.CoerceValueToDouble(arg)); }
private void CollectValue(ValueEval ve, bool isViaReference, DoubleList temp) { if (ve == null) { throw new ArgumentException("ve must not be null"); } if (ve is BoolEval) { if (!isViaReference || _isReferenceBoolCounted) { BoolEval boolEval = (BoolEval)ve; temp.Add(boolEval.NumberValue); } return; } if (ve is NumberEval) { NumberEval ne = (NumberEval)ve; temp.Add(ne.NumberValue); return; } if (ve is StringEval) { if (isViaReference) { // ignore all ref strings return; } String s = ((StringEval)ve).StringValue; Double d = OperandResolver.ParseDouble(s); if (double.IsNaN(d)) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } temp.Add(d); return; } if (ve is ErrorEval) { throw new EvaluationException((ErrorEval)ve); } if (ve == BlankEval.instance) { if (_isBlankCounted) { temp.Add(0.0); } return; } if (ve is NumberValueArrayEval nvae) { temp.Add(nvae.NumberValues); return; } throw new InvalidOperationException("Invalid ValueEval type passed for conversion: (" + ve.GetType() + ")"); }
public override bool Matches(ValueEval x) { double testValue; if (x is StringEval) { // if the target(x) is a string, but parses as a number // it may still count as a match, only for the equality operator switch (Code) { case CmpOp.EQ: case CmpOp.NONE: break; case CmpOp.NE: // Always matches (inconsistent with above two cases). // for example '<>123' matches '123', '4', 'abc', etc return(true); default: // never matches (also inconsistent with above three cases). // for example '>5' does not match '6', return(false); } StringEval se = (StringEval)x; Double val = OperandResolver.ParseDouble(se.StringValue); if (double.IsNaN(val)) { // x is text that is not a number return(false); } return(_value == val); } else if ((x is NumberEval)) { NumberEval ne = (NumberEval)x; testValue = ne.NumberValue; } else if ((x is BlankEval)) { switch (Code) { case CmpOp.NE: // Excel counts blank values in range as not equal to any value. See Bugzilla 51498 return(true); default: return(false); } } else { return(false); } return(Evaluate(testValue.CompareTo(_value))); }