use of org.apache.poi.ss.formula.eval.RefEval 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.RefEval in project poi by apache.
the class Match method evaluateLookupRange.
private static ValueVector evaluateLookupRange(ValueEval eval) throws EvaluationException {
if (eval instanceof RefEval) {
RefEval re = (RefEval) eval;
if (re.getNumberOfSheets() == 1) {
return new SingleValueVector(re.getInnerValueEval(re.getFirstSheetIndex()));
} else {
return LookupUtils.createVector(re);
}
}
if (eval instanceof TwoDEval) {
ValueVector result = LookupUtils.createVector((TwoDEval) eval);
if (result == null) {
throw new EvaluationException(ErrorEval.NA);
}
return result;
}
// Error handling for lookup_range arg is also unusual
if (eval instanceof NumericValueEval) {
throw new EvaluationException(ErrorEval.NA);
}
if (eval instanceof StringEval) {
StringEval se = (StringEval) eval;
Double d = OperandResolver.parseDouble(se.getStringValue());
if (d == null) {
// plain string
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
// else looks like a number
throw new EvaluationException(ErrorEval.NA);
}
throw new RuntimeException("Unexpected eval type (" + eval + ")");
}
use of org.apache.poi.ss.formula.eval.RefEval in project poi by apache.
the class Mode method collectValues.
private static void collectValues(ValueEval arg, List<Double> temp) throws EvaluationException {
if (arg instanceof TwoDEval) {
TwoDEval ae = (TwoDEval) arg;
int width = ae.getWidth();
int height = ae.getHeight();
for (int rrIx = 0; rrIx < height; rrIx++) {
for (int rcIx = 0; rcIx < width; rcIx++) {
ValueEval ve1 = ae.getValue(rrIx, rcIx);
collectValue(ve1, temp, false);
}
}
return;
}
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++) {
collectValue(re.getInnerValueEval(sIx), temp, true);
}
return;
}
collectValue(arg, temp, true);
}
use of org.apache.poi.ss.formula.eval.RefEval in project poi by apache.
the class MultiOperandNumericFunction method collectValues.
/**
* Collects values from a single argument
*/
private void collectValues(ValueEval operand, DoubleList temp) throws EvaluationException {
if (operand instanceof ThreeDEval) {
ThreeDEval ae = (ThreeDEval) operand;
for (int sIx = ae.getFirstSheetIndex(); sIx <= ae.getLastSheetIndex(); sIx++) {
int width = ae.getWidth();
int height = ae.getHeight();
for (int rrIx = 0; rrIx < height; rrIx++) {
for (int rcIx = 0; rcIx < width; rcIx++) {
ValueEval ve = ae.getValue(sIx, rrIx, rcIx);
if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
continue;
collectValue(ve, true, temp);
}
}
}
return;
}
if (operand instanceof TwoDEval) {
TwoDEval ae = (TwoDEval) operand;
int width = ae.getWidth();
int height = ae.getHeight();
for (int rrIx = 0; rrIx < height; rrIx++) {
for (int rcIx = 0; rcIx < width; rcIx++) {
ValueEval ve = ae.getValue(rrIx, rcIx);
if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
continue;
collectValue(ve, true, temp);
}
}
return;
}
if (operand instanceof RefEval) {
RefEval re = (RefEval) operand;
for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
collectValue(re.getInnerValueEval(sIx), true, temp);
}
return;
}
collectValue(operand, false, temp);
}
use of org.apache.poi.ss.formula.eval.RefEval in project poi by apache.
the class Sumproduct method evaluate.
public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
int maxN = args.length;
if (maxN < 1) {
return ErrorEval.VALUE_INVALID;
}
ValueEval firstArg = args[0];
try {
if (firstArg instanceof NumericValueEval) {
return evaluateSingleProduct(args);
}
if (firstArg instanceof RefEval) {
return evaluateSingleProduct(args);
}
if (firstArg instanceof TwoDEval) {
TwoDEval ae = (TwoDEval) firstArg;
if (ae.isRow() && ae.isColumn()) {
return evaluateSingleProduct(args);
}
return evaluateAreaSumProduct(args);
}
} catch (EvaluationException e) {
return e.getErrorEval();
}
throw new RuntimeException("Invalid arg type for SUMPRODUCT: (" + firstArg.getClass().getName() + ")");
}
Aggregations