/** * Get the value of the cell as a number. * * <p>For strings we throw an exception. For blank cells we return a 0. For formulas or error * cells we return the precalculated value; * * @return the value of the cell as a number * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is * CELL_TYPE_STRING * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see org.zkoss.poi.ss.usermodel.DataFormatter for turning this number into a string similar to * that which Excel would render this number as. */ public double getNumericCellValue() { int cellType = getCellType(); switch (cellType) { case CELL_TYPE_BLANK: return 0.0; case CELL_TYPE_FORMULA: { FormulaValue fv = (FormulaValue) _value; if (fv.getFormulaType() != CELL_TYPE_NUMERIC) throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, false); return ((NumericFormulaValue) _value).getPreEvaluatedValue(); } case CELL_TYPE_NUMERIC: return ((NumericValue) _value).getValue(); default: throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false); } }
/** * Sets formula for this cell. * * <p>Note, this method only sets the formula string and does not calculate the formula value. To * set the precalculated value use {@link #setCellValue(double)} or {@link #setCellValue(String)} * * @param formula the formula to set, e.g. <code>"SUM(C4:E4)"</code>. If the argument is <code> * null</code> then the current formula is removed. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid */ public void setCellFormula(String formula) throws FormulaParseException { if (formula == null) { setType(Cell.CELL_TYPE_BLANK); return; } ensureFormulaType(computeTypeFromFormula(formula)); ((FormulaValue) _value).setValue(formula); }
/** * Get the value of the cell as an error code. * * <p>For strings, numbers, and booleans, we throw an exception. For blank cells we return a 0. * * @return the value of the cell as an error code * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't * CELL_TYPE_ERROR * @see org.zkoss.poi.ss.usermodel.FormulaError for error codes */ public byte getErrorCellValue() { int cellType = getCellType(); switch (cellType) { case CELL_TYPE_BLANK: return 0; case CELL_TYPE_FORMULA: { FormulaValue fv = (FormulaValue) _value; if (fv.getFormulaType() != CELL_TYPE_ERROR) throw typeMismatch(CELL_TYPE_ERROR, CELL_TYPE_FORMULA, false); return ((ErrorFormulaValue) _value).getPreEvaluatedValue(); } case CELL_TYPE_ERROR: { return ((ErrorValue) _value).getValue(); } default: throw typeMismatch(CELL_TYPE_ERROR, cellType, false); } }
/** * Get the value of the cell as a boolean. * * <p>For strings, numbers, and errors, we throw an exception. For blank cells we return a false. * * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not * CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA */ public boolean getBooleanCellValue() { int cellType = getCellType(); switch (cellType) { case CELL_TYPE_BLANK: return false; case CELL_TYPE_FORMULA: { FormulaValue fv = (FormulaValue) _value; if (fv.getFormulaType() != CELL_TYPE_BOOLEAN) throw typeMismatch(CELL_TYPE_BOOLEAN, CELL_TYPE_FORMULA, false); return ((BooleanFormulaValue) _value).getPreEvaluatedValue(); } case CELL_TYPE_BOOLEAN: { return ((BooleanValue) _value).getValue(); } default: throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); } }
/** * Get the value of the cell as a string * * <p>For numeric cells we throw an exception. For blank cells we return an empty string. For * formulaCells that are not string Formulas, we throw an exception. * * @return the value of the cell as a string */ public String getStringCellValue() { int cellType = getCellType(); switch (cellType) { case CELL_TYPE_BLANK: return ""; case CELL_TYPE_FORMULA: { FormulaValue fv = (FormulaValue) _value; if (fv.getFormulaType() != CELL_TYPE_STRING) throw typeMismatch(CELL_TYPE_STRING, CELL_TYPE_FORMULA, false); return ((StringFormulaValue) _value).getPreEvaluatedValue(); } case CELL_TYPE_STRING: { if (((StringValue) _value).isRichText()) return ((RichTextValue) _value).getValue().getString(); else return ((PlainStringValue) _value).getValue(); } default: throw typeMismatch(CELL_TYPE_STRING, cellType, false); } }