Esempio 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 + ")");
 }
Esempio 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() + ")");
  }
Esempio n. 3
0
  /** Retrieves the text contents of the file */
  public String getText() {
    StringBuffer text = new StringBuffer();

    // We don't care about the difference between
    //  null (missing) and blank cells
    _wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);

    // Process each sheet in turn
    for (int i = 0; i < _wb.getNumberOfSheets(); i++) {
      HSSFSheet sheet = _wb.getSheetAt(i);
      if (sheet == null) {
        continue;
      }

      if (_includeSheetNames) {
        String name = _wb.getSheetName(i);
        if (name != null) {
          text.append(name);
          text.append("\n");
        }
      }

      // Header text, if there is any
      if (_includeHeadersFooters) {
        text.append(_extractHeaderFooter(sheet.getHeader()));
      }

      int firstRow = sheet.getFirstRowNum();
      int lastRow = sheet.getLastRowNum();
      for (int j = firstRow; j <= lastRow; j++) {
        HSSFRow row = sheet.getRow(j);
        if (row == null) {
          continue;
        }

        // Check each cell in turn
        int firstCell = row.getFirstCellNum();
        int lastCell = row.getLastCellNum();
        if (_includeBlankCells) {
          firstCell = 0;
        }

        for (int k = firstCell; k < lastCell; k++) {
          HSSFCell cell = row.getCell(k);
          boolean outputContents = true;

          if (cell == null) {
            // Only output if requested
            outputContents = _includeBlankCells;
          } else {
            switch (cell.getCellType()) {
              case HSSFCell.CELL_TYPE_STRING:
                text.append(cell.getRichStringCellValue().getString());
                break;
              case HSSFCell.CELL_TYPE_NUMERIC:
                text.append(_formatter.formatCellValue(cell));
                break;
              case HSSFCell.CELL_TYPE_BOOLEAN:
                text.append(cell.getBooleanCellValue());
                break;
              case HSSFCell.CELL_TYPE_ERROR:
                text.append(ErrorEval.getText(cell.getErrorCellValue()));
                break;
              case HSSFCell.CELL_TYPE_FORMULA:
                if (!_shouldEvaluateFormulas) {
                  text.append(cell.getCellFormula());
                } else {
                  switch (cell.getCachedFormulaResultType()) {
                    case HSSFCell.CELL_TYPE_STRING:
                      HSSFRichTextString str = cell.getRichStringCellValue();
                      if (str != null && str.length() > 0) {
                        text.append(str.toString());
                      }
                      break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                      HSSFCellStyle style = cell.getCellStyle();
                      if (style == null) {
                        text.append(cell.getNumericCellValue());
                      } else {
                        text.append(
                            _formatter.formatRawCellContents(
                                cell.getNumericCellValue(),
                                style.getDataFormat(),
                                style.getDataFormatString()));
                      }
                      break;
                    case HSSFCell.CELL_TYPE_BOOLEAN:
                      text.append(cell.getBooleanCellValue());
                      break;
                    case HSSFCell.CELL_TYPE_ERROR:
                      text.append(ErrorEval.getText(cell.getErrorCellValue()));
                      break;
                  }
                }
                break;
              default:
                throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
            }

            // Output the comment, if requested and exists
            HSSFComment comment = cell.getCellComment();
            if (_includeCellComments && comment != null) {
              // Replace any newlines with spaces, otherwise it
              //  breaks the output
              String commentText = comment.getString().getString().replace('\n', ' ');
              text.append(" Comment by " + comment.getAuthor() + ": " + commentText);
            }
          }

          // Output a tab if we're not on the last cell
          if (outputContents && k < (lastCell - 1)) {
            text.append("\t");
          }
        }

        // Finish off the row
        text.append("\n");
      }

      // Finally Footer text, if there is any
      if (_includeHeadersFooters) {
        text.append(_extractHeaderFooter(sheet.getFooter()));
      }
    }

    return text.toString();
  }