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

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


      return ErrorEval.VALUE_INVALID;
    }

    Function innerFunc;
    try {
      ValueEval ve = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex);
      int functionCode = OperandResolver.coerceValueToInt(ve);
      innerFunc = findFunction(functionCode);
    } catch (EvaluationException e) {
      return e.getErrorEval();
    }
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

        for (int sIx=areaEval.getFirstSheetIndex(); sIx <= areaEval.getLastSheetIndex(); sIx++) {
            int height = areaEval.getHeight();
            int width = areaEval.getWidth();
            for (int rrIx=0; rrIx<height; rrIx++) {
                for (int rcIx=0; rcIx<width; rcIx++) {
                    ValueEval ve = areaEval.getValue(sIx, rrIx, rcIx);
   
                    if(criteriaPredicate instanceof I_MatchAreaPredicate){
                        I_MatchAreaPredicate areaPredicate = (I_MatchAreaPredicate)criteriaPredicate;
                        if(!areaPredicate.matches(areaEval, rrIx, rcIx)) continue;
                    }
View Full Code Here

   */
  public static int countMatchingCellsInRef(RefEval refEval, I_MatchPredicate criteriaPredicate) {
      int result = 0;
     
      for (int sIx = refEval.getFirstSheetIndex(); sIx <= refEval.getLastSheetIndex(); sIx++) {
          ValueEval ve = refEval.getInnerValueEval(sIx);
            if(criteriaPredicate.matches(ve)) {
                result++;
            }
      }
    return result;
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

    // avoid tracking dependencies to cells that have constant definition
    boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true
          : !_stabilityClassifier.isCellFinal(sheetIndex, rowIndex, columnIndex);
    if (srcCell == null || srcCell.getCellType() != Cell.CELL_TYPE_FORMULA) {
      ValueEval result = getValueFromNonFormulaCell(srcCell);
      if (shouldCellDependencyBeRecorded) {
        tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);
      }
      return result;
    }

    FormulaCellCacheEntry cce = _cache.getOrCreateFormulaCellEntry(srcCell);
    if (shouldCellDependencyBeRecorded || cce.isInputSensitive()) {
      tracker.acceptFormulaDependency(cce);
    }
    IEvaluationListener evalListener = _evaluationListener;
    ValueEval result;
    if (cce.getValue() == null) {
      if (!tracker.startEvaluate(cce)) {
        return ErrorEval.CIRCULAR_REF_ERROR;
      }
      OperationEvaluationContext ec = new OperationEvaluationContext(this, _workbook, sheetIndex, rowIndex, columnIndex, tracker);

      try {

        Ptg[] ptgs = _workbook.getFormulaTokens(srcCell);
        if (evalListener == null) {
          result = evaluateFormula(ec, ptgs);
        } else {
          evalListener.onStartEvaluate(srcCell, cce);
          result = evaluateFormula(ec, ptgs);
          evalListener.onEndEvaluate(cce, result);
        }

        tracker.updateCacheResult(result);
      }
       catch (NotImplementedException e) {
        throw addExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
       } catch (RuntimeException re) {
         if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) {
           logInfo(re.getCause().getMessage() + " - Continuing with cached value!");
           switch(srcCell.getCachedFormulaResultType()) {
             case Cell.CELL_TYPE_NUMERIC:
               result = new NumberEval(srcCell.getNumericCellValue());
               break;
             case Cell.CELL_TYPE_STRING:
               result =  new StringEval(srcCell.getStringCellValue());
               break;
             case Cell.CELL_TYPE_BLANK:
               result = BlankEval.instance;
               break;
             case Cell.CELL_TYPE_BOOLEAN:
               result =  BoolEval.valueOf(srcCell.getBooleanCellValue());
               break;
             case Cell.CELL_TYPE_ERROR:
              result =  ErrorEval.valueOf(srcCell.getErrorCellValue());
              break;
             case Cell.CELL_TYPE_FORMULA:
            default:
              throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
           }
         } else {
           throw re;
         }
       } finally {
        tracker.endEvaluate(cce);
      }
    } else {
      if(evalListener != null) {
        evalListener.onCacheHit(sheetIndex, rowIndex, columnIndex, cce.getValue());
      }
      return cce.getValue();
    }
    if (isDebugLogEnabled()) {
      String sheetName = getSheetName(sheetIndex);
      CellReference cr = new CellReference(rowIndex, columnIndex);
      logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + result.toString());
    }
    // Usually (result === cce.getValue())
    // But sometimes: (result==ErrorEval.CIRCULAR_REF_ERROR, cce.getValue()==null)
    // When circular references are detected, the cache entry is only updated for
    // the top evaluation frame
View Full Code Here

          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
          continue;
        }
      }
      if (ptg instanceof ControlPtg) {
        // skip Parentheses, Attr, etc
        continue;
      }
      if (ptg instanceof MemFuncPtg || ptg instanceof MemAreaPtg) {
        // can ignore, rest of tokens for this expression are in OK RPN order
        continue;
      }
      if (ptg instanceof MemErrPtg) {
        continue;
      }

      ValueEval opResult;
      if (ptg instanceof OperationPtg) {
        OperationPtg optg = (OperationPtg) ptg;

        if (optg instanceof UnionPtg) { continue; }


        int numops = optg.getNumberOfOperands();
        ValueEval[] ops = new ValueEval[numops];

        // storing the ops in reverse order since they are popping
        for (int j = numops - 1; j >= 0; j--) {
          ValueEval p = stack.pop();
          ops[j] = p;
        }
//        logDebug("invoke " + operation + " (nAgs=" + numops + ")");
        opResult = OperationEvaluatorFactory.evaluate(optg, ops, ec);
      } else {
        opResult = getEvalForPtg(ptg, ec);
      }
      if (opResult == null) {
        throw new RuntimeException("Evaluation result must not be null");
      }
//      logDebug("push " + opResult);
      stack.push(opResult);
      if (dbgEvaluationOutputIndent > 0) {
        EVAL_LOG.log(POILogger.INFO, dbgIndentStr + "    = " + opResult);
      }
    }

    ValueEval value = stack.pop();
    if (!stack.isEmpty()) {
      throw new IllegalStateException("evaluation stack not empty");
    }
    ValueEval result = dereferenceResult(value, ec.getRowIndex(), ec.getColumnIndex());
    if (dbgEvaluationOutputIndent > 0) {
      EVAL_LOG.log(POILogger.INFO, dbgIndentStr + "finshed eval of "
              + new CellReference(ec.getRowIndex(), ec.getColumnIndex()).formatAsString()
              + ": " + result);
      dbgEvaluationOutputIndent--;
View Full Code Here

   * @return a {@link NumberEval}, {@link StringEval}, {@link BoolEval}, or
   *         {@link ErrorEval}. Never <code>null</code>. {@link BlankEval} is
   *         converted to {@link NumberEval#ZERO}
   */
  public static ValueEval dereferenceResult(ValueEval evaluationResult, int srcRowNum, int srcColNum) {
    ValueEval value;
    try {
      value = OperandResolver.getSingleValue(evaluationResult, srcRowNum, srcColNum);
    } catch (EvaluationException e) {
      return e.getErrorEval();
    }
View Full Code Here

    /*
     * Note: no short-circuit boolean loop exit because any ErrorEvals will override the result
     */
    for (int i=0, iSize=args.length; i<iSize; i++) {
            Boolean tempVe;
      ValueEval arg = args[i];
      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;
                for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
                    ValueEval ve = re.getInnerValueEval(sIx);
                    tempVe = OperandResolver.coerceValueToBoolean(ve, true);
                    if (tempVe != null) {
                        result = partialEvaluate(result, tempVe.booleanValue());
                        atleastOneNonBlank = true;
                    }
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.