Package org.apache.poi.ss.formula.eval

Examples of org.apache.poi.ss.formula.eval.ValueEval


  /** "1,0000" is valid, "1,00" is not */
  private static final int MIN_DISTANCE_BETWEEN_THOUSANDS_SEPARATOR = 4;
  private static final Double ZERO = new Double(0.0);

  public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
    ValueEval veText;
    try {
      veText = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
      return e.getErrorEval();
    }
View Full Code Here


      row.createCell(i).setCellFormula(String.valueOf(i));
    }

    EvalCountListener evalListener = new EvalCountListener();
    WorkbookEvaluator evaluator = WorkbookEvaluatorTestHelper.createEvaluator(wb, evalListener);
    ValueEval ve = evaluator.evaluate(HSSFEvaluationTestHelper.wrapCell(cellA1));
    int evalCount = evalListener.getEvalCount();
    if (evalCount == 6) {
      // Without short-circuit-if evaluation, evaluating cell 'A1' takes 3 extra evaluations (for D1,E1,F1)
      throw new AssertionFailedError("Identifed bug 48195 - Formula evaluator should short-circuit IF() calculations.");
    }
View Full Code Here

        if(!(cell instanceof XSSFCell)){
            throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." +
                    " Only XSSFCells can be evaluated.");
        }

    ValueEval eval = _bookEvaluator.evaluate(new XSSFEvaluationCell((XSSFCell) cell));
    if (eval instanceof NumberEval) {
      NumberEval ne = (NumberEval) eval;
      return new CellValue(ne.getNumberValue());
    }
    if (eval instanceof BoolEval) {
      BoolEval be = (BoolEval) eval;
      return CellValue.valueOf(be.getBooleanValue());
    }
    if (eval instanceof StringEval) {
      StringEval ne = (StringEval) eval;
      return new CellValue(ne.getStringValue());
    }
    if (eval instanceof ErrorEval) {
      return CellValue.getError(((ErrorEval)eval).getErrorCode());
    }
    throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
  }
View Full Code Here

    for (int i = 0; i < TEST_VALUES0.length; i++) {
      values[i] = new NumberEval(TEST_VALUES0[i]);
    }

    AreaEval arg1 = EvalFactory.createAreaEval("C1:D5", values);
    ValueEval args[] = { new NumberEval(function), arg1 };

    ValueEval result = new Subtotal().evaluate(args, 0, 0);

    assertEquals(NumberEval.class, result.getClass());
    assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
  }
View Full Code Here

  /**
   * Returns a CellValue wrapper around the supplied ValueEval instance.
   * @param cell
   */
  private CellValue evaluateFormulaCellValue(Cell cell) {
    ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));
    if (eval instanceof BoolEval) {
      BoolEval be = (BoolEval) eval;
      return CellValue.valueOf(be.getBooleanValue());
    }
    if (eval instanceof NumericValueEval) {
      NumericValueEval ne = (NumericValueEval) eval;
      return new CellValue(ne.getNumberValue());
    }
    if (eval instanceof StringValueEval) {
      StringValueEval ne = (StringValueEval) eval;
      return new CellValue(ne.getStringValue());
    }
    if (eval instanceof ErrorEval) {
      return CellValue.getError(((ErrorEval)eval).getErrorCode());
    }
    throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
  }
View Full Code Here

    boolean matchExact = match_type == 0;
    // Note - Excel does not strictly require -1 and +1
    boolean findLargestLessThanOrEqual = match_type > 0;

    try {
      ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
      ValueVector lookupRange = evaluateLookupRange(arg1);
      int index = findIndexOfValue(lookupValue, lookupRange, matchExact, findLargestLessThanOrEqual);
      return new NumberEval(index + 1); // +1 to convert to 1-based
    } catch (EvaluationException e) {
      return e.getErrorEval();
View Full Code Here



  private static double evaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol)
      throws EvaluationException {
    ValueEval match_type = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);

    if(match_type instanceof ErrorEval) {
      throw new EvaluationException((ErrorEval)match_type);
    }
    if(match_type instanceof NumericValueEval) {
      NumericValueEval ne = (NumericValueEval) match_type;
      return ne.getNumberValue();
    }
    if (match_type instanceof StringEval) {
      StringEval se = (StringEval) match_type;
      Double d = OperandResolver.parseDouble(se.getStringValue());
      if(d == null) {
        // plain string
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
      }
      // if the string parses as a number, it is OK
      return d.doubleValue();
    }
    throw new RuntimeException("Unexpected match_type type (" + match_type.getClass().getName() + ")");
  }
View Full Code Here

    boolean accumlatedSome = false;
        // first pass: read in data, compute xbar and ybar
        double sumx = 0.0, sumy = 0.0;
       
    for (int i = 0; i < size; i++) {
      ValueEval vx = x.getItem(i);
      ValueEval vy = y.getItem(i);
      if (vx instanceof ErrorEval) {
        if (firstXerr == null) {
          firstXerr = (ErrorEval) vx;
          continue;
        }
      }
      if (vy instanceof ErrorEval) {
        if (firstYerr == null) {
          firstYerr = (ErrorEval) vy;
          continue;
        }
      }
      // only count pairs if both elements are numbers
      if (vx instanceof NumberEval && vy instanceof NumberEval) {
        accumlatedSome = true;
        NumberEval nx = (NumberEval) vx;
        NumberEval ny = (NumberEval) vy;
        sumx  += nx.getNumberValue();
              sumy  += ny.getNumberValue();
      } else {
        // all other combinations of value types are silently ignored
      }
    }
    double xbar = sumx / size;
        double ybar = sumy / size;
   
     // second pass: compute summary statistics
        double xxbar = 0.0, xybar = 0.0;
        for (int i = 0; i < size; i++) {
      ValueEval vx = x.getItem(i);
      ValueEval vy = y.getItem(i);
     
      if (vx instanceof ErrorEval) {
        if (firstXerr == null) {
          firstXerr = (ErrorEval) vx;
          continue;
View Full Code Here

  public static int resolveRowOrColIndexArg(ValueEval rowColIndexArg, int srcCellRow, int srcCellCol) throws EvaluationException {
    if(rowColIndexArg == null) {
      throw new IllegalArgumentException("argument must not be null");
    }

    ValueEval veRowColIndexArg;
    try {
      veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short)srcCellCol);
    } catch (EvaluationException e) {
      // All errors get translated to #REF!
      throw EvaluationException.invalidRef();
View Full Code Here

   * Resolves the last (optional) parameter (<b>range_lookup</b>) to the VLOOKUP and HLOOKUP functions.
   * @param rangeLookupArg must not be <code>null</code>
   */
  public static boolean resolveRangeLookupArg(ValueEval rangeLookupArg, int srcCellRow, int srcCellCol) throws EvaluationException {

    ValueEval valEval = OperandResolver.getSingleValue(rangeLookupArg, srcCellRow, srcCellCol);
    if(valEval instanceof BlankEval) {
      // Tricky:
      // fourth arg supplied but evaluates to blank
      // this does not get the default value
      return false;
    }
    if(valEval instanceof BoolEval) {
      // Happy day flow
      BoolEval boolEval = (BoolEval) valEval;
      return boolEval.getBooleanValue();
    }

    if (valEval instanceof StringEval) {
      String stringValue = ((StringEval) valEval).getStringValue();
      if(stringValue.length() < 1) {
        // More trickiness:
        // Empty string is not the same as BlankEval.  It causes #VALUE! error
        throw EvaluationException.invalidValue();
      }
      // TODO move parseBoolean to OperandResolver
      Boolean b = Countif.parseBoolean(stringValue);
      if(b != null) {
        // string converted to boolean OK
        return b.booleanValue();
      }
      // Even more trickiness:
      // Note - even if the StringEval represents a number value (for example "1"),
      // Excel does not resolve it to a boolean.
      throw EvaluationException.invalidValue();
      // This is in contrast to the code below,, where NumberEvals values (for
      // example 0.01) *do* resolve to equivalent boolean values.
    }
    if (valEval instanceof NumericValueEval) {
      NumericValueEval nve = (NumericValueEval) valEval;
      // zero is FALSE, everything else is TRUE
      return 0.0 != nve.getNumberValue();
    }
    throw new RuntimeException("Unexpected eval type (" + valEval.getClass().getName() + ")");
  }
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.formula.eval.ValueEval

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.