diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java index 27f168e31..6470fe923 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java @@ -187,15 +187,15 @@ public final class HSSFReadWrite { switch (cell.getCellType()) { - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: value = "FORMULA value=" + cell.getCellFormula(); break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; - case HSSFCell.CELL_TYPE_STRING: + case STRING: value = "STRING value=" + cell.getStringCellValue(); break; diff --git a/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java b/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java index 9714b7e2b..c4a2d1dd0 100644 --- a/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java +++ b/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java @@ -152,23 +152,23 @@ public class SVTableCellEditor extends AbstractCellEditor implements TableCellEd //Set the value that is rendered for the cell switch (cell.getCellType()) { - case HSSFCell.CELL_TYPE_BLANK: + case BLANK: editor.setText(""); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: if (cell.getBooleanCellValue()) { editor.setText("true"); } else { editor.setText("false"); } break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: editor.setText(Double.toString(cell.getNumericCellValue())); break; - case HSSFCell.CELL_TYPE_STRING: + case STRING: editor.setText(cell.getRichStringCellValue().getString()); break; - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: default: editor.setText("?"); } diff --git a/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java b/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java index db7f6304f..e77a18196 100644 --- a/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java +++ b/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java @@ -166,17 +166,17 @@ public class SVTableCellRenderer extends JLabel //Set the value that is rendered for the cell switch (c.getCellType()) { - case HSSFCell.CELL_TYPE_BLANK: + case BLANK: setValue(""); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: if (c.getBooleanCellValue()) { setValue("true"); } else { setValue("false"); } break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: short format = s.getDataFormat(); double numericValue = c.getNumericCellValue(); if (cellFormatter.useRedColor(format, numericValue)) @@ -184,10 +184,10 @@ public class SVTableCellRenderer extends JLabel else setForeground(null); setValue(cellFormatter.format(format, c.getNumericCellValue())); break; - case HSSFCell.CELL_TYPE_STRING: + case STRING: setValue(c.getRichStringCellValue().getString()); break; - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: default: setValue("?"); } diff --git a/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java b/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java index 74e09ae94..0a35eaa11 100644 --- a/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java +++ b/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java @@ -25,6 +25,7 @@ import java.util.Locale; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; @@ -178,25 +179,28 @@ public class ExcelComparator { private void compareDataInCell(Locator loc1, Locator loc2) { if (isCellTypeMatches(loc1, loc2)) { - switch(loc1.cell.getCellType()) { - case Cell.CELL_TYPE_BLANK: - case Cell.CELL_TYPE_STRING: - case Cell.CELL_TYPE_ERROR: - isCellContentMatches(loc1,loc2); - break; - case Cell.CELL_TYPE_BOOLEAN: - isCellContentMatchesForBoolean(loc1,loc2); - break; - case Cell.CELL_TYPE_FORMULA: - isCellContentMatchesForFormula(loc1,loc2); - break; - case Cell.CELL_TYPE_NUMERIC: - if (DateUtil.isCellDateFormatted(loc1.cell)) { - isCellContentMatchesForDate(loc1,loc2); - } else { - isCellContentMatchesForNumeric(loc1,loc2); - } - break; + final CellType loc1cellType = loc1.cell.getCellType(); + switch(loc1cellType) { + case BLANK: + case STRING: + case ERROR: + isCellContentMatches(loc1,loc2); + break; + case BOOLEAN: + isCellContentMatchesForBoolean(loc1,loc2); + break; + case FORMULA: + isCellContentMatchesForFormula(loc1,loc2); + break; + case NUMERIC: + if (DateUtil.isCellDateFormatted(loc1.cell)) { + isCellContentMatchesForDate(loc1,loc2); + } else { + isCellContentMatchesForNumeric(loc1,loc2); + } + break; + default: + throw new IllegalStateException("Unexpected cell type: " + loc1cellType); } } @@ -577,13 +581,12 @@ public class ExcelComparator { * Checks if cell type matches. */ private boolean isCellTypeMatches(Locator loc1, Locator loc2) { - int type1 = loc1.cell.getCellType(); - int type2 = loc2.cell.getCellType(); + CellType type1 = loc1.cell.getCellType(); + CellType type2 = loc2.cell.getCellType(); if (type1 == type2) return true; addMessage(loc1, loc2, "Cell Data-Type does not Match in :: ", - Integer.toString(type1), - Integer.toString(type2) + type1.name(), type2.name() ); return false; } diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java index d59d4666d..3f2ef7ea0 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java @@ -16,7 +16,6 @@ ==================================================================== */ package org.apache.poi.ss.examples.html; -import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.format.CellFormat; @@ -336,9 +335,9 @@ public class ToHtml { } } - private static int ultimateCellType(Cell c) { - int type = c.getCellType(); - if (type == Cell.CELL_TYPE_FORMULA) + private static CellType ultimateCellType(Cell c) { + CellType type = c.getCellType(); + if (type == CellType.FORMULA) type = c.getCachedFormulaResultType(); return type; } @@ -443,12 +442,12 @@ public class ToHtml { private String tagStyle(Cell cell, CellStyle style) { if (style.getAlignment() == ALIGN_GENERAL) { switch (ultimateCellType(cell)) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: return "style=\"text-align: left;\""; - case HSSFCell.CELL_TYPE_BOOLEAN: - case HSSFCell.CELL_TYPE_ERROR: + case BOOLEAN: + case ERROR: return "style=\"text-align: center;\""; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: default: // "right" is the default break; diff --git a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index e5f37aefc..267fbfd43 100644 --- a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -323,42 +323,40 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p outputContents = _includeBlankCells; } else { switch(cell.getCellType()) { - case Cell.CELL_TYPE_STRING: + case STRING: text.append(cell.getRichStringCellValue().getString()); break; - case Cell.CELL_TYPE_NUMERIC: - text.append( - _formatter.formatCellValue(cell) - ); + case NUMERIC: + text.append(_formatter.formatCellValue(cell)); break; - case Cell.CELL_TYPE_BOOLEAN: + case BOOLEAN: text.append(cell.getBooleanCellValue()); break; - case Cell.CELL_TYPE_ERROR: + case ERROR: text.append(ErrorEval.getText(cell.getErrorCellValue())); break; - case Cell.CELL_TYPE_FORMULA: + case FORMULA: if(!_shouldEvaluateFormulas) { text.append(cell.getCellFormula()); } else { switch(cell.getCachedFormulaResultType()) { - case Cell.CELL_TYPE_STRING: + case STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if(str != null && str.length() > 0) { text.append(str.toString()); } break; - case Cell.CELL_TYPE_NUMERIC: + case NUMERIC: HSSFCellStyle style = cell.getCellStyle(); double nVal = cell.getNumericCellValue(); short df = style.getDataFormat(); String dfs = style.getDataFormatString(); text.append(_formatter.formatRawCellContents(nVal, df, dfs)); break; - case Cell.CELL_TYPE_BOOLEAN: + case BOOLEAN: text.append(cell.getBooleanCellValue()); break; - case Cell.CELL_TYPE_ERROR: + case ERROR: text.append(ErrorEval.getText(cell.getErrorCellValue())); break; default: diff --git a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java index e7b2019b1..605e328a0 100644 --- a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java @@ -43,7 +43,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NotOLE2FileException; -import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.util.IOUtils; /** @@ -266,12 +266,12 @@ public class OldExcelExtractor implements Closeable { // Biff 2 and 5+ share the same SID, due to a bug... if (biffVersion == 5) { FormulaRecord fr = new FormulaRecord(ris); - if (fr.getCachedResultType() == Cell.CELL_TYPE_NUMERIC) { + if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) { handleNumericCell(text, fr.getValue()); } } else { OldFormulaRecord fr = new OldFormulaRecord(ris); - if (fr.getCachedResultType() == Cell.CELL_TYPE_NUMERIC) { + if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) { handleNumericCell(text, fr.getValue()); } } diff --git a/src/java/org/apache/poi/hssf/record/FormulaRecord.java b/src/java/org/apache/poi/hssf/record/FormulaRecord.java index 91605873e..4eae6ce5c 100644 --- a/src/java/org/apache/poi/hssf/record/FormulaRecord.java +++ b/src/java/org/apache/poi/hssf/record/FormulaRecord.java @@ -17,10 +17,10 @@ package org.apache.poi.hssf.record; -import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.formula.Formula; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.ptg.Ptg; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.HexDump; @@ -53,6 +53,8 @@ public final class FormulaRecord extends CellRecord implements Cloneable { private static final int VARIABLE_DATA_LENGTH = 6; private static final int DATA_INDEX = 2; + // FIXME: can these be merged with {@link CellType}? + // are the numbers specific to the HSSF formula record format or just a poor-man's enum? public static final int STRING = 0; public static final int BOOLEAN = 1; public static final int ERROR_CODE = 2; @@ -146,10 +148,10 @@ public final class FormulaRecord extends CellRecord implements Cloneable { public int getValueType() { int typeCode = getTypeCode(); switch (typeCode) { - case STRING: return HSSFCell.CELL_TYPE_STRING; - case BOOLEAN: return HSSFCell.CELL_TYPE_BOOLEAN; - case ERROR_CODE: return HSSFCell.CELL_TYPE_ERROR; - case EMPTY: return HSSFCell.CELL_TYPE_STRING; // is this correct? + case STRING: return CellType.STRING.getCode(); + case BOOLEAN: return CellType.BOOLEAN.getCode(); + case ERROR_CODE: return CellType.ERROR.getCode(); + case EMPTY: return CellType.STRING.getCode(); // is this correct? } throw new IllegalStateException("Unexpected type id (" + typeCode + ")"); } @@ -241,7 +243,7 @@ public final class FormulaRecord extends CellRecord implements Cloneable { public int getCachedResultType() { if (specialCachedValue == null) { - return HSSFCell.CELL_TYPE_NUMERIC; + return CellType.NUMERIC.getCode(); } return specialCachedValue.getValueType(); } diff --git a/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java b/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java index f3aaf872e..62769e0e4 100644 --- a/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java +++ b/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java @@ -21,6 +21,7 @@ import org.apache.poi.hssf.record.FormulaRecord.SpecialCachedValue; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.formula.Formula; import org.apache.poi.ss.formula.ptg.Ptg; +import org.apache.poi.ss.usermodel.CellType; /** * Formula Record (0x0006 / 0x0206 / 0x0406) - holds a formula in @@ -63,7 +64,7 @@ public final class OldFormulaRecord extends OldCellRecord { public int getCachedResultType() { if (specialCachedValue == null) { - return HSSFCell.CELL_TYPE_NUMERIC; + return CellType.NUMERIC.getCode(); } return specialCachedValue.getValueType(); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 08825ce35..003a93ae1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -44,6 +44,7 @@ import org.apache.poi.ss.formula.ptg.ExpPtg; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.Hyperlink; @@ -80,7 +81,7 @@ public class HSSFCell implements Cell { private final HSSFWorkbook _book; private final HSSFSheet _sheet; - private int _cellType; + private CellType _cellType; private HSSFRichTextString _stringValue; private CellValueRecordInterface _record; private HSSFComment _comment; @@ -89,7 +90,7 @@ public class HSSFCell implements Cell { * Creates new Cell - Should only be called by HSSFRow. This creates a cell * from scratch. *
- * When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types
+ * When the cell is initially created it is set to {@link CellType#BLANK}. Cell types
* can be changed/overwritten by calling setCellValue with the appropriate
* type as a parameter although conversions from one type to another may be
* prohibited.
@@ -109,10 +110,10 @@ public class HSSFCell implements Cell {
_sheet = sheet;
// Relying on the fact that by default the cellType is set to 0 which
- // is different to CELL_TYPE_BLANK hence the following method call correctly
+ // is different to {@link CellType#BLANK} hence the following method call correctly
// creates a new blank cell.
short xfindex = sheet.getSheet().getXFIndexForColAt(col);
- setCellType(CELL_TYPE_BLANK, false, row, col,xfindex);
+ setCellType(CellType.BLANK, false, row, col,xfindex);
}
/**
@@ -142,16 +143,14 @@ public class HSSFCell implements Cell {
* @param sheet - Sheet record of the sheet containing this cell
* @param row - the row of this cell
* @param col - the column for this cell
- * @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
- * CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
- * Type of cell
+ * @param type - Type of cell
* @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(int,int)
*/
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
- int type)
+ CellType type)
{
checkBounds(col);
- _cellType = -1; // Force 'setCellType' to create a first Record
+ _cellType = CellType._UNINITIALIZED; // Force 'setCellType' to create a first Record
_stringValue = null;
_book = book;
_sheet = sheet;
@@ -176,14 +175,14 @@ public class HSSFCell implements Cell {
_sheet = sheet;
switch (_cellType)
{
- case CELL_TYPE_STRING :
+ case STRING :
_stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval);
break;
- case CELL_TYPE_BLANK :
+ case BLANK :
break;
- case CELL_TYPE_FORMULA :
+ case FORMULA :
_stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
break;
@@ -196,23 +195,23 @@ public class HSSFCell implements Cell {
/**
* used internally -- given a cell value record, figure out its type
*/
- private static int determineType(CellValueRecordInterface cval) {
+ private static CellType determineType(CellValueRecordInterface cval) {
if (cval instanceof FormulaRecordAggregate) {
- return HSSFCell.CELL_TYPE_FORMULA;
+ return CellType.FORMULA;
}
// all others are plain BIFF records
Record record = ( Record ) cval;
switch (record.getSid()) {
- case NumberRecord.sid : return HSSFCell.CELL_TYPE_NUMERIC;
- case BlankRecord.sid : return HSSFCell.CELL_TYPE_BLANK;
- case LabelSSTRecord.sid : return HSSFCell.CELL_TYPE_STRING;
+ case NumberRecord.sid : return CellType.NUMERIC;
+ case BlankRecord.sid : return CellType.BLANK;
+ case LabelSSTRecord.sid : return CellType.STRING;
case BoolErrRecord.sid :
BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
return boolErrRecord.isBoolean()
- ? HSSFCell.CELL_TYPE_BOOLEAN
- : HSSFCell.CELL_TYPE_ERROR;
+ ? CellType.BOOLEAN
+ : CellType.ERROR;
}
throw new RuntimeException("Bad cell value rec (" + cval.getClass().getName() + ")");
}
@@ -255,19 +254,29 @@ public class HSSFCell implements Cell {
return new CellAddress(this);
}
-
/**
* Set the cells type (numeric, formula or string).
* If the cell currently contains a value, the value will
* be converted to match the new type, if possible.
- * @see #CELL_TYPE_NUMERIC
- * @see #CELL_TYPE_STRING
- * @see #CELL_TYPE_FORMULA
- * @see #CELL_TYPE_BLANK
- * @see #CELL_TYPE_BOOLEAN
- * @see #CELL_TYPE_ERROR
+ * @see CellType#NUMERIC
+ * @see CellType#STRING
+ * @see CellType#FORMULA
+ * @see CellType#BLANK
+ * @see CellType#BOOLEAN
+ * @see CellType#ERROR
+ * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/
+ @Override
public void setCellType(int cellType) {
+ setCellType(CellType.forInt(cellType));
+ }
+ /**
+ * Set the cells type (numeric, formula or string).
+ * If the cell currently contains a value, the value will
+ * be converted to match the new type, if possible.
+ */
+ @Override
+ public void setCellType(CellType cellType) {
notifyFormulaChanging();
if(isPartOfArrayFormulaGroup()){
notifyArrayFormulaChanging();
@@ -287,17 +296,12 @@ public class HSSFCell implements Cell {
*
*/
- private void setCellType(int cellType, boolean setValue, int row,short col, short styleIndex)
+ private void setCellType(CellType cellType, boolean setValue, int row,short col, short styleIndex)
{
-
- if (cellType > CELL_TYPE_ERROR)
- {
- throw new RuntimeException("I have no idea what type that is!");
- }
switch (cellType)
{
- case CELL_TYPE_FORMULA :
+ case FORMULA :
FormulaRecordAggregate frec;
if (cellType != _cellType) {
@@ -315,7 +319,7 @@ public class HSSFCell implements Cell {
_record = frec;
break;
- case CELL_TYPE_NUMERIC :
+ case NUMERIC :
NumberRecord nrec = null;
if (cellType != _cellType)
@@ -336,7 +340,7 @@ public class HSSFCell implements Cell {
_record = nrec;
break;
- case CELL_TYPE_STRING :
+ case STRING :
LabelSSTRecord lrec;
if (cellType == _cellType) {
@@ -352,7 +356,7 @@ public class HSSFCell implements Cell {
if(str == null) {
// bug 55668: don't try to store null-string when formula
// results in empty/null value
- setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
+ setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
} else {
int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
@@ -365,7 +369,7 @@ public class HSSFCell implements Cell {
_record = lrec;
break;
- case CELL_TYPE_BLANK :
+ case BLANK :
BlankRecord brec = null;
if (cellType != _cellType)
@@ -384,7 +388,7 @@ public class HSSFCell implements Cell {
_record = brec;
break;
- case CELL_TYPE_BOOLEAN :
+ case BOOLEAN :
BoolErrRecord boolRec = null;
if (cellType != _cellType)
@@ -405,7 +409,7 @@ public class HSSFCell implements Cell {
_record = boolRec;
break;
- case CELL_TYPE_ERROR :
+ case ERROR :
BoolErrRecord errRec = null;
if (cellType != _cellType)
@@ -429,7 +433,7 @@ public class HSSFCell implements Cell {
throw new IllegalStateException("Invalid cell type: " + cellType);
}
if (cellType != _cellType &&
- _cellType!=-1 ) // Special Value to indicate an uninitialized Cell
+ _cellType != CellType._UNINITIALIZED ) // Special Value to indicate an uninitialized Cell
{
_sheet.getSheet().replaceValueRecord(_record);
}
@@ -438,14 +442,9 @@ public class HSSFCell implements Cell {
/**
* get the cells type (numeric, formula or string)
- * @see #CELL_TYPE_STRING
- * @see #CELL_TYPE_NUMERIC
- * @see #CELL_TYPE_FORMULA
- * @see #CELL_TYPE_BOOLEAN
- * @see #CELL_TYPE_ERROR
*/
-
- public int getCellType()
+ @Override
+ public CellType getCellType()
{
return _cellType;
}
@@ -458,6 +457,7 @@ public class HSSFCell implements Cell {
* will change the cell to a numeric cell and set its value.
*/
@SuppressWarnings("fallthrough")
+ @Override
public void setCellValue(double value) {
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
@@ -474,12 +474,12 @@ public class HSSFCell implements Cell {
switch (_cellType) {
default:
- setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
+ setCellType(CellType.NUMERIC, false, row, col, styleIndex);
// fall through
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
(( NumberRecord ) _record).setValue(value);
break;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
((FormulaRecordAggregate)_record).setCachedDoubleResult(value);
break;
}
@@ -550,7 +550,7 @@ public class HSSFCell implements Cell {
if (value == null)
{
notifyFormulaChanging();
- setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
+ setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
}
@@ -558,7 +558,7 @@ public class HSSFCell implements Cell {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
- if (_cellType == CELL_TYPE_FORMULA) {
+ if (_cellType == CellType.FORMULA) {
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.
FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
@@ -573,8 +573,8 @@ public class HSSFCell implements Cell {
// If we get here, we're not dealing with a formula,
// so handle things as a normal rich text cell
- if (_cellType != CELL_TYPE_STRING) {
- setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
+ if (_cellType != CellType.STRING) {
+ setCellType(CellType.STRING, false, row, col, styleIndex);
}
int index = 0;
@@ -598,12 +598,12 @@ public class HSSFCell implements Cell {
if (formula==null) {
notifyFormulaChanging();
- setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
+ setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
}
int sheetIndex = _book.getSheetIndex(_sheet);
Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex);
- setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
+ setCellType(CellType.FORMULA, false, row, col, styleIndex);
FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
FormulaRecord frec = agg.getFormulaRecord();
frec.setOptions((short) 2);
@@ -627,34 +627,18 @@ public class HSSFCell implements Cell {
public String getCellFormula() {
if (!(_record instanceof FormulaRecordAggregate)) {
- throw typeMismatch(CELL_TYPE_FORMULA, _cellType, true);
+ throw typeMismatch(CellType.FORMULA, _cellType, true);
}
return HSSFFormulaParser.toFormulaString(_book, ((FormulaRecordAggregate)_record).getFormulaTokens());
}
- /**
- * Used to help format error messages
- */
- private static String getCellTypeName(int cellTypeCode) {
- switch (cellTypeCode) {
- case CELL_TYPE_BLANK: return "blank";
- case CELL_TYPE_STRING: return "text";
- case CELL_TYPE_BOOLEAN: return "boolean";
- case CELL_TYPE_ERROR: return "error";
- case CELL_TYPE_NUMERIC: return "numeric";
- case CELL_TYPE_FORMULA: return "formula";
- }
- return "#unknown cell type (" + cellTypeCode + ")#";
- }
-
- private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) {
- String msg = "Cannot get a "
- + getCellTypeName(expectedTypeCode) + " value from a "
- + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
+ private static RuntimeException typeMismatch(CellType expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) {
+ String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode
+ + " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg);
}
- private static void checkFormulaCachedValueType(int expectedTypeCode, FormulaRecord fr) {
- int cachedValueType = fr.getCachedResultType();
+ private static void checkFormulaCachedValueType(CellType expectedTypeCode, FormulaRecord fr) {
+ CellType cachedValueType = CellType.forInt(fr.getCachedResultType());
if (cachedValueType != expectedTypeCode) {
throw typeMismatch(expectedTypeCode, cachedValueType, true);
}
@@ -671,17 +655,17 @@ public class HSSFCell implements Cell {
public double getNumericCellValue() {
switch(_cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return 0.0;
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return ((NumberRecord)_record).getValue();
default:
- throw typeMismatch(CELL_TYPE_NUMERIC, _cellType, false);
- case CELL_TYPE_FORMULA:
+ throw typeMismatch(CellType.NUMERIC, _cellType, false);
+ case FORMULA:
break;
}
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
- checkFormulaCachedValueType(CELL_TYPE_NUMERIC, fr);
+ checkFormulaCachedValueType(CellType.NUMERIC, fr);
return fr.getValue();
}
@@ -694,7 +678,7 @@ public class HSSFCell implements Cell {
*/
public Date getDateCellValue() {
- if (_cellType == CELL_TYPE_BLANK) {
+ if (_cellType == CellType.BLANK) {
return null;
}
double value = getNumericCellValue();
@@ -723,17 +707,17 @@ public class HSSFCell implements Cell {
public HSSFRichTextString getRichStringCellValue() {
switch(_cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return new HSSFRichTextString("");
- case CELL_TYPE_STRING:
+ case STRING:
return _stringValue;
default:
- throw typeMismatch(CELL_TYPE_STRING, _cellType, false);
- case CELL_TYPE_FORMULA:
+ throw typeMismatch(CellType.STRING, _cellType, false);
+ case FORMULA:
break;
}
FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
- checkFormulaCachedValueType(CELL_TYPE_STRING, fra.getFormulaRecord());
+ checkFormulaCachedValueType(CellType.STRING, fra.getFormulaRecord());
String strVal = fra.getStringValue();
return new HSSFRichTextString(strVal == null ? "" : strVal);
}
@@ -753,12 +737,12 @@ public class HSSFCell implements Cell {
switch (_cellType) {
default:
- setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex);
+ setCellType(CellType.BOOLEAN, false, row, col, styleIndex);
// fall through
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
(( BoolErrRecord ) _record).setValue(value);
break;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
((FormulaRecordAggregate)_record).setCachedBooleanResult(value);
break;
}
@@ -793,12 +777,12 @@ public class HSSFCell implements Cell {
short styleIndex=_record.getXFIndex();
switch (_cellType) {
default:
- setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex);
+ setCellType(CellType.ERROR, false, row, col, styleIndex);
// fall through
- case CELL_TYPE_ERROR:
+ case ERROR:
(( BoolErrRecord ) _record).setValue(error);
break;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
((FormulaRecordAggregate)_record).setCachedErrorResult(error.getCode());
break;
}
@@ -816,24 +800,24 @@ public class HSSFCell implements Cell {
private boolean convertCellValueToBoolean() {
switch (_cellType) {
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return (( BoolErrRecord ) _record).getBooleanValue();
- case CELL_TYPE_STRING:
+ case STRING:
int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
String text = _book.getWorkbook().getSSTString(sstIndex).getString();
return Boolean.valueOf(text).booleanValue();
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return ((NumberRecord)_record).getValue() != 0;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
// use cached formula result if it's the right type:
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
- checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr);
+ checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue();
// Other cases convert to false
// These choices are not well justified.
- case CELL_TYPE_ERROR:
- case CELL_TYPE_BLANK:
+ case ERROR:
+ case BLANK:
return false;
}
throw new RuntimeException("Unexpected cell type (" + _cellType + ")");
@@ -841,18 +825,18 @@ public class HSSFCell implements Cell {
private String convertCellValueToString() {
switch (_cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return "";
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
- case CELL_TYPE_STRING:
+ case STRING:
int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
return _book.getWorkbook().getSSTString(sstIndex).getString();
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return NumberToTextConverter.toText(((NumberRecord)_record).getValue());
- case CELL_TYPE_ERROR:
+ case ERROR:
return FormulaError.forInt(((BoolErrRecord)_record).getErrorValue()).getString();
- case CELL_TYPE_FORMULA:
+ case FORMULA:
// should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
// just use cached formula result instead
break;
@@ -861,37 +845,40 @@ public class HSSFCell implements Cell {
}
FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
FormulaRecord fr = fra.getFormulaRecord();
- switch (fr.getCachedResultType()) {
- case CELL_TYPE_BOOLEAN:
+ switch (CellType.forInt(fr.getCachedResultType())) {
+ case BOOLEAN:
return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
- case CELL_TYPE_STRING:
+ case STRING:
return fra.getStringValue();
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return NumberToTextConverter.toText(fr.getValue());
- case CELL_TYPE_ERROR:
- return FormulaError.forInt(fr.getCachedErrorValue()).getString();
+ case ERROR:
+ return FormulaError.forInt(fr.getCachedErrorValue()).getString();
+ default:
+ throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
}
- throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
+
}
/**
* get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception.
* For blank cells we return a false.
*/
+ @Override
public boolean getBooleanCellValue() {
switch(_cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return false;
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return (( BoolErrRecord ) _record).getBooleanValue();
- default:
- throw typeMismatch(CELL_TYPE_BOOLEAN, _cellType, false);
- case CELL_TYPE_FORMULA:
+ case FORMULA:
break;
+ default:
+ throw typeMismatch(CellType.BOOLEAN, _cellType, false);
}
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
- checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr);
+ checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue();
}
@@ -899,17 +886,18 @@ public class HSSFCell implements Cell {
* get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception.
* For blank cells we return a 0.
*/
+ @Override
public byte getErrorCellValue() {
switch(_cellType) {
- case CELL_TYPE_ERROR:
+ case ERROR:
return (( BoolErrRecord ) _record).getErrorValue();
- default:
- throw typeMismatch(CELL_TYPE_ERROR, _cellType, false);
- case CELL_TYPE_FORMULA:
+ case FORMULA:
break;
+ default:
+ throw typeMismatch(CellType.ERROR, _cellType, false);
}
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
- checkFormulaCachedValueType(CELL_TYPE_ERROR, fr);
+ checkFormulaCachedValueType(CellType.ERROR, fr);
return (byte) fr.getCachedErrorValue();
}
@@ -1008,15 +996,15 @@ public class HSSFCell implements Cell {
*/
public String toString() {
switch (getCellType()) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return "";
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return getBooleanCellValue()?"TRUE":"FALSE";
- case CELL_TYPE_ERROR:
+ case ERROR:
return ErrorEval.getText((( BoolErrRecord ) _record).getErrorValue());
- case CELL_TYPE_FORMULA:
+ case FORMULA:
return getCellFormula();
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
//TODO apply the dataformat for this cell
if (HSSFDateUtil.isCellDateFormatted(this)) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
@@ -1024,7 +1012,7 @@ public class HSSFCell implements Cell {
return sdf.format(getDateCellValue());
}
return String.valueOf(getNumericCellValue());
- case CELL_TYPE_STRING:
+ case STRING:
return getStringCellValue();
default:
return "Unknown Cell Type: " + getCellType();
@@ -1142,22 +1130,23 @@ public class HSSFCell implements Cell {
/**
* Only valid for formula cells
- * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
- * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
+ * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
+ * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula
*/
- public int getCachedFormulaResultType() {
- if (_cellType != CELL_TYPE_FORMULA) {
+ public CellType getCachedFormulaResultType() {
+ if (_cellType != CellType.FORMULA) {
throw new IllegalStateException("Only formula cells have cached results");
}
- return ((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultType();
+ int code = ((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultType();
+ return CellType.forInt(code);
}
void setCellArrayFormula(CellRangeAddress range) {
int row = _record.getRow();
short col = _record.getColumn();
short styleIndex = _record.getXFIndex();
- setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
+ setCellType(CellType.FORMULA, false, row, col, styleIndex);
// Billet for formula in rec
Ptg[] ptgsForCell = {new ExpPtg(range.getFirstRow(), range.getFirstColumn())};
@@ -1166,7 +1155,7 @@ public class HSSFCell implements Cell {
}
public CellRangeAddress getArrayFormulaRange() {
- if (_cellType != CELL_TYPE_FORMULA) {
+ if (_cellType != CellType.FORMULA) {
String ref = new CellReference(this).formatAsString();
throw new IllegalStateException("Cell " + ref
+ " is not part of an array formula.");
@@ -1175,7 +1164,7 @@ public class HSSFCell implements Cell {
}
public boolean isPartOfArrayFormulaGroup() {
- if (_cellType != CELL_TYPE_FORMULA) {
+ if (_cellType != CellType.FORMULA) {
return false;
}
return ((FormulaRecordAggregate)_record).isPartOfArrayFormula();
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java
index afbc0b404..dc51b7d9e 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.usermodel.CellType;
/**
* HSSF wrapper for a cell under evaluation
*/
@@ -34,6 +35,7 @@ final class HSSFEvaluationCell implements EvaluationCell {
public HSSFEvaluationCell(HSSFCell cell) {
this(cell, new HSSFEvaluationSheet(cell.getSheet()));
}
+ @Override
public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key
// Note - this assumes HSSFCell has not overridden hashCode and equals
@@ -43,31 +45,40 @@ final class HSSFEvaluationCell implements EvaluationCell {
public HSSFCell getHSSFCell() {
return _cell;
}
+ @Override
public boolean getBooleanCellValue() {
return _cell.getBooleanCellValue();
}
- public int getCellType() {
+ @Override
+ public CellType getCellType() {
return _cell.getCellType();
}
+ @Override
public int getColumnIndex() {
return _cell.getColumnIndex();
}
+ @Override
public int getErrorCellValue() {
return _cell.getErrorCellValue();
}
+ @Override
public double getNumericCellValue() {
return _cell.getNumericCellValue();
}
+ @Override
public int getRowIndex() {
return _cell.getRowIndex();
}
+ @Override
public EvaluationSheet getSheet() {
return _evalSheet;
}
+ @Override
public String getStringCellValue() {
return _cell.getRichStringCellValue().getString();
}
- public int getCachedFormulaResultType() {
+ @Override
+ public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType();
}
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
index 969da83f9..6582c3810 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
@@ -30,6 +30,7 @@ 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.Row;
@@ -171,26 +172,28 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
* @return
* The cell that is returned will be of the requested type.
* The type can be changed either through calling setCellValue
* or setCellType, but there is a small overhead to doing this,
- * so it is best to create of the required type up front.
+ * so it is best to create the required type up front.
+ *
+ * @param columnIndex - the column number this cell represents
+ *
+ * @return HSSFCell a high level representation of the created cell.
+ * @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
+ * the maximum number of columns supported by the Excel binary format (.xls)
+ * @deprecated POI 3.15 beta 3
+ */
+ @Override
+ public HSSFCell createCell(int columnIndex, int type)
+ {
+ return createCell(columnIndex, CellType.forInt(type));
+ }
+ /**
+ * Use this to create new cells within the row and return it.
+ *
+ * The cell that is returned will be of the requested type.
+ * The type can be changed either through calling setCellValue
+ * or setCellType, but there is a small overhead to doing this,
+ * so it is best to create the required type up front.
*
* @param columnIndex - the column number this cell represents
*
@@ -128,7 +149,7 @@ public final class HSSFRow implements Row, Comparable If the cell currently contains a value, the value will
+ * be converted to match the new type, if possible. Formatting
+ * is generally lost in the process however. If what you want to do is get a String value for your
+ * numeric cell, stop!. This is not the way to do it.
+ * Instead, for fetching the string value of a numeric or boolean
+ * or date cell, use {@link DataFormatter} instead. Note - There is actually no 'DATE' cell type in Excel. In many
* cases (when entering date values), Excel automatically adjusts the
* cell style to some date format, creating the illusion that the cell
- * data type is now something besides {@link Cell#CELL_TYPE_NUMERIC}. POI
+ * data type is now something besides {@link CellType#NUMERIC}. POI
* does not attempt to replicate this behaviour. To make a numeric cell
* display as a date, use {@link #setCellStyle(CellStyle)} etc.null
if the supplied cell is null
or blank
*/
@Override
- public CellValue evaluate(Cell cell) {
+ public CellValue evaluate(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return CellValue.valueOf(cell.getBooleanCellValue());
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
return CellValue.getError(cell.getErrorCellValue());
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
return evaluateFormulaCellValue(cell);
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
return new CellValue(cell.getNumericCellValue());
- case Cell.CELL_TYPE_STRING:
+ case STRING:
return new CellValue(cell.getRichStringCellValue().getString());
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
return null;
+ default:
+ throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
}
- throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
+
}
@@ -210,9 +213,9 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
* @return -1 for non-formula cells, or the type of the formula result
*/
@Override
- public int evaluateFormulaCell(Cell cell) {
- if (cell == null || cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
- return -1;
+ public CellType evaluateFormulaCell(Cell cell) {
+ if (cell == null || cell.getCellType() != CellType.FORMULA) {
+ return CellType._UNINITIALIZED;
}
CellValue cv = evaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is changed
@@ -241,7 +244,7 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
return null;
}
HSSFCell result = (HSSFCell) cell;
- if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
+ if (cell.getCellType() == CellType.FORMULA) {
CellValue cv = evaluateFormulaCellValue(cell);
setCellValue(cell, cv);
setCellType(cell, cv); // cell will no longer be a formula cell
@@ -249,40 +252,42 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
return result;
}
private static void setCellType(Cell cell, CellValue cv) {
- int cellType = cv.getCellType();
+ CellType cellType = cv.getCellType();
switch (cellType) {
- case Cell.CELL_TYPE_BOOLEAN:
- case Cell.CELL_TYPE_ERROR:
- case Cell.CELL_TYPE_NUMERIC:
- case Cell.CELL_TYPE_STRING:
+ case BOOLEAN:
+ case ERROR:
+ case NUMERIC:
+ case STRING:
cell.setCellType(cellType);
return;
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
// never happens - blanks eventually get translated to zero
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
// this will never happen, we have already evaluated the formula
+ default:
+ throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
}
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
+
}
private static void setCellValue(Cell cell, CellValue cv) {
- int cellType = cv.getCellType();
+ CellType cellType = cv.getCellType();
switch (cellType) {
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
cell.setCellValue(cv.getBooleanValue());
break;
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
cell.setCellErrorValue(cv.getErrorValue());
break;
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
cell.setCellValue(cv.getNumberValue());
break;
- case Cell.CELL_TYPE_STRING:
+ case STRING:
cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
break;
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
// never happens - blanks eventually get translated to zero
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
// this will never happen, we have already evaluated the formula
default:
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
@@ -325,7 +330,7 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
for(Row r : sheet) {
for (Cell c : r) {
- if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
+ if (c.getCellType() == CellType.FORMULA) {
evaluator.evaluateFormulaCell(c);
}
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
index 6712e00fb..ba4c7b58c 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
@@ -26,6 +26,7 @@ import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.Configurator;
@@ -110,16 +111,36 @@ public final class HSSFRow implements Row, Comparable
@@ -35,12 +37,12 @@ public interface EvaluationCell {
EvaluationSheet getSheet();
int getRowIndex();
int getColumnIndex();
- int getCellType();
+ CellType getCellType();
double getNumericCellValue();
String getStringCellValue();
boolean getBooleanCellValue();
int getErrorCellValue();
- int getCachedFormulaResultType();
+ CellType getCachedFormulaResultType();
}
diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
index 7a964912a..127fbe520 100644
--- a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
+++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
@@ -76,7 +76,7 @@ import org.apache.poi.ss.formula.ptg.UnionPtg;
import org.apache.poi.ss.formula.ptg.UnknownPtg;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
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.util.CellReference;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -276,7 +276,7 @@ public final class WorkbookEvaluator {
// avoid tracking dependencies to cells that have constant definition
boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true
: !_stabilityClassifier.isCellFinal(sheetIndex, rowIndex, columnIndex);
- if (srcCell == null || srcCell.getCellType() != Cell.CELL_TYPE_FORMULA) {
+ if (srcCell == null || srcCell.getCellType() != CellType.FORMULA) {
ValueEval result = getValueFromNonFormulaCell(srcCell);
if (shouldCellDependencyBeRecorded) {
tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);
@@ -315,22 +315,22 @@ public final class WorkbookEvaluator {
if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) {
logInfo(re.getCause().getMessage() + " - Continuing with cached value!");
switch(srcCell.getCachedFormulaResultType()) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
result = new NumberEval(srcCell.getNumericCellValue());
break;
- case Cell.CELL_TYPE_STRING:
+ case STRING:
result = new StringEval(srcCell.getStringCellValue());
break;
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
result = BlankEval.instance;
break;
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
result = BoolEval.valueOf(srcCell.getBooleanCellValue());
break;
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
result = ErrorEval.valueOf(srcCell.getErrorCellValue());
break;
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
default:
throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
}
@@ -385,20 +385,22 @@ public final class WorkbookEvaluator {
if (cell == null) {
return BlankEval.instance;
}
- int cellType = cell.getCellType();
+ CellType cellType = cell.getCellType();
switch (cellType) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
return new NumberEval(cell.getNumericCellValue());
- case Cell.CELL_TYPE_STRING:
+ case STRING:
return new StringEval(cell.getStringCellValue());
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return BoolEval.valueOf(cell.getBooleanCellValue());
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
return BlankEval.instance;
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
return ErrorEval.valueOf(cell.getErrorCellValue());
+ default:
+ throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
- throw new RuntimeException("Unexpected cell type (" + cellType + ")");
+
}
diff --git a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java
index 2c169e648..efab4912b 100644
--- a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java
+++ b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java
@@ -26,6 +26,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
/**
* Represents a cell being used for forked evaluation that has had a value set different from the
@@ -39,7 +40,7 @@ final class ForkedEvaluationCell implements EvaluationCell {
/** corresponding cell from master workbook */
private final EvaluationCell _masterCell;
private boolean _booleanValue;
- private int _cellType;
+ private CellType _cellType;
private int _errorValue;
private double _numberValue;
private String _stringValue;
@@ -59,64 +60,64 @@ final class ForkedEvaluationCell implements EvaluationCell {
Class extends ValueEval> cls = value.getClass();
if (cls == NumberEval.class) {
- _cellType = Cell.CELL_TYPE_NUMERIC;
+ _cellType = CellType.NUMERIC;
_numberValue = ((NumberEval)value).getNumberValue();
return;
}
if (cls == StringEval.class) {
- _cellType = Cell.CELL_TYPE_STRING;
+ _cellType = CellType.STRING;
_stringValue = ((StringEval)value).getStringValue();
return;
}
if (cls == BoolEval.class) {
- _cellType = Cell.CELL_TYPE_BOOLEAN;
+ _cellType = CellType.BOOLEAN;
_booleanValue = ((BoolEval)value).getBooleanValue();
return;
}
if (cls == ErrorEval.class) {
- _cellType = Cell.CELL_TYPE_ERROR;
+ _cellType = CellType.ERROR;
_errorValue = ((ErrorEval)value).getErrorCode();
return;
}
if (cls == BlankEval.class) {
- _cellType = Cell.CELL_TYPE_BLANK;
+ _cellType = CellType.BLANK;
return;
}
throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
}
public void copyValue(Cell destCell) {
switch (_cellType) {
- case Cell.CELL_TYPE_BLANK: destCell.setCellType(Cell.CELL_TYPE_BLANK); return;
- case Cell.CELL_TYPE_NUMERIC: destCell.setCellValue(_numberValue); return;
- case Cell.CELL_TYPE_BOOLEAN: destCell.setCellValue(_booleanValue); return;
- case Cell.CELL_TYPE_STRING: destCell.setCellValue(_stringValue); return;
- case Cell.CELL_TYPE_ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
+ case BLANK: destCell.setCellType(CellType.BLANK); return;
+ case NUMERIC: destCell.setCellValue(_numberValue); return;
+ case BOOLEAN: destCell.setCellValue(_booleanValue); return;
+ case STRING: destCell.setCellValue(_stringValue); return;
+ case ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
+ default: throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
}
- throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
}
- private void checkCellType(int expectedCellType) {
+ private void checkCellType(CellType expectedCellType) {
if (_cellType != expectedCellType) {
throw new RuntimeException("Wrong data type (" + _cellType + ")");
}
}
- public int getCellType() {
+ public CellType getCellType() {
return _cellType;
}
public boolean getBooleanCellValue() {
- checkCellType(Cell.CELL_TYPE_BOOLEAN);
+ checkCellType(CellType.BOOLEAN);
return _booleanValue;
}
public int getErrorCellValue() {
- checkCellType(Cell.CELL_TYPE_ERROR);
+ checkCellType(CellType.ERROR);
return _errorValue;
}
public double getNumericCellValue() {
- checkCellType(Cell.CELL_TYPE_NUMERIC);
+ checkCellType(CellType.NUMERIC);
return _numberValue;
}
public String getStringCellValue() {
- checkCellType(Cell.CELL_TYPE_STRING);
+ checkCellType(CellType.STRING);
return _stringValue;
}
public EvaluationSheet getSheet() {
@@ -128,7 +129,7 @@ final class ForkedEvaluationCell implements EvaluationCell {
public int getColumnIndex() {
return _masterCell.getColumnIndex();
}
- public int getCachedFormulaResultType() {
+ public CellType getCachedFormulaResultType() {
return _masterCell.getCachedFormulaResultType();
}
diff --git a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
index 9ae19a191..abbf3a301 100644
--- a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
+++ b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
@@ -33,7 +33,6 @@ import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.WorkbookEvaluator;
-import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
/**
@@ -114,20 +113,21 @@ public final class ForkedEvaluator {
EvaluationCell cell = _sewb.getEvaluationCell(sheetName, rowIndex, columnIndex);
switch (cell.getCellType()) {
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return BoolEval.valueOf(cell.getBooleanCellValue());
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
return ErrorEval.valueOf(cell.getErrorCellValue());
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
return _evaluator.evaluate(cell);
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
return new NumberEval(cell.getNumericCellValue());
- case Cell.CELL_TYPE_STRING:
+ case STRING:
return new StringEval(cell.getStringCellValue());
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
return null;
+ default:
+ throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
}
- throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
}
/**
* Coordinates several formula evaluators together so that formulas that involve external
diff --git a/src/java/org/apache/poi/ss/usermodel/Cell.java b/src/java/org/apache/poi/ss/usermodel/Cell.java
index 65cade5cb..be6638954 100644
--- a/src/java/org/apache/poi/ss/usermodel/Cell.java
+++ b/src/java/org/apache/poi/ss/usermodel/Cell.java
@@ -43,43 +43,49 @@ public interface Cell {
* Numeric Cell type (0)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#NUMERIC} instead.
*/
- int CELL_TYPE_NUMERIC = 0;
+ CellType CELL_TYPE_NUMERIC = CellType.NUMERIC;
/**
* String Cell type (1)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#STRING} instead.
*/
- int CELL_TYPE_STRING = 1;
+ CellType CELL_TYPE_STRING = CellType.STRING;
/**
* Formula Cell type (2)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#FORMULA} instead.
*/
- int CELL_TYPE_FORMULA = 2;
+ CellType CELL_TYPE_FORMULA = CellType.FORMULA;
/**
* Blank Cell type (3)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#BLANK} instead.
*/
- int CELL_TYPE_BLANK = 3;
+ CellType CELL_TYPE_BLANK = CellType.BLANK;
/**
* Boolean Cell type (4)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#BOOLEAN} instead.
*/
- int CELL_TYPE_BOOLEAN = 4;
+ CellType CELL_TYPE_BOOLEAN = CellType.BOOLEAN;
/**
* Error Cell type (5)
* @see #setCellType(int)
* @see #getCellType()
+ * @deprecated POI 3.15 beta 3. Use {@link CellType#ERROR} instead.
*/
- int CELL_TYPE_ERROR = 5;
+ CellType CELL_TYPE_ERROR = CellType.ERROR;
/**
* Returns column index of this cell
@@ -127,29 +133,38 @@ public interface Cell {
* @see #CELL_TYPE_BLANK
* @see #CELL_TYPE_BOOLEAN
* @see #CELL_TYPE_ERROR
+ * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/
void setCellType(int cellType);
+ /**
+ * Set the cells type (numeric, formula or string).
+ * SUM(C4:E4)
*
* @return a formula for the cell
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA}
*/
String getCellFormula();
@@ -244,7 +259,7 @@ public interface Cell {
* For formulas or error cells we return the precalculated value;
* double
.
* @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
*/
@@ -256,7 +271,7 @@ public interface Cell {
* For strings we throw an exception. For blank cells we return a null.
*
double
.
* @see DataFormatter for formatting this date into a string similar to how excel does.
*/
@@ -309,7 +324,7 @@ public interface Cell {
*
* @return the value of the cell as a boolean
* @throws IllegalStateException if the cell type returned by {@link #getCellType()}
- * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA
+ * is not {@link CellType#BOOLEAN}, {@link CellType#BLANK} or {@link CellType#FORMULA}
*/
boolean getBooleanCellValue();
@@ -321,13 +336,13 @@ public interface Cell {
*
*
* @return the value of the cell as an error code
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType#ERROR}
* @see FormulaError for error codes
*/
byte getErrorCellValue();
/**
- * Set the style for the cell. The style should be an CellStyle created/retreived from + *
Set the style for the cell. The style should be an CellStyle created/retrieved from * the Workbook.
* *To change the style of a cell without affecting other cells that use the same style,
diff --git a/src/java/org/apache/poi/ss/usermodel/CellType.java b/src/java/org/apache/poi/ss/usermodel/CellType.java
new file mode 100644
index 000000000..d6ef7b321
--- /dev/null
+++ b/src/java/org/apache/poi/ss/usermodel/CellType.java
@@ -0,0 +1,78 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.usermodel;
+
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.util.Internal;
+
+public enum CellType {
+ @Internal
+ _UNINITIALIZED(-1),
+
+ /** Numeric cell type (whole numbers, fractional numbers, dates) */
+ NUMERIC(0),
+
+ /** String cell type */
+ STRING(1),
+
+ /**
+ * Formula cell type
+ * @see FormulaType
+ */
+ FORMULA(2),
+
+ /**
+ * Blank cell type
+ */
+ BLANK(3),
+
+ /**
+ * Boolean cell type
+ */
+ BOOLEAN(4),
+
+ /**
+ * Error cell type
+ * @see FormulaError
+ */
+ ERROR(5);
+
+ /** @deprecated POI 3.15 beta 3 */
+ private final int code;
+
+ /** @deprecated POI 3.15 beta 3 */
+ private CellType(int code) {
+ this.code = code;
+ }
+
+ /** @deprecated POI 3.15 beta 3. Used to transition code from Example:
diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
index 951b06af1..1f7c8fd83 100644
--- a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
+++ b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
@@ -99,7 +99,7 @@ public interface FormulaEvaluator {
* or one of Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR
* Note: the cell's type remains as Cell.CELL_TYPE_FORMULA however.
*/
- int evaluateFormulaCell(Cell cell);
+ CellType evaluateFormulaCell(Cell cell);
/**
* If cell contains formula, it evaluates the formula, and
diff --git a/src/java/org/apache/poi/ss/usermodel/Row.java b/src/java/org/apache/poi/ss/usermodel/Row.java
index a2bacb487..b17edcd30 100644
--- a/src/java/org/apache/poi/ss/usermodel/Row.java
+++ b/src/java/org/apache/poi/ss/usermodel/Row.java
@@ -27,7 +27,7 @@ public interface Row extends Iterable
- * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed
+ * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
* either through calling
+ * The cell that is returned will be of the requested type.
+ * The type can be changed either through calling setCellValue
+ * or setCellType, but there is a small overhead to doing this,
+ * so it is best to create of the required type up front.
+ *
+ * @param column - the column number this cell represents
+ * @param type - the cell's data type
+ * @return Cell a high level representation of the created cell.
+ * @throws IllegalArgumentException if columnIndex < 0 or greater than a maximum number of supported columns
+ * (255 for *.xls, 1048576 for *.xlsx)
+ */
+ Cell createCell(int column, CellType type);
/**
* Remove the Cell from this row.
diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java
index d73c3d618..291ab549e 100644
--- a/src/java/org/apache/poi/ss/util/SheetUtil.java
+++ b/src/java/org/apache/poi/ss/util/SheetUtil.java
@@ -28,6 +28,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font;
@@ -79,7 +80,7 @@ public class SheetUtil {
public void setIgnoreMissingWorkbooks(boolean ignore) {}
public void evaluateAll() {}
- public int evaluateFormulaCell(Cell cell) {
+ public CellType evaluateFormulaCell(Cell cell) {
return cell.getCachedFormulaResultType();
}
};
@@ -119,15 +120,16 @@ public class SheetUtil {
}
CellStyle style = cell.getCellStyle();
- int cellType = cell.getCellType();
+ CellType cellType = cell.getCellType();
// for formula cells we compute the cell width for the cached formula result
- if(cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
+ if (cellType == CellType.FORMULA)
+ cellType = cell.getCachedFormulaResultType();
Font font = wb.getFontAt(style.getFontIndex());
double width = -1;
- if (cellType == Cell.CELL_TYPE_STRING) {
+ if (cellType == CellType.STRING) {
RichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (int i = 0; i < lines.length; i++) {
@@ -144,14 +146,14 @@ public class SheetUtil {
}
} else {
String sval = null;
- if (cellType == Cell.CELL_TYPE_NUMERIC) {
+ if (cellType == CellType.NUMERIC) {
// Try to get it formatted to look the same as excel
try {
sval = formatter.formatCellValue(cell, dummyEvaluator);
} catch (Exception e) {
sval = String.valueOf(cell.getNumericCellValue());
}
- } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
+ } else if (cellType == CellType.BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
}
if(sval != null) {
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
index 923c5aed8..719dd636e 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
@@ -26,6 +26,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.HeaderFooter;
@@ -161,19 +162,19 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
Cell cell = ri.next();
// Is it a formula one?
- if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
+ if(cell.getCellType() == CellType.FORMULA) {
if (formulasNotResults) {
String contents = cell.getCellFormula();
checkMaxTextSize(text, contents);
text.append(contents);
} else {
- if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
+ if (cell.getCachedFormulaResultType() == CellType.STRING) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
}
- } else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
+ } else if(cell.getCellType() == CellType.STRING) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
@@ -235,12 +236,12 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
}
private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
- int type = cell.getCellType();
- if (type == Cell.CELL_TYPE_FORMULA) {
+ CellType type = cell.getCellType();
+ if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultType();
}
- if (type == Cell.CELL_TYPE_NUMERIC) {
+ if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) {
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
index d187b66ca..f9d3c876f 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
@@ -42,7 +42,7 @@ import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.LocaleUtil;
@@ -280,11 +280,11 @@ public class XSSFExportToXml implements Comparatorint
s to CellType
s. */
+ public static CellType forInt(int code) {
+ for (CellType type : values()) {
+ if (type.code == code) {
+ return type;
+ }
+ }
+ throw new IllegalArgumentException("Invalid CellType code: " + code);
+ }
+
+ /** @deprecated POI 3.15 beta 3 */
+ public int getCode() {
+ return code;
+ }
+
+}
diff --git a/src/java/org/apache/poi/ss/usermodel/CellValue.java b/src/java/org/apache/poi/ss/usermodel/CellValue.java
index cfe69ac7f..52fea225a 100644
--- a/src/java/org/apache/poi/ss/usermodel/CellValue.java
+++ b/src/java/org/apache/poi/ss/usermodel/CellValue.java
@@ -23,19 +23,18 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
* Mimics the 'data view' of a cell. This allows formula evaluator
* to return a CellValue instead of precasting the value to String
* or Number or boolean type.
- * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
*/
public final class CellValue {
- public static final CellValue TRUE = new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, true, null, 0);
- public static final CellValue FALSE= new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, false, null, 0);
+ public static final CellValue TRUE = new CellValue(CellType.BOOLEAN, 0.0, true, null, 0);
+ public static final CellValue FALSE= new CellValue(CellType.BOOLEAN, 0.0, false, null, 0);
- private final int _cellType;
+ private final CellType _cellType;
private final double _numberValue;
private final boolean _booleanValue;
private final String _textValue;
private final int _errorCode;
- private CellValue(int cellType, double numberValue, boolean booleanValue,
+ private CellValue(CellType cellType, double numberValue, boolean booleanValue,
String textValue, int errorCode) {
_cellType = cellType;
_numberValue = numberValue;
@@ -46,16 +45,16 @@ public final class CellValue {
public CellValue(double numberValue) {
- this(Cell.CELL_TYPE_NUMERIC, numberValue, false, null, 0);
+ this(CellType.NUMERIC, numberValue, false, null, 0);
}
public static CellValue valueOf(boolean booleanValue) {
return booleanValue ? TRUE : FALSE;
}
public CellValue(String stringValue) {
- this(Cell.CELL_TYPE_STRING, 0.0, false, stringValue, 0);
+ this(CellType.STRING, 0.0, false, stringValue, 0);
}
public static CellValue getError(int errorCode) {
- return new CellValue(Cell.CELL_TYPE_ERROR, 0.0, false, null, errorCode);
+ return new CellValue(CellType.ERROR, 0.0, false, null, errorCode);
}
@@ -80,7 +79,7 @@ public final class CellValue {
/**
* @return Returns the cellType.
*/
- public int getCellType() {
+ public CellType getCellType() {
return _cellType;
}
/**
@@ -99,15 +98,17 @@ public final class CellValue {
public String formatAsString() {
switch (_cellType) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
return String.valueOf(_numberValue);
- case Cell.CELL_TYPE_STRING:
+ case STRING:
return '"' + _textValue + '"';
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return _booleanValue ? "TRUE" : "FALSE";
- case Cell.CELL_TYPE_ERROR:
+ case ERROR:
return ErrorEval.getText(_errorCode);
+ default:
+ return "setCellValue
or setCellType
.
*
* @param column - the column number this cell represents
@@ -50,14 +50,30 @@ public interface Row extends IterableSUM(C4:E4)
*
* @return a formula for the cell
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CellType.FORMULA
*/
@Override
public String getCellFormula()
{
- if(_value.getType()!=CELL_TYPE_FORMULA)
- throw typeMismatch(CELL_TYPE_FORMULA,_value.getType(),false);
+ if(_value.getType()!=CellType.FORMULA)
+ throw typeMismatch(CellType.FORMULA,_value.getType(),false);
return ((FormulaValue)_value).getValue();
}
@@ -341,29 +347,29 @@ public class SXSSFCell implements Cell {
* For formulas or error cells we return the precalculated value;
* double
.
* @see org.apache.poi.ss.usermodel.DataFormatter for turning this number into a string similar to that which Excel would render this number as.
*/
@Override
public double getNumericCellValue()
{
- int cellType = getCellType();
+ CellType cellType = getCellType();
switch(cellType)
{
- case CELL_TYPE_BLANK:
+ case BLANK:
return 0.0;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
- if(fv.getFormulaType()!=CELL_TYPE_NUMERIC)
- throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, false);
+ if(fv.getFormulaType()!=CellType.NUMERIC)
+ throw typeMismatch(CellType.NUMERIC, CellType.FORMULA, false);
return ((NumericFormulaValue)_value).getPreEvaluatedValue();
}
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return ((NumericValue)_value).getValue();
default:
- throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);
+ throw typeMismatch(CellType.NUMERIC, cellType, false);
}
}
@@ -373,15 +379,15 @@ public class SXSSFCell implements Cell {
* For strings we throw an exception. For blank cells we return a null.
* double
.
* @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does.
*/
@Override
public Date getDateCellValue()
{
- int cellType = getCellType();
- if (cellType == CELL_TYPE_BLANK)
+ CellType cellType = getCellType();
+ if (cellType == CellType.BLANK)
{
return null;
}
@@ -402,9 +408,9 @@ public class SXSSFCell implements Cell {
@Override
public RichTextString getRichStringCellValue()
{
- int cellType = getCellType();
- if(getCellType() != CELL_TYPE_STRING)
- throw typeMismatch(CELL_TYPE_STRING, cellType, false);
+ CellType cellType = getCellType();
+ if(getCellType() != CellType.STRING)
+ throw typeMismatch(CellType.STRING, cellType, false);
StringValue sval = (StringValue)_value;
if(sval.isRichText())
@@ -427,19 +433,19 @@ public class SXSSFCell implements Cell {
@Override
public String getStringCellValue()
{
- int cellType = getCellType();
+ CellType cellType = getCellType();
switch(cellType)
{
- case CELL_TYPE_BLANK:
+ case BLANK:
return "";
- case CELL_TYPE_FORMULA:
+ case FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
- if(fv.getFormulaType()!=CELL_TYPE_STRING)
- throw typeMismatch(CELL_TYPE_STRING, CELL_TYPE_FORMULA, false);
+ if(fv.getFormulaType()!=CellType.STRING)
+ throw typeMismatch(CellType.STRING, CellType.FORMULA, false);
return ((StringFormulaValue)_value).getPreEvaluatedValue();
}
- case CELL_TYPE_STRING:
+ case STRING:
{
if(((StringValue)_value).isRichText())
return ((RichTextValue)_value).getValue().getString();
@@ -447,7 +453,7 @@ public class SXSSFCell implements Cell {
return ((PlainStringValue)_value).getValue();
}
default:
- throw typeMismatch(CELL_TYPE_STRING, cellType, false);
+ throw typeMismatch(CellType.STRING, cellType, false);
}
}
@@ -461,8 +467,8 @@ public class SXSSFCell implements Cell {
@Override
public void setCellValue(boolean value)
{
- ensureTypeOrFormulaType(CELL_TYPE_BOOLEAN);
- if(_value.getType()==CELL_TYPE_FORMULA)
+ ensureTypeOrFormulaType(CellType.BOOLEAN);
+ if(_value.getType()==CellType.FORMULA)
((BooleanFormulaValue)_value).setPreEvaluatedValue(value);
else
((BooleanValue)_value).setValue(value);
@@ -480,8 +486,8 @@ public class SXSSFCell implements Cell {
@Override
public void setCellErrorValue(byte value)
{
- ensureType(CELL_TYPE_ERROR);
- if(_value.getType()==CELL_TYPE_FORMULA)
+ ensureType(CellType.ERROR);
+ if(_value.getType()==CellType.FORMULA)
((ErrorFormulaValue)_value).setPreEvaluatedValue(value);
else
((ErrorValue)_value).setValue(value);
@@ -494,29 +500,29 @@ public class SXSSFCell implements Cell {
*
- * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed
+ * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
* either through calling
- * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed
+ * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
+ * either through calling setCellValue or setCellType.
+ *
+ * @param column - the column number this cell represents
+ * @return Cell a high level representation of the created cell.
+ * @throws IllegalArgumentException if columnIndex < 0 or greate than a maximum number of supported columns
+ * (255 for *.xls, 1048576 for *.xlsx)
+ * @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead.
+ */
+ @Override
+ public SXSSFCell createCell(int column, int type)
+ {
+ return createCell(column, CellType.forInt(type));
+ }
+ /**
+ * Use this to create new cells within the row and return it.
+ *
+ * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
* either through calling setCellValue or setCellType.
*
* @param column - the column number this cell represents
@@ -145,7 +163,7 @@ public class SXSSFRow implements Row, ComparablesetCellValue
or setCellType
.
*
* @param column - the column number this cell represents
@@ -130,13 +131,30 @@ public class SXSSFRow implements Row, Comparable
double
.
* @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
*/
@Override
public double getNumericCellValue() {
- int cellType = getCellType();
+ CellType cellType = getCellType();
switch(cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return 0.0;
- case CELL_TYPE_FORMULA:
- case CELL_TYPE_NUMERIC:
+ case FORMULA:
+ // fall-through
+ case NUMERIC:
if(_cell.isSetV()) {
String v = _cell.getV();
if (v.isEmpty()) return 0.0;
try {
return Double.parseDouble(v);
} catch(NumberFormatException e) {
- throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false);
+ throw typeMismatch(CellType.NUMERIC, CellType.STRING, false);
}
} else {
return 0.0;
}
default:
- throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);
+ throw typeMismatch(CellType.NUMERIC, cellType, false);
}
}
@@ -361,13 +366,13 @@ public final class XSSFCell implements Cell {
*/
@Override
public XSSFRichTextString getRichStringCellValue() {
- int cellType = getCellType();
+ CellType cellType = getCellType();
XSSFRichTextString rt;
switch (cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
rt = new XSSFRichTextString("");
break;
- case CELL_TYPE_STRING:
+ case STRING:
if (_cell.getT() == STCellType.INLINE_STR) {
if(_cell.isSetIs()) {
//string is expressed directly in the cell definition instead of implementing the shared string table.
@@ -391,18 +396,18 @@ public final class XSSFCell implements Cell {
}
}
break;
- case CELL_TYPE_FORMULA:
- checkFormulaCachedValueType(CELL_TYPE_STRING, getBaseCellType(false));
+ case FORMULA:
+ checkFormulaCachedValueType(CellType.STRING, getBaseCellType(false));
rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
break;
default:
- throw typeMismatch(CELL_TYPE_STRING, cellType, false);
+ throw typeMismatch(CellType.STRING, cellType, false);
}
rt.setStylesTableReference(_stylesSource);
return rt;
}
- private static void checkFormulaCachedValueType(int expectedTypeCode, int cachedValueType) {
+ private static void checkFormulaCachedValueType(CellType expectedTypeCode, CellType cachedValueType) {
if (cachedValueType != expectedTypeCode) {
throw typeMismatch(expectedTypeCode, cachedValueType, true);
}
@@ -432,7 +437,7 @@ public final class XSSFCell implements Cell {
@Override
public void setCellValue(RichTextString str) {
if(str == null || str.getString() == null){
- setCellType(Cell.CELL_TYPE_BLANK);
+ setCellType(CellType.BLANK);
return;
}
@@ -440,9 +445,9 @@ public final class XSSFCell implements Cell {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
- int cellType = getCellType();
- switch(cellType){
- case Cell.CELL_TYPE_FORMULA:
+ CellType cellType = getCellType();
+ switch (cellType){
+ case FORMULA:
_cell.setV(str.getString());
_cell.setT(STCellType.STR);
break;
@@ -465,7 +470,7 @@ public final class XSSFCell implements Cell {
* Return a formula for the cell, for example, SUM(C4:E4)
*
* @return a formula for the cell
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA}
*/
@Override
public String getCellFormula() {
@@ -478,11 +483,11 @@ public final class XSSFCell implements Cell {
*
* @param fpb evaluation workbook for reuse, if available, or null to create a new one as needed
* @return a formula for the cell
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA}
*/
protected String getCellFormula(XSSFEvaluationWorkbook fpb) {
- int cellType = getCellType();
- if(cellType != CELL_TYPE_FORMULA) throw typeMismatch(CELL_TYPE_FORMULA, cellType, false);
+ CellType cellType = getCellType();
+ if(cellType != CellType.FORMULA) throw typeMismatch(CellType.FORMULA, cellType, false);
CTCellFormula f = _cell.getF();
if (isPartOfArrayFormulaGroup() && f == null) {
@@ -660,28 +665,22 @@ public final class XSSFCell implements Cell {
* Return the cell type.
*
* @return the cell type
- * @see Cell#CELL_TYPE_BLANK
- * @see Cell#CELL_TYPE_NUMERIC
- * @see Cell#CELL_TYPE_STRING
- * @see Cell#CELL_TYPE_FORMULA
- * @see Cell#CELL_TYPE_BOOLEAN
- * @see Cell#CELL_TYPE_ERROR
*/
@Override
- public int getCellType() {
- if (isFormulaCell()) return CELL_TYPE_FORMULA;
+ public CellType getCellType() {
+ if (isFormulaCell()) return CellType.FORMULA;
return getBaseCellType(true);
}
/**
* Only valid for formula cells
- * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
- * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
+ * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
+ * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula
*/
@Override
- public int getCachedFormulaResultType() {
+ public CellType getCachedFormulaResultType() {
if (! isFormulaCell()) {
throw new IllegalStateException("Only formula cells have cached results");
}
@@ -692,10 +691,10 @@ public final class XSSFCell implements Cell {
/**
* Detect cell type based on the "t" attribute of the CTCell bean
*/
- private int getBaseCellType(boolean blankCells) {
+ private CellType getBaseCellType(boolean blankCells) {
switch (_cell.getT().intValue()) {
case STCellType.INT_B:
- return CELL_TYPE_BOOLEAN;
+ return CellType.BOOLEAN;
case STCellType.INT_N:
if (!_cell.isSetV() && blankCells) {
// ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as
@@ -703,15 +702,15 @@ public final class XSSFCell implements Cell {
// The formula evaluator (and perhaps other clients of this interface) needs to
// distinguish blank values which sometimes get translated into zero and sometimes
// empty string, depending on context
- return CELL_TYPE_BLANK;
+ return CellType.BLANK;
}
- return CELL_TYPE_NUMERIC;
+ return CellType.NUMERIC;
case STCellType.INT_E:
- return CELL_TYPE_ERROR;
+ return CellType.ERROR;
case STCellType.INT_S: // String is in shared strings
case STCellType.INT_INLINE_STR: // String is inline in cell
case STCellType.INT_STR:
- return CELL_TYPE_STRING;
+ return CellType.STRING;
default:
throw new IllegalStateException("Illegal cell type: " + this._cell.getT());
}
@@ -723,13 +722,13 @@ public final class XSSFCell implements Cell {
* For strings we throw an exception. For blank cells we return a null.
*
* @return the value of the cell as a date
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING}
* @exception NumberFormatException if the cell value isn't a parsable double
.
* @see DataFormatter for formatting this date into a string similar to how excel does.
*/
@Override
public Date getDateCellValue() {
- if (getCellType() == CELL_TYPE_BLANK) {
+ if (getCellType() == CellType.BLANK) {
return null;
}
@@ -749,7 +748,7 @@ public final class XSSFCell implements Cell {
@Override
public void setCellValue(Date value) {
if(value == null) {
- setCellType(Cell.CELL_TYPE_BLANK);
+ setCellType(CellType.BLANK);
return;
}
@@ -776,7 +775,7 @@ public final class XSSFCell implements Cell {
@Override
public void setCellValue(Calendar value) {
if(value == null) {
- setCellType(Cell.CELL_TYPE_BLANK);
+ setCellType(CellType.BLANK);
return;
}
@@ -788,12 +787,12 @@ public final class XSSFCell implements Cell {
* Returns the error message, such as #VALUE!
*
* @return the error message such as #VALUE!
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType#ERROR}
* @see FormulaError
*/
public String getErrorCellString() {
- int cellType = getBaseCellType(true);
- if(cellType != CELL_TYPE_ERROR) throw typeMismatch(CELL_TYPE_ERROR, cellType, false);
+ CellType cellType = getBaseCellType(true);
+ if(cellType != CellType.ERROR) throw typeMismatch(CellType.ERROR, cellType, false);
return _cell.getV();
}
@@ -805,7 +804,7 @@ public final class XSSFCell implements Cell {
*
*
* @return the value of the cell as an error code
- * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR
+ * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType #ERROR}
* @see FormulaError
*/
@Override
@@ -881,41 +880,40 @@ public final class XSSFCell implements Cell {
* Set the cells type (numeric, formula or string)
*
* @throws IllegalArgumentException if the specified cell type is invalid
- * @see #CELL_TYPE_NUMERIC
- * @see #CELL_TYPE_STRING
- * @see #CELL_TYPE_FORMULA
- * @see #CELL_TYPE_BLANK
- * @see #CELL_TYPE_BOOLEAN
- * @see #CELL_TYPE_ERROR
+ * @see CellType#NUMERIC
+ * @see CellType#STRING
+ * @see CellType#FORMULA
+ * @see CellType#BLANK
+ * @see CellType#BOOLEAN
+ * @see CellType#ERROR
+ * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/
@Override
public void setCellType(int cellType) {
- int prevType = getCellType();
+ setCellType(CellType.forInt(cellType));
+ }
+ /**
+ * Set the cells type (numeric, formula or string)
+ *
+ * @throws IllegalArgumentException if the specified cell type is invalid
+ */
+ @Override
+ public void setCellType(CellType cellType) {
+ CellType prevType = getCellType();
if(isPartOfArrayFormulaGroup()){
notifyArrayFormulaChanging();
}
- if(prevType == CELL_TYPE_FORMULA && cellType != CELL_TYPE_FORMULA) {
+ if(prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
getSheet().getWorkbook().onDeleteFormula(this);
}
switch (cellType) {
- case CELL_TYPE_BLANK:
- setBlank();
- break;
- case CELL_TYPE_BOOLEAN:
- String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
- _cell.setT(STCellType.B);
- _cell.setV(newVal);
- break;
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
_cell.setT(STCellType.N);
break;
- case CELL_TYPE_ERROR:
- _cell.setT(STCellType.E);
- break;
- case CELL_TYPE_STRING:
- if(prevType != CELL_TYPE_STRING){
+ case STRING:
+ if(prevType != CellType.STRING){
String str = convertCellValueToString();
XSSFRichTextString rt = new XSSFRichTextString(str);
rt.setStylesTableReference(_stylesSource);
@@ -924,7 +922,7 @@ public final class XSSFCell implements Cell {
}
_cell.setT(STCellType.S);
break;
- case CELL_TYPE_FORMULA:
+ case FORMULA:
if(!_cell.isSetF()){
CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue("0");
@@ -932,10 +930,24 @@ public final class XSSFCell implements Cell {
if(_cell.isSetT()) _cell.unsetT();
}
break;
+ case BLANK:
+ setBlank();
+ break;
+ case BOOLEAN:
+ String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
+ _cell.setT(STCellType.B);
+ _cell.setV(newVal);
+ break;
+
+ case ERROR:
+ _cell.setT(STCellType.E);
+ break;
+
+
default:
throw new IllegalArgumentException("Illegal cell type: " + cellType);
}
- if (cellType != CELL_TYPE_FORMULA && _cell.isSetF()) {
+ if (cellType != CellType.FORMULA && _cell.isSetF()) {
_cell.unsetF();
}
}
@@ -951,23 +963,23 @@ public final class XSSFCell implements Cell {
@Override
public String toString() {
switch (getCellType()) {
- case CELL_TYPE_BLANK:
- return "";
- case CELL_TYPE_BOOLEAN:
- return getBooleanCellValue() ? "TRUE" : "FALSE";
- case CELL_TYPE_ERROR:
- return ErrorEval.getText(getErrorCellValue());
- case CELL_TYPE_FORMULA:
- return getCellFormula();
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone());
return sdf.format(getDateCellValue());
}
return Double.toString(getNumericCellValue());
- case CELL_TYPE_STRING:
+ case STRING:
return getRichStringCellValue().toString();
+ case FORMULA:
+ return getCellFormula();
+ case BLANK:
+ return "";
+ case BOOLEAN:
+ return getBooleanCellValue() ? TRUE : FALSE;
+ case ERROR:
+ return ErrorEval.getText(getErrorCellValue());
default:
return "Unknown Cell Type: " + getCellType();
}
@@ -989,28 +1001,12 @@ public final class XSSFCell implements Cell {
return _cell.getV();
}
- /**
- * Used to help format error messages
- */
- private static String getCellTypeName(int cellTypeCode) {
- switch (cellTypeCode) {
- case CELL_TYPE_BLANK: return "blank";
- case CELL_TYPE_STRING: return "text";
- case CELL_TYPE_BOOLEAN: return "boolean";
- case CELL_TYPE_ERROR: return "error";
- case CELL_TYPE_NUMERIC: return "numeric";
- case CELL_TYPE_FORMULA: return "formula";
- }
- return "#unknown cell type (" + cellTypeCode + ")#";
- }
/**
* Used to help format error messages
*/
- private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) {
- String msg = "Cannot get a "
- + getCellTypeName(expectedTypeCode) + " value from a "
- + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
+ private static RuntimeException typeMismatch(CellType expectedType, CellType actualType, boolean isFormulaCell) {
+ String msg = "Cannot get a " + expectedType + " value from a " + actualType+ " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg);
}
@@ -1137,46 +1133,49 @@ public final class XSSFCell implements Cell {
* TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
*/
private boolean convertCellValueToBoolean() {
- int cellType = getCellType();
+ CellType cellType = getCellType();
- if (cellType == CELL_TYPE_FORMULA) {
+ if (cellType == CellType.FORMULA) {
cellType = getBaseCellType(false);
}
switch (cellType) {
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
return TRUE_AS_STRING.equals(_cell.getV());
- case CELL_TYPE_STRING:
+ case STRING:
int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
String text = rt.getString();
return Boolean.parseBoolean(text);
- case CELL_TYPE_NUMERIC:
+ case NUMERIC:
return Double.parseDouble(_cell.getV()) != 0;
- case CELL_TYPE_ERROR:
- case CELL_TYPE_BLANK:
+ case ERROR:
+ // fall-through
+ case BLANK:
return false;
+
+ default:
+ throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
- throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
private String convertCellValueToString() {
- int cellType = getCellType();
+ CellType cellType = getCellType();
switch (cellType) {
- case CELL_TYPE_BLANK:
+ case BLANK:
return "";
- case CELL_TYPE_BOOLEAN:
- return TRUE_AS_STRING.equals(_cell.getV()) ? "TRUE" : "FALSE";
- case CELL_TYPE_STRING:
+ case BOOLEAN:
+ return TRUE_AS_STRING.equals(_cell.getV()) ? TRUE : FALSE;
+ case STRING:
int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
return rt.getString();
- case CELL_TYPE_NUMERIC:
- case CELL_TYPE_ERROR:
+ case NUMERIC:
+ case ERROR:
return _cell.getV();
- case CELL_TYPE_FORMULA:
+ case FORMULA:
// should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
// just use cached formula result instead
break;
@@ -1186,21 +1185,27 @@ public final class XSSFCell implements Cell {
cellType = getBaseCellType(false);
String textValue = _cell.getV();
switch (cellType) {
- case CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
if (TRUE_AS_STRING.equals(textValue)) {
- return "TRUE";
+ return TRUE;
}
if (FALSE_AS_STRING.equals(textValue)) {
- return "FALSE";
+ return FALSE;
}
throw new IllegalStateException("Unexpected boolean cached formula value '"
+ textValue + "'.");
- case CELL_TYPE_STRING:
- case CELL_TYPE_NUMERIC:
- case CELL_TYPE_ERROR:
+
+ case STRING:
+ // fall-through
+ case NUMERIC:
+ // fall-through
+ case ERROR:
return textValue;
+
+ default:
+ throw new IllegalStateException("Unexpected formula result type (" + cellType + ")");
}
- throw new IllegalStateException("Unexpected formula result type (" + cellType + ")");
+
}
@Override
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java
index 9fd3d9edd..40b44001a 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.usermodel.CellType;
/**
* XSSF wrapper for a cell under evaluation
@@ -37,6 +38,7 @@ final class XSSFEvaluationCell implements EvaluationCell {
this(cell, new XSSFEvaluationSheet(cell.getSheet()));
}
+ @Override
public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key
// Note - this assumes XSSFCell has not overridden hashCode and equals
@@ -46,31 +48,40 @@ final class XSSFEvaluationCell implements EvaluationCell {
public XSSFCell getXSSFCell() {
return _cell;
}
+ @Override
public boolean getBooleanCellValue() {
return _cell.getBooleanCellValue();
}
- public int getCellType() {
+ @Override
+ public CellType getCellType() {
return _cell.getCellType();
}
+ @Override
public int getColumnIndex() {
return _cell.getColumnIndex();
}
+ @Override
public int getErrorCellValue() {
return _cell.getErrorCellValue();
}
+ @Override
public double getNumericCellValue() {
return _cell.getNumericCellValue();
}
+ @Override
public int getRowIndex() {
return _cell.getRowIndex();
}
+ @Override
public EvaluationSheet getSheet() {
return _evalSheet;
}
+ @Override
public String getStringCellValue() {
return _cell.getRichStringCellValue().getString();
}
- public int getCachedFormulaResultType() {
+ @Override
+ public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType();
}
}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
index c4e63c70f..8196abfb4 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
@@ -27,6 +27,7 @@ import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
@@ -184,7 +185,7 @@ public class XSSFRow implements Row, Comparable
- * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed
+ * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
* either through calling setCellValue
or setCellType
.
*
* SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell.
- * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell
+ * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly identifies the cell
*/
@Test
public void testReadSheetWithRK() throws IOException {
HSSFWorkbook wb = openSample("rk.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFCell c = s.getRow(0).getCell(0);
- int a = c.getCellType();
- assertEquals(a, Cell.CELL_TYPE_NUMERIC);
+ assertEquals(CellType.NUMERIC, c.getCellType());
wb.close();
}
diff --git a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
index 6afcc445e..18f79f1a6 100644
--- a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
+++ b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
@@ -42,6 +42,7 @@ import org.apache.poi.ss.formula.ptg.IntPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefErrorPtg;
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.Name;
@@ -200,7 +201,7 @@ public class TestWorkbookEvaluator {
} catch (RuntimeException e) {
fail("Missing arg result not being handled correctly.");
}
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+ assertEquals(CellType.NUMERIC, cv.getCellType());
// adding blank to 1.0 gives 1.0
assertEquals(1.0, cv.getNumberValue(), 0.0);
@@ -208,7 +209,7 @@ public class TestWorkbookEvaluator {
cell.setCellFormula("\"abc\"&IF(1,,)");
fe.notifySetFormula(cell);
cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType());
+ assertEquals(CellType.STRING, cv.getCellType());
// adding blank to "abc" gives "abc"
assertEquals("abc", cv.getStringValue());
@@ -216,7 +217,7 @@ public class TestWorkbookEvaluator {
cell.setCellFormula("\"abc\"&CHOOSE(2,5,,9)");
fe.notifySetFormula(cell);
cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType());
+ assertEquals(CellType.STRING, cv.getCellType());
// adding blank to "abc" gives "abc"
assertEquals("abc", cv.getStringValue());
}
@@ -242,14 +243,14 @@ public class TestWorkbookEvaluator {
}
throw new RuntimeException(e);
}
- assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
+ assertEquals(CellType.ERROR, cv.getCellType());
assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv.getErrorValue());
// verify circular refs are still detected properly
fe.clearAllCachedResultValues();
cell.setCellFormula("OFFSET(A1,0,0)");
cv = fe.evaluate(cell);
- assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
+ assertEquals(CellType.ERROR, cv.getCellType());
assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
} finally {
wb.close();
@@ -304,7 +305,7 @@ public class TestWorkbookEvaluator {
// Test IF-Equals Formula Evaluation (bug 58591)
- private Workbook testIFEqualsFormulaEvaluation_setup(String formula, int a1CellType) {
+ private Workbook testIFEqualsFormulaEvaluation_setup(String formula, CellType a1CellType) {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("IFEquals");
Row row = sheet.createRow(0);
@@ -314,27 +315,29 @@ public class TestWorkbookEvaluator {
Cell D1 = row.createCell(3);
switch (a1CellType) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
A1.setCellValue(1.0);
// "A1=1" should return true
break;
- case Cell.CELL_TYPE_STRING:
+ case STRING:
A1.setCellValue("1");
// "A1=1" should return false
// "A1=\"1\"" should return true
break;
- case Cell.CELL_TYPE_BOOLEAN:
+ case BOOLEAN:
A1.setCellValue(true);
// "A1=1" should return true
break;
- case Cell.CELL_TYPE_FORMULA:
+ case FORMULA:
A1.setCellFormula("1");
// "A1=1" should return true
break;
- case Cell.CELL_TYPE_BLANK:
+ case BLANK:
A1.setCellValue((String) null);
// "A1=1" should return false
break;
+ default:
+ throw new IllegalArgumentException("unexpected cell type: " + a1CellType);
}
B1.setCellValue(2.0);
C1.setCellValue(3.0);
@@ -354,7 +357,7 @@ public class TestWorkbookEvaluator {
private void testIFEqualsFormulaEvaluation_evaluate(
- String formula, int cellType, String expectedFormula, double expectedResult) {
+ String formula, CellType cellType, String expectedFormula, double expectedResult) {
Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@@ -362,17 +365,17 @@ public class TestWorkbookEvaluator {
CellValue result = eval.evaluate(D1);
// Call should not modify the contents
- assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType());
+ assertEquals(CellType.FORMULA, D1.getCellType());
assertEquals(expectedFormula, D1.getCellFormula());
- assertEquals(Cell.CELL_TYPE_NUMERIC, result.getCellType());
+ assertEquals(CellType.NUMERIC, result.getCellType());
assertEquals(expectedResult, result.getNumberValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb);
}
private void testIFEqualsFormulaEvaluation_eval(
- final String formula, final int cellType, final String expectedFormula, final double expectedValue) {
+ final String formula, final CellType cellType, final String expectedFormula, final double expectedValue) {
testIFEqualsFormulaEvaluation_evaluate(formula, cellType, expectedFormula, expectedValue);
testIFEqualsFormulaEvaluation_evaluateFormulaCell(formula, cellType, expectedFormula, expectedValue);
testIFEqualsFormulaEvaluation_evaluateInCell(formula, cellType, expectedFormula, expectedValue);
@@ -383,7 +386,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_NumericLiteral() {
final String formula = "IF(A1=1, 2, 3)";
- final int cellType = Cell.CELL_TYPE_NUMERIC;
+ final CellType cellType = CellType.NUMERIC;
final String expectedFormula = "IF(A1=1,2,3)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -392,7 +395,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_Numeric() {
final String formula = "IF(A1=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_NUMERIC;
+ final CellType cellType = CellType.NUMERIC;
final String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -401,7 +404,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_NumericCoerceToString() {
final String formula = "IF(A1&\"\"=\"1\", B1, C1)";
- final int cellType = Cell.CELL_TYPE_NUMERIC;
+ final CellType cellType = CellType.NUMERIC;
final String expectedFormula = "IF(A1&\"\"=\"1\",B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -410,7 +413,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_String() {
final String formula = "IF(A1=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_STRING;
+ final CellType cellType = CellType.STRING;
final String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 3.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -419,7 +422,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_StringCompareToString() {
final String formula = "IF(A1=\"1\", B1, C1)";
- final int cellType = Cell.CELL_TYPE_STRING;
+ final CellType cellType = CellType.STRING;
final String expectedFormula = "IF(A1=\"1\",B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -428,7 +431,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_StringCoerceToNumeric() {
final String formula = "IF(A1+0=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_STRING;
+ final CellType cellType = CellType.STRING;
final String expectedFormula = "IF(A1+0=1,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -438,7 +441,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_Boolean() {
final String formula = "IF(A1=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_BOOLEAN;
+ final CellType cellType = CellType.BOOLEAN;
final String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -448,7 +451,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_BooleanSimple() {
final String formula = "3-(A1=1)";
- final int cellType = Cell.CELL_TYPE_BOOLEAN;
+ final CellType cellType = CellType.BOOLEAN;
final String expectedFormula = "3-(A1=1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -457,7 +460,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_Formula() {
final String formula = "IF(A1=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_FORMULA;
+ final CellType cellType = CellType.FORMULA;
final String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -466,7 +469,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_Blank() {
final String formula = "IF(A1=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_BLANK;
+ final CellType cellType = CellType.BLANK;
final String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 3.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -475,7 +478,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_BlankCompareToZero() {
final String formula = "IF(A1=0, B1, C1)";
- final int cellType = Cell.CELL_TYPE_BLANK;
+ final CellType cellType = CellType.BLANK;
final String expectedFormula = "IF(A1=0,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -485,7 +488,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_BlankInverted() {
final String formula = "IF(NOT(A1)=1, B1, C1)";
- final int cellType = Cell.CELL_TYPE_BLANK;
+ final CellType cellType = CellType.BLANK;
final String expectedFormula = "IF(NOT(A1)=1,B1,C1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -495,7 +498,7 @@ public class TestWorkbookEvaluator {
@Test
public void testIFEqualsFormulaEvaluation_BlankInvertedSimple() {
final String formula = "3-(NOT(A1)=1)";
- final int cellType = Cell.CELL_TYPE_BLANK;
+ final CellType cellType = CellType.BLANK;
final String expectedFormula = "3-(NOT(A1)=1)";
final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@@ -503,25 +506,25 @@ public class TestWorkbookEvaluator {
private void testIFEqualsFormulaEvaluation_evaluateFormulaCell(
- String formula, int cellType, String expectedFormula, double expectedResult) {
+ String formula, CellType cellType, String expectedFormula, double expectedResult) {
Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
- int resultCellType = eval.evaluateFormulaCell(D1);
+ CellType resultCellType = eval.evaluateFormulaCell(D1);
// Call should modify the contents, but leave the formula intact
- assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType());
+ assertEquals(CellType.FORMULA, D1.getCellType());
assertEquals(expectedFormula, D1.getCellFormula());
- assertEquals(Cell.CELL_TYPE_NUMERIC, resultCellType);
- assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType());
+ assertEquals(CellType.NUMERIC, resultCellType);
+ assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb);
}
private void testIFEqualsFormulaEvaluation_evaluateInCell(
- String formula, int cellType, String expectedFormula, double expectedResult) {
+ String formula, CellType cellType, String expectedFormula, double expectedResult) {
Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@@ -534,14 +537,14 @@ public class TestWorkbookEvaluator {
D1.getCellFormula();
fail("cell formula should be overwritten with formula result");
} catch (final IllegalStateException expected) { }
- assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCellType());
+ assertEquals(CellType.NUMERIC, D1.getCellType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb);
}
private void testIFEqualsFormulaEvaluation_evaluateAll(
- String formula, int cellType, String expectedFormula, double expectedResult) {
+ String formula, CellType cellType, String expectedFormula, double expectedResult) {
Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@@ -549,28 +552,28 @@ public class TestWorkbookEvaluator {
eval.evaluateAll();
// Call should modify the contents
- assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType());
+ assertEquals(CellType.FORMULA, D1.getCellType());
assertEquals(expectedFormula, D1.getCellFormula());
- assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType());
+ assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb);
}
private void testIFEqualsFormulaEvaluation_evaluateAllFormulaCells(
- String formula, int cellType, String expectedFormula, double expectedResult) {
+ String formula, CellType cellType, String expectedFormula, double expectedResult) {
Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
// Call should modify the contents
- assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType());
+ assertEquals(CellType.FORMULA, D1.getCellType());
// whitespace gets deleted because formula is parsed and re-rendered
assertEquals(expectedFormula, D1.getCellFormula());
- assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType());
+ assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb);
diff --git a/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java
index 85ddfe01c..8457a07d9 100644
--- a/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java
+++ b/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java
@@ -31,6 +31,7 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.TestMathX;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
@@ -173,7 +174,7 @@ public final class TestFormulasFromSpreadsheet {
// iterate across the row for all the evaluation cases
for (int colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) {
Cell c = formulasRow.getCell(colnum);
- if (c == null || c.getCellType() != Cell.CELL_TYPE_FORMULA) {
+ if (c == null || c.getCellType() != CellType.FORMULA) {
continue;
}
@@ -186,28 +187,31 @@ public final class TestFormulasFromSpreadsheet {
assertNotNull(msg + " - Bad setup data expected value is null", expValue);
assertNotNull(msg + " - actual value was null", actValue);
- switch (expValue.getCellType()) {
- case Cell.CELL_TYPE_BLANK:
- assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType());
+ final CellType cellType = expValue.getCellType();
+ switch (cellType) {
+ case BLANK:
+ assertEquals(msg, CellType.BLANK, actValue.getCellType());
break;
- case Cell.CELL_TYPE_BOOLEAN:
- assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType());
+ case BOOLEAN:
+ assertEquals(msg, CellType.BOOLEAN, actValue.getCellType());
assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue());
break;
- case Cell.CELL_TYPE_ERROR:
- assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType());
+ case ERROR:
+ assertEquals(msg, CellType.ERROR, actValue.getCellType());
assertEquals(msg, ErrorEval.getText(expValue.getErrorCellValue()), ErrorEval.getText(actValue.getErrorValue()));
break;
- case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation
+ case FORMULA: // will never be used, since we will call method after formula evaluation
fail("Cannot expect formula as result of formula evaluation: " + msg);
- case Cell.CELL_TYPE_NUMERIC:
- assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType());
+ case NUMERIC:
+ assertEquals(msg, CellType.NUMERIC, actValue.getCellType());
TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
break;
- case Cell.CELL_TYPE_STRING:
- assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType());
+ case STRING:
+ assertEquals(msg, CellType.STRING, actValue.getCellType());
assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue());
break;
+ default:
+ fail("Unexpected cell type: " + cellType);
}
}
}
@@ -224,10 +228,10 @@ public final class TestFormulasFromSpreadsheet {
System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+ if(cell.getCellType() == CellType.BLANK) {
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
+ if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString();
}
diff --git a/src/testcases/org/apache/poi/ss/formula/eval/TestMultiSheetEval.java b/src/testcases/org/apache/poi/ss/formula/eval/TestMultiSheetEval.java
index 9e52a4efd..bb9b6a85b 100644
--- a/src/testcases/org/apache/poi/ss/formula/eval/TestMultiSheetEval.java
+++ b/src/testcases/org/apache/poi/ss/formula/eval/TestMultiSheetEval.java
@@ -30,6 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.TestMathX;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
@@ -104,29 +105,33 @@ public final class TestMultiSheetEval extends TestCase {
if(actual == null) {
throw new AssertionFailedError(msg + " - actual value was null");
}
+
+ final CellType cellType = expected.getCellType();
- switch (expected.getCellType()) {
- case Cell.CELL_TYPE_BLANK:
- assertEquals(msg, Cell.CELL_TYPE_BLANK, actual.getCellType());
+ switch (cellType) {
+ case BLANK:
+ assertEquals(msg, CellType.BLANK, actual.getCellType());
break;
- case Cell.CELL_TYPE_BOOLEAN:
- assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actual.getCellType());
+ case BOOLEAN:
+ assertEquals(msg, CellType.BOOLEAN, actual.getCellType());
assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue());
break;
- case Cell.CELL_TYPE_ERROR:
- assertEquals(msg, Cell.CELL_TYPE_ERROR, actual.getCellType());
+ case ERROR:
+ assertEquals(msg, CellType.ERROR, actual.getCellType());
assertEquals(msg, ErrorEval.getText(expected.getErrorCellValue()), ErrorEval.getText(actual.getErrorValue()));
break;
- case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation
+ case FORMULA: // will never be used, since we will call method after formula evaluation
throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg);
- case Cell.CELL_TYPE_NUMERIC:
- assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actual.getCellType());
+ case NUMERIC:
+ assertEquals(msg, CellType.NUMERIC, actual.getCellType());
TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
break;
- case Cell.CELL_TYPE_STRING:
- assertEquals(msg, Cell.CELL_TYPE_STRING, actual.getCellType());
+ case STRING:
+ assertEquals(msg, CellType.STRING, actual.getCellType());
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break;
+ default:
+ throw new AssertionFailedError("Unexpected cell type: " + cellType);
}
}
@@ -226,7 +231,7 @@ public final class TestMultiSheetEval extends TestCase {
int result = Result.NO_EVALUATIONS_FOUND; // so far
Cell c = formulasRow.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE);
- if (c == null || c.getCellType() != Cell.CELL_TYPE_FORMULA) {
+ if (c == null || c.getCellType() != CellType.FORMULA) {
return result;
}
@@ -295,10 +300,10 @@ public final class TestMultiSheetEval extends TestCase {
System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+ if(cell.getCellType() == CellType.BLANK) {
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
+ if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString();
}
@@ -318,10 +323,10 @@ public final class TestMultiSheetEval extends TestCase {
System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_TEST_NAME + ", can't figure out test name");
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+ if(cell.getCellType() == CellType.BLANK) {
return null;
}
- if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
+ if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString();
}
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/BaseTestFunctionsFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/functions/BaseTestFunctionsFromSpreadsheet.java
index e222220ce..4fed4d48f 100644
--- a/src/testcases/org/apache/poi/ss/formula/functions/BaseTestFunctionsFromSpreadsheet.java
+++ b/src/testcases/org/apache/poi/ss/formula/functions/BaseTestFunctionsFromSpreadsheet.java
@@ -36,6 +36,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -122,7 +123,7 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
currentGroupComment = newMarkerValue;
}
HSSFCell evalCell = r.getCell(SS.COLUMN_INDEX_EVALUATION);
- if (evalCell == null || evalCell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
+ if (evalCell == null || evalCell.getCellType() != CellType.FORMULA) {
continue;
}
String rowComment = getCellTextValue(r, SS.COLUMN_ROW_COMMENT, "row comment");
@@ -152,9 +153,9 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
assertNotNull(msg + " - Bad setup data expected value is null", expectedCell);
assertNotNull(msg + " - actual value was null", actualValue);
- if (expectedCell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
+ if (expectedCell.getCellType() == CellType.ERROR) {
int expectedErrorCode = expectedCell.getErrorCellValue();
- assertEquals(msg, HSSFCell.CELL_TYPE_ERROR, actualValue.getCellType());
+ assertEquals(msg, CellType.ERROR, actualValue.getCellType());
assertEquals(msg, ErrorEval.getText(expectedErrorCode), actualValue.formatAsString());
assertEquals(msg, expectedErrorCode, actualValue.getErrorValue());
assertEquals(msg, ErrorEval.getText(expectedErrorCode), ErrorEval.getText(actualValue.getErrorValue()));
@@ -162,24 +163,27 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
}
// unexpected error
- assertNotEquals(msg, HSSFCell.CELL_TYPE_ERROR, actualValue.getCellType());
+ assertNotEquals(msg, CellType.ERROR, actualValue.getCellType());
assertNotEquals(msg, formatValue(expectedCell), ErrorEval.getText(actualValue.getErrorValue()));
// wrong type error
assertEquals(msg, expectedCell.getCellType(), actualValue.getCellType());
- switch (expectedCell.getCellType()) {
- case HSSFCell.CELL_TYPE_BOOLEAN:
+ final CellType expectedCellType = expectedCell.getCellType();
+ switch (expectedCellType) {
+ case BOOLEAN:
assertEquals(msg, expectedCell.getBooleanCellValue(), actualValue.getBooleanValue());
break;
- case HSSFCell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation
+ case FORMULA: // will never be used, since we will call method after formula evaluation
fail("Cannot expect formula as result of formula evaluation: " + msg);
- case HSSFCell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
assertEquals(expectedCell.getNumericCellValue(), actualValue.getNumberValue(), 0.0);
break;
- case HSSFCell.CELL_TYPE_STRING:
+ case STRING:
assertEquals(msg, expectedCell.getRichStringCellValue().getString(), actualValue.getStringValue());
break;
+ default:
+ fail("Unexpected cell type: " + expectedCellType);
}
}
@@ -208,10 +212,10 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
if(cell == null) {
return null;
}
- if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
+ if(cell.getCellType() == CellType.BLANK) {
return null;
}
- if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
+ if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString();
}
@@ -222,12 +226,12 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
private static String formatValue(HSSFCell expecedCell) {
switch (expecedCell.getCellType()) {
- case HSSFCell.CELL_TYPE_BLANK: return "