Search in sources :

Example 46 with EvaluationException

use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.

the class XYNumericFunction method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
    double result;
    try {
        ValueVector vvX = createValueVector(arg0);
        ValueVector vvY = createValueVector(arg1);
        int size = vvX.getSize();
        if (size == 0 || vvY.getSize() != size) {
            return ErrorEval.NA;
        }
        result = evaluateInternal(vvX, vvY, size);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    if (Double.isNaN(result) || Double.isInfinite(result)) {
        return ErrorEval.NUM_ERROR;
    }
    return new NumberEval(result);
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 47 with EvaluationException

use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.

the class Rept method evaluate.

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());
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 48 with EvaluationException

use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.

the class Subtotal method evaluate.

public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
    // -1: first arg is used to select from a basic aggregate function
    int nInnerArgs = args.length - 1;
    if (nInnerArgs < 1) {
        return ErrorEval.VALUE_INVALID;
    }
    final 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();
    }
    // ignore the first arg, this is the function-type, we check for the length above
    final List<ValueEval> list = new ArrayList<ValueEval>(Arrays.asList(args).subList(1, args.length));
    Iterator<ValueEval> it = list.iterator();
    // For array references it is handled in other evaluation steps, but we need to handle this here for references to subtotal-functions
    while (it.hasNext()) {
        ValueEval eval = it.next();
        if (eval instanceof LazyRefEval) {
            LazyRefEval lazyRefEval = (LazyRefEval) eval;
            if (lazyRefEval.isSubTotal()) {
                it.remove();
            }
        }
    }
    return innerFunc.evaluate(list.toArray(new ValueEval[list.size()]), srcRowIndex, srcColumnIndex);
}
Also used : LazyRefEval(org.apache.poi.ss.formula.LazyRefEval) ArrayList(java.util.ArrayList) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 49 with EvaluationException

use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.

the class Roman method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval formVE) {
    int number = 0;
    try {
        ValueEval ve = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);
        number = OperandResolver.coerceValueToInt(ve);
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
    if (number < 0) {
        return ErrorEval.VALUE_INVALID;
    }
    if (number > 3999) {
        return ErrorEval.VALUE_INVALID;
    }
    if (number == 0) {
        return new StringEval("");
    }
    int form = 0;
    try {
        ValueEval ve = OperandResolver.getSingleValue(formVE, srcRowIndex, srcColumnIndex);
        form = OperandResolver.coerceValueToInt(ve);
    } catch (EvaluationException e) {
        return ErrorEval.NUM_ERROR;
    }
    if (form > 4 || form < 0) {
        return ErrorEval.VALUE_INVALID;
    }
    String result = this.integerToRoman(number);
    if (form == 0) {
        return new StringEval(result);
    }
    return new StringEval(makeConcise(result, form));
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 50 with EvaluationException

use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.

the class Sumproduct method getScalarValue.

private static double getScalarValue(ValueEval arg) throws EvaluationException {
    ValueEval eval;
    if (arg instanceof RefEval) {
        RefEval re = (RefEval) arg;
        if (re.getNumberOfSheets() > 1) {
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        eval = re.getInnerValueEval(re.getFirstSheetIndex());
    } else {
        eval = arg;
    }
    if (eval == null) {
        throw new RuntimeException("parameter may not be null");
    }
    if (eval instanceof AreaEval) {
        AreaEval ae = (AreaEval) eval;
        // an area ref can work as a scalar value if it is 1x1
        if (!ae.isColumn() || !ae.isRow()) {
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        eval = ae.getRelativeValue(0, 0);
    }
    return getProductTerm(eval, true);
}
Also used : RefEval(org.apache.poi.ss.formula.eval.RefEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Aggregations

EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)50 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)36 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)19 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 StringEval (org.apache.poi.ss.formula.eval.StringEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)7 Calendar (java.util.Calendar)6 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 RefEval (org.apache.poi.ss.formula.eval.RefEval)5 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)3 Date (java.util.Date)2 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)2 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 AssertionFailedError (junit.framework.AssertionFailedError)1