Ejemplo n.º 1
0
 /**
  * Gets the value from a non-formula cell.
  *
  * @param cell may be <code>null</code>
  * @return {@link BlankEval} if cell is <code>null</code> or blank, never <code>null</code>
  */
 /* package */ static ValueEval getValueFromNonFormulaCell(EvaluationCell cell) {
   if (cell == null) {
     return BlankEval.INSTANCE;
   }
   int cellType = cell.getCellType();
   switch (cellType) {
     case Cell.CELL_TYPE_NUMERIC:
       return new NumberEval(cell.getNumericCellValue());
     case Cell.CELL_TYPE_STRING:
       return new StringEval(cell.getStringCellValue());
     case Cell.CELL_TYPE_BOOLEAN:
       return BoolEval.valueOf(cell.getBooleanCellValue());
     case Cell.CELL_TYPE_BLANK:
       return BlankEval.INSTANCE;
     case Cell.CELL_TYPE_ERROR:
       return ErrorEval.valueOf(cell.getErrorCellValue());
   }
   throw new RuntimeException("Unexpected cell type (" + cellType + ")");
 }
Ejemplo n.º 2
0
  /**
   * returns an appropriate Eval impl instance for the Ptg. The Ptg must be one of: Area3DPtg,
   * AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg, StringPtg, BoolPtg <br>
   * special Note: OperationPtg subtypes cannot be passed here!
   */
  private ValueEval getEvalForPtg(Ptg ptg, OperationEvaluationContext ec) {
    //  consider converting all these (ptg instanceof XxxPtg) expressions to (ptg.getClass() ==
    // XxxPtg.class)

    if (ptg instanceof NamePtg) {
      // named ranges, macro functions
      NamePtg namePtg = (NamePtg) ptg;
      EvaluationName nameRecord = _workbook.getName(namePtg);
      if (nameRecord.isFunctionName()) {
        return new NameEval(nameRecord.getNameText());
      }
      if (nameRecord.hasFormula()) {
        return evaluateNameFormula(nameRecord.getNameDefinition(), ec);
      }

      throw new RuntimeException(
          "Don't now how to evalate name '" + nameRecord.getNameText() + "'");
    }
    if (ptg instanceof NameXPtg) {
      return new NameXEval(((NameXPtg) ptg));
    }

    if (ptg instanceof IntPtg) {
      return new NumberEval(((IntPtg) ptg).getValue());
    }
    if (ptg instanceof NumberPtg) {
      return new NumberEval(((NumberPtg) ptg).getValue());
    }
    if (ptg instanceof StringPtg) {
      return new StringEval(((StringPtg) ptg).getValue());
    }
    if (ptg instanceof BoolPtg) {
      return BoolEval.valueOf(((BoolPtg) ptg).getValue());
    }
    if (ptg instanceof ErrPtg) {
      return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
    }
    if (ptg instanceof MissingArgPtg) {
      return MissingArgEval.instance;
    }
    if (ptg instanceof AreaErrPtg
        || ptg instanceof RefErrorPtg
        || ptg instanceof DeletedArea3DPtg
        || ptg instanceof DeletedRef3DPtg) {
      return ErrorEval.REF_INVALID;
    }
    if (ptg instanceof Ref3DPtg) {
      Ref3DPtg refPtg = (Ref3DPtg) ptg;
      SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(refPtg);
      return new LazyRefEval(refPtg, sre);
    }
    if (ptg instanceof Area3DPtg) {
      Area3DPtg aptg = (Area3DPtg) ptg;
      SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(aptg);
      return new LazyAreaEval(aptg, sre);
    }
    SheetRefEvaluator sre = ec.getRefEvaluatorForCurrentSheet();
    if (ptg instanceof RefPtg) {
      return new LazyRefEval(((RefPtg) ptg), sre);
    }
    if (ptg instanceof AreaPtg) {
      return new LazyAreaEval(((AreaPtg) ptg), sre);
    }

    if (ptg instanceof UnknownPtg) {
      // POI uses UnknownPtg when the encoded Ptg array seems to be corrupted.
      // This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment
      // 21790)
      // In any case, formulas are re-parsed before execution, so UnknownPtg should not get here
      throw new RuntimeException("UnknownPtg not allowed");
    }
    if (ptg instanceof ExpPtg) {
      // ExpPtg is used for array formulas and shared formulas.
      // it is currently unsupported, and may not even get implemented here
      throw new RuntimeException("ExpPtg currently not supported");
    }

    throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
  }