import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.RichTextString;
public void notifySetFormula(Cell cell) {
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
}
-
- /**
- * 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 #evaluateFormulaCellEnum(Cell)}}
- */
+
@Override
public HSSFCell evaluateInCell(Cell cell) {
- if (cell == null) {
- return null;
- }
- HSSFCell result = (HSSFCell) cell;
- if (cell.getCellTypeEnum() == CellType.FORMULA) {
- CellValue cv = evaluateFormulaCellValue(cell);
- setCellValue(cell, cv);
- setCellType(cell, cv); // cell will no longer be a formula cell
- }
- return result;
+ return (HSSFCell) super.evaluateInCell(cell);
}
/**
import java.util.Map;
-import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
-import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
throw new IllegalStateException("Bad cell type (" + cell.getCellTypeEnum() + ")");
}
}
+
+ /**
+ * 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 #evaluateFormulaCellEnum(Cell)}}
+ * @param cell
+ * @return the {@code cell} that was passed in, allowing for chained calls
+ */
+ @Override
+ public Cell evaluateInCell(Cell cell) {
+ if (cell == null) {
+ return null;
+ }
+ Cell result = cell;
+ if (cell.getCellTypeEnum() == CellType.FORMULA) {
+ CellValue cv = evaluateFormulaCellValue(cell);
+ setCellValue(cell, cv);
+ setCellType(cell, cv); // cell will no longer be a formula cell
+ }
+ return result;
+ }
protected abstract CellValue evaluateFormulaCellValue(Cell cell);
return new SXSSFEvaluationCell((SXSSFCell)cell);
}
- /**
- * 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 SXSSFCell 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 #evaluateFormulaCellEnum(org.apache.poi.ss.usermodel.Cell)} }
- */
+ @Override
public SXSSFCell evaluateInCell(Cell cell) {
- doEvaluateInCell(cell);
- return (SXSSFCell)cell;
+ return (SXSSFCell) super.evaluateInCell(cell);
}
/**
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
}
- /**
- * 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.
- */
- protected void doEvaluateInCell(Cell cell) {
- if (cell == null) return;
- if (cell.getCellTypeEnum() == CellType.FORMULA) {
- CellValue cv = evaluateFormulaCellValue(cell);
- setCellType(cell, cv); // cell will no longer be a formula cell
- setCellValue(cell, cv);
- }
- }
-
/**
* Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
*/
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.CellValue;
/**
* Evaluates formula cells.<p/>
return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
}
- /**
- * 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 XSSFCell 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 #evaluateFormulaCellEnum(org.apache.poi.ss.usermodel.Cell)} }
- * @param cell
- */
- public XSSFCell evaluateInCell(Cell cell) {
- doEvaluateInCell(cell);
- return (XSSFCell)cell;
- }
-
/**
* Loops over all cells in all sheets of the supplied
* workbook.
public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
BaseFormulaEvaluator.evaluateAllFormulaCells(wb);
}
+
+ @Override
+ public XSSFCell evaluateInCell(Cell cell) {
+ return (XSSFCell) super.evaluateInCell(cell);
+ }
+
/**
* Loops over all cells in all sheets of the supplied
* workbook.
value = evaluator.evaluate(cell);
assertEquals(1, value.getNumberValue(), 0.001);
}
+
+ @Test
+ public void evaluateInCellReturnsSameDataType() throws IOException {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ wb.createSheet().createRow(0).createCell(0);
+ XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+ XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
+ XSSFCell same = evaluator.evaluateInCell(cell);
+ assertSame(cell, same);
+ wb.close();
+ }
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.io.IOException;
wb.close();
}
+
+ @Test
+ public void evaluateInCellReturnsSameCell() throws IOException {
+ Workbook wb = _testDataProvider.createWorkbook();
+ wb.createSheet().createRow(0).createCell(0);
+ FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+ Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
+ Cell same = evaluator.evaluateInCell(cell);
+ assertSame(cell, same);
+ wb.close();
+ }
}
\ No newline at end of file