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;
}
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);
}
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());
}
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();
}
}
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);
}
Aggregations