private static double evaluateDateArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol); if (ve instanceof StringEval) { String strVal = ((StringEval) ve).getStringValue(); Double dVal = OperandResolver.parseDouble(strVal); if (dVal != null) { return dVal.doubleValue(); } Calendar date = DateParser.parseDate(strVal); return DateUtil.getExcelDate(date, false); } return OperandResolver.coerceValueToDouble(ve); }
/** * Retrieves a single value from a variety of different argument types according to standard * Excel rules. Does not perform any type conversion. * @param arg the evaluated argument as passed to the function or operator. * @param srcCellRow used when arg is a single column AreaRef * @param srcCellCol used when arg is a single row AreaRef * @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt> or <tt>BlankEval</tt>. * Never <code>null</code> or <tt>ErrorEval</tt>. * @throws EvaluationException(#VALUE!) if srcCellRow or srcCellCol do not properly index into * an AreaEval. If the actual value retrieved is an ErrorEval, a corresponding * EvaluationException is thrown. */ public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { final ValueEval result; if (arg instanceof RefEval) { result = chooseSingleElementFromRef((RefEval) arg); } else if (arg instanceof AreaEval) { result = chooseSingleElementFromArea((AreaEval) arg, srcCellRow, srcCellCol); } else { result = arg; } if (result instanceof ErrorEval) { throw new EvaluationException((ErrorEval) result); } return result; }
public static boolean evaluateFirstArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol); Boolean b = OperandResolver.coerceValueToBoolean(ve, false); if (b == null) { return false; } return b.booleanValue(); } }
private static int evaluateIntArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol); return OperandResolver.coerceValueToInt(ve); } }
protected static String evaluateStringArg(ValueEval eval, int srcRow, int srcCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(eval, srcRow, srcCol); return OperandResolver.coerceValueToString(ve); } protected static int evaluateIntArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
protected static double evaluateDoubleArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol); return OperandResolver.coerceValueToDouble(ve); }
@Override public double eval(ValueEval[] args, int srcCellRow, int srcCellCol) throws EvaluationException { 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.ppmt(interestRate, period, numberPayments, PV) ; checkValue(result); return result; }
try { ValueEval numberValueEval = OperandResolver.getSingleValue( numberParam, srcRowIndex, srcColumnIndex); BigDecimal number = new BigDecimal(OperandResolver.coerceValueToDouble(numberValueEval)); ValueEval placesValueEval = OperandResolver.getSingleValue( placesParam, srcRowIndex, srcColumnIndex); int places = OperandResolver.coerceValueToInt(placesValueEval); ValueEval skipThousandsSeparatorValueEval = OperandResolver.getSingleValue( skipThousandsSeparatorParam, srcRowIndex, srcColumnIndex); Boolean skipThousandsSeparator = OperandResolver.coerceValueToBoolean( skipThousandsSeparatorValueEval, false);
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) { ValueEval veText1; try { veText1 = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } String strText1 = OperandResolver.coerceValueToString(veText1); Double number1 = OperandResolver.parseDouble(strText1); if (number1 == null) { return ErrorEval.VALUE_INVALID; } ValueEval veText2; try { veText2 = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } String strText2 = OperandResolver.coerceValueToString(veText2); Double number2 = OperandResolver.parseDouble(strText2); if (number2 == null) { return ErrorEval.VALUE_INVALID; } int result = new BigDecimal(number1.doubleValue()).compareTo(new BigDecimal(number2.doubleValue())); return result == 0 ? ONE : ZERO; }
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval text, ValueEval number_times) { ValueEval veText1; try { veText1 = OperandResolver.getSingleValue(text, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } String strText1 = OperandResolver.coerceValueToString(veText1); double numberOfTime = 0; try { numberOfTime = OperandResolver.coerceValueToDouble(number_times); } catch (EvaluationException e) { return ErrorEval.VALUE_INVALID; } int numberOfTimeInt = (int)numberOfTime; StringBuffer strb = new StringBuffer(strText1.length() * numberOfTimeInt); for(int i = 0; i < numberOfTimeInt; i++) { strb.append(strText1); } if (strb.toString().length() > 32767) { return ErrorEval.VALUE_INVALID; } return new StringEval(strb.toString()); } }
veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short)srcCellCol); } catch (EvaluationException e) { StringEval se = (StringEval) veRowColIndexArg; String strVal = se.getStringValue(); Double dVal = OperandResolver.parseDouble(strVal); if(dVal == null) { oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg); if (oneBasedIndex < 1) {
private static double evaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval match_type = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol); if(match_type instanceof ErrorEval) { throw new EvaluationException((ErrorEval)match_type); } if(match_type instanceof NumericValueEval) { NumericValueEval ne = (NumericValueEval) match_type; return ne.getNumberValue(); } if (match_type instanceof StringEval) { StringEval se = (StringEval) match_type; Double d = OperandResolver.parseDouble(se.getStringValue()); if(d == null) { // plain string throw new EvaluationException(ErrorEval.VALUE_INVALID); } // if the string parses as a number, it is OK return d.doubleValue(); } throw new RuntimeException("Unexpected match_type type (" + match_type.getClass().getName() + ")"); }
/** * * * @param nameValueEval Must not be a RefEval or AreaEval. Thus make sure resolveReference() is called on the value first! * @param db Database * @return Corresponding column number. * @throws EvaluationException If it's not possible to turn all headings into strings. */ private static int getColumnForName(ValueEval nameValueEval, AreaEval db) throws EvaluationException { String name = OperandResolver.coerceValueToString(nameValueEval); return getColumnForString(db, name); }
/** * Applies some conversion rules if the supplied value is not already a number. * Note - <tt>BlankEval</tt> is converted to {@link NumberEval#ZERO}. * @param ev must be a {@link NumberEval}, {@link StringEval}, {@link BoolEval} or * {@link BlankEval} * @return actual, parsed or interpreted double value (respectively). * @throws EvaluationException(#VALUE!) only if a StringEval is supplied and cannot be parsed * as a double (See <tt>parseDouble()</tt> for allowable formats). * @throws RuntimeException if the supplied parameter is not {@link NumberEval}, * {@link StringEval}, {@link BoolEval} or {@link BlankEval} */ public static double coerceValueToDouble(ValueEval ev) throws EvaluationException { if (ev == BlankEval.instance) { return 0.0; } if (ev instanceof NumericValueEval) { // this also handles booleans return ((NumericValueEval)ev).getNumberValue(); } if (ev instanceof StringEval) { Double dd = parseDouble(((StringEval) ev).getStringValue()); if (dd == null) { throw EvaluationException.invalidValue(); } return dd.doubleValue(); } throw new RuntimeException("Unexpected arg eval type (" + ev.getClass().getName() + ")"); }
/** * 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) throws EvaluationException { if (arg == null) { return 0f; } return OperandResolver.coerceValueToDouble(arg); } }
ValueEval result = chooseSingleElementFromAreaInternal(ae, srcCellRow, srcCellCol); if (result instanceof ErrorEval) { throw new EvaluationException((ErrorEval) result);
/** * * @return the de-referenced criteria arg (possibly {@link ErrorEval}) */ private static ValueEval evaluateCriteriaArg(ValueEval arg, int srcRowIndex, int srcColumnIndex) { try { return OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } } /**
for (int rcIx=0; rcIx<width; rcIx++) { ValueEval ve = ae.getValue(rrIx, rcIx); tempVe = OperandResolver.coerceValueToBoolean(ve, true); if (tempVe != null) { result = partialEvaluate(result, tempVe.booleanValue()); for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) { ValueEval ve = re.getInnerValueEval(sIx); tempVe = OperandResolver.coerceValueToBoolean(ve, true); if (tempVe != null) { result = partialEvaluate(result, tempVe.booleanValue()); tempVe = OperandResolver.coerceValueToBoolean(arg, false);
/** * Retrieves a single value from a variety of different argument types according to standard * Excel rules. Does not perform any type conversion. * @param arg the evaluated argument as passed to the function or operator. * @param srcCellRow used when arg is a single column AreaRef * @param srcCellCol used when arg is a single row AreaRef * @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt> or <tt>BlankEval</tt>. * Never <code>null</code> or <tt>ErrorEval</tt>. * @throws EvaluationException(#VALUE!) if srcCellRow or srcCellCol do not properly index into * an AreaEval. If the actual value retrieved is an ErrorEval, a corresponding * EvaluationException is thrown. */ public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval result; if (arg instanceof RefEval) { result = ((RefEval) arg).getInnerValueEval(); } else if (arg instanceof AreaEval) { result = chooseSingleElementFromArea((AreaEval) arg, srcCellRow, srcCellCol); } else { result = arg; } if (result instanceof ErrorEval) { throw new EvaluationException((ErrorEval) result); } return result; }
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) { int number; try { number = OperandResolver.coerceValueToInt(numberVE); } catch (EvaluationException e) { return ErrorEval.VALUE_INVALID; } if (number < 0) { return ErrorEval.NUM_ERROR; } return new NumberEval(factorial(number).longValue()); }