move BaseXSSFFormulaEvaluator#evaluateFormulaCellEnum(Cell) and HSSFFormulaEvaluator#evaluateFormulaCellEnum(Cell) up to BaseFormulaEvaluator class to reduce duplicated code
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f71ae8036d
commit
8842d7bffd
@ -33,9 +33,8 @@ 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;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.util.Internal;
|
||||
import org.apache.poi.util.Removal;
|
||||
|
||||
/**
|
||||
* Evaluates formula cells.<p/>
|
||||
@ -83,6 +82,11 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
return new HSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RichTextString createRichTextString(String str) {
|
||||
return new HSSFRichTextString(str);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Coordinates several formula evaluators together so that formulas that involve external
|
||||
@ -138,34 +142,6 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, rather than throwing an
|
||||
* exception, this method returns {@link CellType#_NONE} 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>
|
||||
* CellType 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 {@link CellType#_NONE} for non-formula cells, or the type of the <em>formula result</em>
|
||||
* @since POI 3.15 beta 3
|
||||
* @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
|
||||
*/
|
||||
@Override
|
||||
public CellType evaluateFormulaCellEnum(Cell cell) {
|
||||
if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
|
||||
return CellType._NONE;
|
||||
}
|
||||
CellValue cv = evaluateFormulaCellValue(cell);
|
||||
// cell remains a formula cell, but the cached value is changed
|
||||
setCellValue(cell, cv);
|
||||
return cv.getCellTypeEnum();
|
||||
}
|
||||
|
||||
/**
|
||||
* If cell contains formula, it evaluates the formula, and
|
||||
* puts the formula result back into the cell, in place
|
||||
@ -195,30 +171,6 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void setCellValue(Cell cell, CellValue cv) {
|
||||
CellType cellType = cv.getCellTypeEnum();
|
||||
switch (cellType) {
|
||||
case BOOLEAN:
|
||||
cell.setCellValue(cv.getBooleanValue());
|
||||
break;
|
||||
case ERROR:
|
||||
cell.setCellErrorValue(cv.getErrorValue());
|
||||
break;
|
||||
case NUMERIC:
|
||||
cell.setCellValue(cv.getNumberValue());
|
||||
break;
|
||||
case STRING:
|
||||
cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
|
||||
break;
|
||||
case BLANK:
|
||||
// never happens - blanks eventually get translated to zero
|
||||
case FORMULA:
|
||||
// this will never happen, we have already evaluated the formula
|
||||
default:
|
||||
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops over all cells in all sheets of the supplied
|
||||
* workbook.
|
||||
|
@ -19,10 +19,13 @@ 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;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
@ -132,6 +135,38 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
|
||||
return evaluateFormulaCellEnum(cell).getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* If cell contains formula, it evaluates the formula,
|
||||
* and saves the result of the formula. The cell
|
||||
* remains as a formula cell.
|
||||
* Else if cell does not contain formula, this method leaves
|
||||
* the cell unchanged.
|
||||
* Note that the type of the formula result is returned,
|
||||
* so you know what kind of value is also stored with
|
||||
* the formula.
|
||||
* <pre>
|
||||
* CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(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 #evaluate(org.apache.poi.ss.usermodel.Cell)} }
|
||||
* @param cell The cell to evaluate
|
||||
* @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
|
||||
* If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
|
||||
* @since POI 3.15 beta 3
|
||||
* @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
|
||||
*/
|
||||
@Override
|
||||
public CellType evaluateFormulaCellEnum(Cell cell) {
|
||||
if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
|
||||
return CellType._NONE;
|
||||
}
|
||||
CellValue cv = evaluateFormulaCellValue(cell);
|
||||
// cell remains a formula cell, but the cached value is changed
|
||||
setCellValue(cell, cv);
|
||||
return cv.getCellTypeEnum();
|
||||
}
|
||||
|
||||
protected static void setCellType(Cell cell, CellValue cv) {
|
||||
CellType cellType = cv.getCellTypeEnum();
|
||||
switch (cellType) {
|
||||
@ -152,6 +187,33 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract RichTextString createRichTextString(String str);
|
||||
|
||||
protected void setCellValue(Cell cell, CellValue cv) {
|
||||
CellType cellType = cv.getCellTypeEnum();
|
||||
switch (cellType) {
|
||||
case BOOLEAN:
|
||||
cell.setCellValue(cv.getBooleanValue());
|
||||
break;
|
||||
case ERROR:
|
||||
cell.setCellErrorValue(cv.getErrorValue());
|
||||
break;
|
||||
case NUMERIC:
|
||||
cell.setCellValue(cv.getNumberValue());
|
||||
break;
|
||||
case STRING:
|
||||
cell.setCellValue(createRichTextString(cv.getStringValue()));
|
||||
break;
|
||||
case BLANK:
|
||||
// never happens - blanks eventually get translated to zero
|
||||
case FORMULA:
|
||||
// this will never happen, we have already evaluated the formula
|
||||
default:
|
||||
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loops over all cells in all sheets of the supplied
|
||||
* workbook.
|
||||
|
@ -101,6 +101,7 @@ public interface FormulaEvaluator {
|
||||
* or one of {@link CellType#NUMERIC}, {@link CellType#STRING},
|
||||
* {@link CellType#BOOLEAN}, {@link CellType#ERROR}
|
||||
* Note: the cell's type remains as CellType.FORMULA however.
|
||||
* @deprecated 3.15. Will return a {@link CellType} enum in the future
|
||||
*/
|
||||
int evaluateFormulaCell(Cell cell);
|
||||
|
||||
|
@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
|
||||
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.RichTextString;
|
||||
|
||||
/**
|
||||
* Internal POI use only - parent of XSSF and SXSSF formula evaluators
|
||||
@ -36,6 +37,10 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
protected BaseXSSFFormulaEvaluator(WorkbookEvaluator bookEvaluator) {
|
||||
super(bookEvaluator);
|
||||
}
|
||||
@Override
|
||||
protected RichTextString createRichTextString(String str) {
|
||||
return new XSSFRichTextString(str);
|
||||
}
|
||||
|
||||
public void notifySetFormula(Cell cell) {
|
||||
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
|
||||
@ -47,37 +52,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
|
||||
}
|
||||
|
||||
/**
|
||||
* If cell contains formula, it evaluates the formula,
|
||||
* and saves the result of the formula. The cell
|
||||
* remains as a formula cell.
|
||||
* Else if cell does not contain formula, this method leaves
|
||||
* the cell unchanged.
|
||||
* Note that the type of the formula result is returned,
|
||||
* so you know what kind of value is also stored with
|
||||
* the formula.
|
||||
* <pre>
|
||||
* CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(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 #evaluate(org.apache.poi.ss.usermodel.Cell)} }
|
||||
* @param cell The cell to evaluate
|
||||
* @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
|
||||
* If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
|
||||
* @since POI 3.15 beta 3
|
||||
* @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
|
||||
*/
|
||||
public CellType evaluateFormulaCellEnum(Cell cell) {
|
||||
if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
|
||||
return CellType._NONE;
|
||||
}
|
||||
CellValue cv = evaluateFormulaCellValue(cell);
|
||||
// cell remains a formula cell, but the cached value is changed
|
||||
setCellValue(cell, cv);
|
||||
return cv.getCellTypeEnum();
|
||||
}
|
||||
|
||||
/**
|
||||
* If cell contains formula, it evaluates the formula, and
|
||||
* puts the formula result back into the cell, in place
|
||||
@ -94,30 +68,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
}
|
||||
}
|
||||
|
||||
private static void setCellValue(Cell cell, CellValue cv) {
|
||||
CellType cellType = cv.getCellTypeEnum();
|
||||
switch (cellType) {
|
||||
case BOOLEAN:
|
||||
cell.setCellValue(cv.getBooleanValue());
|
||||
break;
|
||||
case ERROR:
|
||||
cell.setCellErrorValue(cv.getErrorValue());
|
||||
break;
|
||||
case NUMERIC:
|
||||
cell.setCellValue(cv.getNumberValue());
|
||||
break;
|
||||
case STRING:
|
||||
cell.setCellValue(new XSSFRichTextString(cv.getStringValue()));
|
||||
break;
|
||||
case BLANK:
|
||||
// never happens - blanks eventually get translated to zero
|
||||
case FORMULA:
|
||||
// this will never happen, we have already evaluated the formula
|
||||
default:
|
||||
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user