Search in sources :

Example 1 with StringEval

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

the class EvaluationConditionalFormatRule method checkValue.

/**
     * @param cell the cell to check for
     * @param region for adjusting relative formulas
     * @return if the value of the cell is valid or not for the formatting rule
     */
private boolean checkValue(Cell cell, CellRangeAddress region) {
    if (cell == null || DataValidationEvaluator.isType(cell, CellType.BLANK) || DataValidationEvaluator.isType(cell, CellType.ERROR) || (DataValidationEvaluator.isType(cell, CellType.STRING) && (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty()))) {
        return false;
    }
    ValueEval eval = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ConditionalFormattingEvaluator.getRef(cell), region));
    String f2 = rule.getFormula2();
    ValueEval eval2 = null;
    if (f2 != null && f2.length() > 0) {
        eval2 = unwrapEval(workbookEvaluator.evaluate(f2, ConditionalFormattingEvaluator.getRef(cell), region));
    }
    // we assume the cell has been evaluated, and the current formula value stored
    if (DataValidationEvaluator.isType(cell, CellType.BOOLEAN)) {
        if (eval instanceof BoolEval && (eval2 == null || eval2 instanceof BoolEval)) {
            return operator.isValid(cell.getBooleanCellValue(), ((BoolEval) eval).getBooleanValue(), eval2 == null ? null : ((BoolEval) eval2).getBooleanValue());
        }
        // wrong types
        return false;
    }
    if (DataValidationEvaluator.isType(cell, CellType.NUMERIC)) {
        if (eval instanceof NumberEval && (eval2 == null || eval2 instanceof NumberEval)) {
            return operator.isValid(cell.getNumericCellValue(), ((NumberEval) eval).getNumberValue(), eval2 == null ? null : ((NumberEval) eval2).getNumberValue());
        }
        // wrong types
        return false;
    }
    if (DataValidationEvaluator.isType(cell, CellType.STRING)) {
        if (eval instanceof StringEval && (eval2 == null || eval2 instanceof StringEval)) {
            return operator.isValid(cell.getStringCellValue(), ((StringEval) eval).getStringValue(), eval2 == null ? null : ((StringEval) eval2).getStringValue());
        }
        // wrong types
        return false;
    }
    // should not get here, but in case...
    return false;
}
Also used : BoolEval(org.apache.poi.ss.formula.eval.BoolEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 2 with StringEval

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

the class DataValidationEvaluator method getValidationValuesForConstraint.

/**
     * static so enums can reference it without creating a whole instance
     * @return returns an unmodifiable {@link List} of {@link ValueEval}s, which may be empty
     */
protected static List<ValueEval> getValidationValuesForConstraint(DataValidationContext context) {
    final DataValidationConstraint val = context.getValidation().getValidationConstraint();
    if (val.getValidationType() != ValidationType.LIST)
        return null;
    String formula = val.getFormula1();
    final List<ValueEval> values = new ArrayList<ValueEval>();
    if (val.getExplicitListValues() != null && val.getExplicitListValues().length > 0) {
        // assumes parsing interprets the overloaded property right for XSSF
        for (String s : val.getExplicitListValues()) {
            // constructor throws exception on null
            if (s != null)
                values.add(new StringEval(s));
        }
    } else if (formula != null) {
        // evaluate formula for cell refs then get their values
        ValueEval eval = context.getEvaluator().getWorkbookEvaluator().evaluate(formula, context.getTarget(), context.getRegion());
        // there is no way from the model to tell if the list is fixed values or formula based.
        if (eval instanceof TwoDEval) {
            TwoDEval twod = (TwoDEval) eval;
            for (int i = 0; i < twod.getHeight(); i++) {
                final ValueEval cellValue = twod.getValue(i, 0);
                values.add(cellValue);
            }
        }
    }
    return Collections.unmodifiableList(values);
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) ArrayList(java.util.ArrayList) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Example 3 with StringEval

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

the class Complex method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval real_num, ValueEval i_num, ValueEval suffix) {
    ValueEval veText1;
    try {
        veText1 = OperandResolver.getSingleValue(real_num, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    double realNum = 0;
    try {
        realNum = OperandResolver.coerceValueToDouble(veText1);
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
    ValueEval veINum;
    try {
        veINum = OperandResolver.getSingleValue(i_num, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    double realINum = 0;
    try {
        realINum = OperandResolver.coerceValueToDouble(veINum);
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
    String suffixValue = OperandResolver.coerceValueToString(suffix);
    if (suffixValue.length() == 0) {
        suffixValue = DEFAULT_SUFFIX;
    }
    if (suffixValue.equals(DEFAULT_SUFFIX.toUpperCase(Locale.ROOT)) || suffixValue.equals(SUPPORTED_SUFFIX.toUpperCase(Locale.ROOT))) {
        return ErrorEval.VALUE_INVALID;
    }
    if (!(suffixValue.equals(DEFAULT_SUFFIX) || suffixValue.equals(SUPPORTED_SUFFIX))) {
        return ErrorEval.VALUE_INVALID;
    }
    StringBuffer strb = new StringBuffer("");
    if (realNum != 0) {
        if (isDoubleAnInt(realNum)) {
            strb.append((int) realNum);
        } else {
            strb.append(realNum);
        }
    }
    if (realINum != 0) {
        if (strb.length() != 0) {
            if (realINum > 0) {
                strb.append("+");
            }
        }
        if (realINum != 1 && realINum != -1) {
            if (isDoubleAnInt(realINum)) {
                strb.append((int) realINum);
            } else {
                strb.append(realINum);
            }
        }
        strb.append(suffixValue);
    }
    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 4 with StringEval

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

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

StringEval (org.apache.poi.ss.formula.eval.StringEval)58 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)48 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)29 Test (org.junit.Test)18 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)13 Calendar (java.util.Calendar)10 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)9 Date (java.util.Date)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)7 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3 RefEval (org.apache.poi.ss.formula.eval.RefEval)3 BigDecimal (java.math.BigDecimal)1 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 NumberFormat (java.text.NumberFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1