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