Selaa lähdekoodia

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
tags/REL_3_15_FINAL^2
Javen O'Neal 7 vuotta sitten
vanhempi
commit
de6c83cb47

+ 2
- 27
src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java Näytä tiedosto

@@ -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.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;
@@ -141,34 +140,10 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
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);
}

/**

+ 31
- 2
src/java/org/apache/poi/ss/formula/BaseFormulaEvaluator.java Näytä tiedosto

@@ -19,11 +19,9 @@ package org.apache.poi.ss.formula;

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;
@@ -111,6 +109,37 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
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);


+ 2
- 17
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFFormulaEvaluator.java Näytä tiedosto

@@ -72,24 +72,9 @@ public final class SXSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
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);
}
/**

+ 0
- 16
src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java Näytä tiedosto

@@ -52,22 +52,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
_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
*/

+ 8
- 21
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java Näytä tiedosto

@@ -23,6 +23,8 @@ import org.apache.poi.ss.formula.IStabilityClassifier;
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/>
@@ -55,27 +57,6 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
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.
@@ -90,6 +71,12 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
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.

+ 11
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java Näytä tiedosto

@@ -682,4 +682,15 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
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();
}
}

+ 12
- 0
src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java Näytä tiedosto

@@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel;

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;
@@ -326,4 +327,15 @@ public abstract class BaseTestFormulaEvaluator {
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…
Peruuta
Tallenna