/** * If cell contains formula, it evaluates the formula, and * puts the formula result back into the cell, in place * of the old formula. * Else if cell does not contain formula, this method leaves * the cell unchanged. * Note that the same instance of HSSFCell is returned to * allow chained calls like: * <pre> * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType(); * </pre> * Be aware that your cell value will be changed to hold the * result of the formula. If you simply want the formula * value computed for you, use {@link #evaluateFormulaCell(Cell)}} */ public HSSFCell evaluateInCell(Cell cell) { if (cell == null) { return null; } HSSFCell result = (HSSFCell) cell; if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { CellValue cv = evaluateFormulaCellValue(cell); setCellValue(cell, cv); setCellType(cell, cv); // cell will no longer be a formula cell } return result; } private static void setCellType(Cell cell, CellValue cv) {
/** * If cell contains formula, it evaluates the formula, and saves the result of the formula. The * cell remains as a formula cell. If the cell does not contain formula, this method returns -1 * and leaves the cell unchanged. * * Note that the type of the <em>formula result</em> is returned, so you know what kind of * cached formula result is also stored with the formula. * <pre> * int evaluatedCellType = evaluator.evaluateFormulaCell(cell); * </pre> * Be aware that your cell will hold both the formula, and the result. If you want the cell * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)} * @param cell The cell to evaluate * @return -1 for non-formula cells, or the type of the <em>formula result</em> */ public int evaluateFormulaCell(Cell cell) { if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) { return -1; } CellValue cv = evaluateFormulaCellValue(cell); // cell remains a formula cell, but the cached value is changed setCellValue(cell, cv); return cv.getCellType(); }
/** * If cell contains formula, it evaluates the formula, and saves the result of the formula. The * cell remains as a formula cell. If the cell does not contain formula, this method returns -1 * and leaves the cell unchanged. * * Note that the type of the <em>formula result</em> is returned, so you know what kind of * cached formula result is also stored with the formula. * <pre> * int evaluatedCellType = evaluator.evaluateFormulaCell(cell); * </pre> * Be aware that your cell will hold both the formula, and the result. If you want the cell * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)} * @param cell The cell to evaluate * @return -1 for non-formula cells, or the type of the <em>formula result</em> */ public int evaluateFormulaCell(Cell cell) { if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) { return -1; } CellValue cv = evaluateFormulaCellValue(cell); // cell remains a formula cell, but the cached value is changed setCellValue(cell, cv); return cv.getCellType(); }
/** * If cell contains formula, it evaluates the formula, and * puts the formula result back into the cell, in place * of the old formula. * Else if cell does not contain formula, this method leaves * the cell unchanged. * Note that the same instance of HSSFCell is returned to * allow chained calls like: * <pre> * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType(); * </pre> * Be aware that your cell value will be changed to hold the * result of the formula. If you simply want the formula * value computed for you, use {@link #evaluateFormulaCell(Cell)}} */ public HSSFCell evaluateInCell(Cell cell) { if (cell == null) { return null; } HSSFCell result = (HSSFCell) cell; if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { CellValue cv = evaluateFormulaCellValue(cell); setCellValue(cell, cv); setCellType(cell, cv); // cell will no longer be a formula cell } return result; } private static void setCellType(Cell cell, CellValue cv) {