mirror of
https://github.com/apache/poi.git
synced 2025-03-06 08:59:07 +00:00
move HSSFFormulaEvaluator#evaluateInCell and BaseXSSFFormulaEvaluator#evaluateInCell(Cell) up to BaseFormulaEvaluator to reduce duplicated code
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760651 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a394090969
commit
de6c83cb47
@ -30,7 +30,6 @@ import org.apache.poi.ss.formula.eval.StringValueEval;
|
|||||||
import org.apache.poi.ss.formula.eval.ValueEval;
|
import org.apache.poi.ss.formula.eval.ValueEval;
|
||||||
import org.apache.poi.ss.formula.udf.UDFFinder;
|
import org.apache.poi.ss.formula.udf.UDFFinder;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
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.CellValue;
|
||||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
import org.apache.poi.ss.usermodel.RichTextString;
|
import org.apache.poi.ss.usermodel.RichTextString;
|
||||||
@ -141,34 +140,10 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
|||||||
public void notifySetFormula(Cell cell) {
|
public void notifySetFormula(Cell cell) {
|
||||||
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)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
|
@Override
|
||||||
public HSSFCell evaluateInCell(Cell cell) {
|
public HSSFCell evaluateInCell(Cell cell) {
|
||||||
if (cell == null) {
|
return (HSSFCell) super.evaluateInCell(cell);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,11 +19,9 @@ package org.apache.poi.ss.formula;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
|
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.ss.usermodel.CellType;
|
||||||
import org.apache.poi.ss.usermodel.CellValue;
|
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.FormulaEvaluator;
|
||||||
import org.apache.poi.ss.usermodel.RichTextString;
|
import org.apache.poi.ss.usermodel.RichTextString;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
@ -111,6 +109,37 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
|
|||||||
throw new IllegalStateException("Bad cell type (" + cell.getCellTypeEnum() + ")");
|
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);
|
protected abstract CellValue evaluateFormulaCellValue(Cell cell);
|
||||||
|
|
||||||
|
@ -72,24 +72,9 @@ public final class SXSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
|
|||||||
return new SXSSFEvaluationCell((SXSSFCell)cell);
|
return new SXSSFEvaluationCell((SXSSFCell)cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 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)} }
|
|
||||||
*/
|
|
||||||
public SXSSFCell evaluateInCell(Cell cell) {
|
public SXSSFCell evaluateInCell(Cell cell) {
|
||||||
doEvaluateInCell(cell);
|
return (SXSSFCell) super.evaluateInCell(cell);
|
||||||
return (SXSSFCell)cell;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,22 +52,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
|||||||
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)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
|
* Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
|
||||||
*/
|
*/
|
||||||
|
@ -23,6 +23,8 @@ import org.apache.poi.ss.formula.IStabilityClassifier;
|
|||||||
import org.apache.poi.ss.formula.WorkbookEvaluator;
|
import org.apache.poi.ss.formula.WorkbookEvaluator;
|
||||||
import org.apache.poi.ss.formula.udf.UDFFinder;
|
import org.apache.poi.ss.formula.udf.UDFFinder;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
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/>
|
* Evaluates formula cells.<p/>
|
||||||
@ -55,27 +57,6 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
|
|||||||
return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
|
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
|
* Loops over all cells in all sheets of the supplied
|
||||||
* workbook.
|
* workbook.
|
||||||
@ -90,6 +71,12 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
|
|||||||
public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
|
public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
|
||||||
BaseFormulaEvaluator.evaluateAllFormulaCells(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
|
* Loops over all cells in all sheets of the supplied
|
||||||
* workbook.
|
* workbook.
|
||||||
|
@ -682,4 +682,15 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
|
|||||||
value = evaluator.evaluate(cell);
|
value = evaluator.evaluate(cell);
|
||||||
assertEquals(1, value.getNumberValue(), 0.001);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -326,4 +327,15 @@ public abstract class BaseTestFormulaEvaluator {
|
|||||||
|
|
||||||
wb.close();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user