Search in sources :

Example 6 with EvaluationException

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

the class CalendarFieldFunction method evaluate.

public final ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
    double val;
    try {
        ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        val = OperandResolver.coerceValueToDouble(ve);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    if (val < 0) {
        return ErrorEval.NUM_ERROR;
    }
    return new NumberEval(getCalField(val));
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 7 with EvaluationException

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

the class BooleanFunction method calculate.

private boolean calculate(ValueEval[] args) throws EvaluationException {
    boolean result = getInitialResultValue();
    boolean atleastOneNonBlank = false;
    /*
		 * Note: no short-circuit boolean loop exit because any ErrorEvals will override the result
		 */
    for (final ValueEval arg : args) {
        Boolean tempVe;
        if (arg instanceof TwoDEval) {
            TwoDEval ae = (TwoDEval) arg;
            int height = ae.getHeight();
            int width = ae.getWidth();
            for (int rrIx = 0; rrIx < height; rrIx++) {
                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());
                        atleastOneNonBlank = true;
                    }
                }
            }
            continue;
        }
        if (arg instanceof RefEval) {
            RefEval re = (RefEval) arg;
            final int firstSheetIndex = re.getFirstSheetIndex();
            final int lastSheetIndex = re.getLastSheetIndex();
            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());
                    atleastOneNonBlank = true;
                }
            }
            continue;
        }
        if (arg == MissingArgEval.instance) {
            // you can leave out parameters, they are simply ignored
            tempVe = null;
        } else {
            tempVe = OperandResolver.coerceValueToBoolean(arg, false);
        }
        if (tempVe != null) {
            result = partialEvaluate(result, tempVe.booleanValue());
            atleastOneNonBlank = true;
        }
    }
    if (!atleastOneNonBlank) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    return result;
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 8 with EvaluationException

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

the class Fixed method fixed.

private ValueEval fixed(ValueEval numberParam, ValueEval placesParam, ValueEval skipThousandsSeparatorParam, int srcRowIndex, int srcColumnIndex) {
    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);
        // Round number to respective places.
        number = number.setScale(places, RoundingMode.HALF_UP);
        // Format number conditionally using a thousands separator.
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
        DecimalFormat formatter = (DecimalFormat) nf;
        formatter.setGroupingUsed(!(skipThousandsSeparator != null && skipThousandsSeparator));
        formatter.setMinimumFractionDigits(places >= 0 ? places : 0);
        formatter.setMaximumFractionDigits(places >= 0 ? places : 0);
        String numberString = formatter.format(number.doubleValue());
        // Return the result as a StringEval.
        return new StringEval(numberString);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat)

Example 9 with EvaluationException

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

the class Hlookup method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2, ValueEval arg3) {
    try {
        // Evaluation order:
        // arg0 lookup_value, arg1 table_array, arg3 range_lookup, find lookup value, arg2 row_index, fetch result
        ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        TwoDEval tableArray = LookupUtils.resolveTableArrayArg(arg1);
        boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcRowIndex, srcColumnIndex);
        int colIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createRowVector(tableArray, 0), isRangeLookup);
        int rowIndex = LookupUtils.resolveRowOrColIndexArg(arg2, srcRowIndex, srcColumnIndex);
        ValueVector resultCol = createResultColumnVector(tableArray, rowIndex);
        return resultCol.getItem(colIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) TwoDEval(org.apache.poi.ss.formula.TwoDEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 10 with EvaluationException

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

the class ImReal method evaluate.

public 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 = Imaginary.COMPLEX_NUMBER_PATTERN.matcher(iNumber);
    boolean result = m.matches();
    String real = "";
    if (result == true) {
        String realGroup = m.group(2);
        boolean hasRealPart = realGroup.length() != 0;
        if (realGroup.length() == 0) {
            return new StringEval(String.valueOf(0));
        }
        if (hasRealPart) {
            String sign = "";
            String realSign = m.group(Imaginary.GROUP1_REAL_SIGN);
            if (realSign.length() != 0 && !(realSign.equals("+"))) {
                sign = realSign;
            }
            String groupRealNumber = m.group(Imaginary.GROUP2_IMAGINARY_INTEGER_OR_DOUBLE);
            if (groupRealNumber.length() != 0) {
                real = sign + groupRealNumber;
            } else {
                real = sign + "1";
            }
        }
    } else {
        return ErrorEval.NUM_ERROR;
    }
    return new StringEval(real);
}
Also used : Matcher(java.util.regex.Matcher) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

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