Search in sources :

Example 11 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval 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 12 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval 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 13 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval 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 14 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval 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 15 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval 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

ValueEval (org.apache.poi.ss.formula.eval.ValueEval)223 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)101 StringEval (org.apache.poi.ss.formula.eval.StringEval)48 Test (org.junit.Test)39 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)36 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)29 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)28 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)27 Calendar (java.util.Calendar)16 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)13 Date (java.util.Date)11 RefEval (org.apache.poi.ss.formula.eval.RefEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)4 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)4 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)4 ArrayList (java.util.ArrayList)3 AssertionFailedError (junit.framework.AssertionFailedError)3 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3