diff options
54 files changed, 1334 insertions, 1117 deletions
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 27f168e31e..6470fe923b 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 9714b7e2b0..c4a2d1dd08 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 db7f6304f7..e77a18196c 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 74e09ae94a..0a35eaa116 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 d59d4666df..3f2ef7ea09 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 e5f37aefcf..267fbfd43d 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 e7b2019b14..605e328a04 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 91605873ec..4eae6ce5c2 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 f3aaf872e0..62769e0e4d 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 08825ce358..003a93ae1c 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. * <p> - * 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 afbc0b4041..dc51b7d9e4 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 969da83f9d..6582c3810c 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 <code>null</code> if the supplied cell is <code>null</code> 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 <em>formula result</em> */ @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 6712e00fb8..ba4c7b58c9 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,26 +111,46 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> { @Override public HSSFCell createCell(int column) { - return this.createCell(column,Cell.CELL_TYPE_BLANK); + return this.createCell(column,CellType.BLANK); } - + /** * Use this to create new cells within the row and return it. * <p> * 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. + * <p> + * 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 + * + * @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) + */ + @Override + public HSSFCell createCell(int columnIndex, CellType type) + { short shortCellNum = (short)columnIndex; if(columnIndex > 0x7FFF) { shortCellNum = (short)(0xffff - columnIndex); @@ -367,10 +388,10 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> { case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: - boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK); + boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: - return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } diff --git a/src/java/org/apache/poi/ss/format/CellFormat.java b/src/java/org/apache/poi/ss/format/CellFormat.java index d3a7f8df43..df60021dd6 100644 --- a/src/java/org/apache/poi/ss/format/CellFormat.java +++ b/src/java/org/apache/poi/ss/format/CellFormat.java @@ -29,6 +29,7 @@ import java.util.regex.Pattern; import javax.swing.JLabel; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.ConditionalFormatting; import org.apache.poi.ss.usermodel.ConditionalFormattingRule; import org.apache.poi.ss.usermodel.DataFormatter; @@ -265,11 +266,11 @@ public class CellFormat { */ public CellFormatResult apply(Cell c) { switch (ultimateType(c)) { - case Cell.CELL_TYPE_BLANK: + case BLANK: return apply(""); - case Cell.CELL_TYPE_BOOLEAN: + case BOOLEAN: return apply(c.getBooleanCellValue()); - case Cell.CELL_TYPE_NUMERIC: + case NUMERIC: Double value = c.getNumericCellValue(); if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) { if (DateUtil.isValidExcelDate(value)) { @@ -280,7 +281,7 @@ public class CellFormat { } else { return apply(value); } - case Cell.CELL_TYPE_STRING: + case STRING: return apply(c.getStringCellValue()); default: return apply("?"); @@ -335,26 +336,26 @@ public class CellFormat { */ public CellFormatResult apply(JLabel label, Cell c) { switch (ultimateType(c)) { - case Cell.CELL_TYPE_BLANK: - return apply(label, ""); - case Cell.CELL_TYPE_BOOLEAN: - return apply(label, c.getBooleanCellValue()); - case Cell.CELL_TYPE_NUMERIC: - Double value = c.getNumericCellValue(); - if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) { - if (DateUtil.isValidExcelDate(value)) { - return apply(label, c.getDateCellValue(), value); + case BLANK: + return apply(label, ""); + case BOOLEAN: + return apply(label, c.getBooleanCellValue()); + case NUMERIC: + Double value = c.getNumericCellValue(); + if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) { + if (DateUtil.isValidExcelDate(value)) { + return apply(label, c.getDateCellValue(), value); + } else { + return apply(label, INVALID_VALUE_FOR_FORMAT); + } } else { - return apply(label, INVALID_VALUE_FOR_FORMAT); + return apply(label, value); } - } else { - return apply(label, value); + case STRING: + return apply(label, c.getStringCellValue()); + default: + return apply(label, "?"); } - case Cell.CELL_TYPE_STRING: - return apply(label, c.getStringCellValue()); - default: - return apply(label, "?"); - } } /** @@ -417,9 +418,9 @@ public class CellFormat { * * @return The ultimate type of this cell. */ - public static int ultimateType(Cell cell) { - int type = cell.getCellType(); - if (type == Cell.CELL_TYPE_FORMULA) + public static CellType ultimateType(Cell cell) { + CellType type = cell.getCellType(); + if (type == CellType.FORMULA) return cell.getCachedFormulaResultType(); else return type; diff --git a/src/java/org/apache/poi/ss/formula/EvaluationCell.java b/src/java/org/apache/poi/ss/formula/EvaluationCell.java index a3de9e8945..b5a051fec0 100644 --- a/src/java/org/apache/poi/ss/formula/EvaluationCell.java +++ b/src/java/org/apache/poi/ss/formula/EvaluationCell.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.formula; +import org.apache.poi.ss.usermodel.CellType; + /** * Abstracts a cell for the purpose of formula evaluation. This interface represents both formula * and non-formula cells.<br/> @@ -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 7a964912ad..127fbe520e 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 2c169e648e..efab4912b0 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 9ae19a1917..abbf3a3011 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 65cade5cba..be66389543 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). + * <p>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.</p> + * <p>If what you want to do is get a String value for your + * numeric cell, <i>stop!</i>. 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.</p> + * + * @throws IllegalArgumentException if the specified cell type is invalid + * @throws IllegalStateException if the current value cannot be converted to the new type + */ + void setCellType(CellType cellType); /** * 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 */ - int getCellType(); + CellType getCellType(); /** * 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 */ - int getCachedFormulaResultType(); + CellType getCachedFormulaResultType(); /** * Set a numeric value for the cell @@ -167,7 +182,7 @@ public interface Cell { * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many * cases (when entering date values), Excel automatically adjusts the * <i>cell style</i> 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.</p> * @@ -233,7 +248,7 @@ public interface Cell { * Return a formula for the cell, for example, <code>SUM(C4:E4)</code> * * @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; * </p> * @return the value of the cell as a number - * @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 <code>double</code>. * @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. * </p> * @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 <code>double</code>. * @see DataFormatter for formatting this date into a string similar to how excel does. */ @@ -309,7 +324,7 @@ public interface Cell { * </p> * @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 { * </p> * * @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(); /** - * <p>Set the style for the cell. The style should be an CellStyle created/retreived from + * <p>Set the style for the cell. The style should be an CellStyle created/retrieved from * the Workbook.</p> * * <p>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 0000000000..d6ef7b3218 --- /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 <code>int</code>s to <code>CellType</code>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 cfe69ac7f2..52fea225ab 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 "<error unexpected cell type " + _cellType + ">"; } - return "<error unexpected cell type " + _cellType + ">"; + } } diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 4de0936f58..225652f852 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -879,32 +879,33 @@ public class DataFormatter implements Observer { return ""; } - int cellType = cell.getCellType(); - if (cellType == Cell.CELL_TYPE_FORMULA) { + CellType cellType = cell.getCellType(); + if (cellType == CellType.FORMULA) { if (evaluator == null) { return cell.getCellFormula(); } cellType = evaluator.evaluateFormulaCell(cell); } switch (cellType) { - case Cell.CELL_TYPE_NUMERIC : + case NUMERIC : if (DateUtil.isCellDateFormatted(cell)) { return getFormattedDateString(cell); } return getFormattedNumberString(cell); - case Cell.CELL_TYPE_STRING : + case STRING : return cell.getRichStringCellValue().getString(); - case Cell.CELL_TYPE_BOOLEAN : + case BOOLEAN : return String.valueOf(cell.getBooleanCellValue()); - case Cell.CELL_TYPE_BLANK : + case BLANK : return ""; - case Cell.CELL_TYPE_ERROR: - return FormulaError.forInt(cell.getErrorCellValue()).getString(); + case ERROR: + return FormulaError.forInt(cell.getErrorCellValue()).getString(); + default: + throw new RuntimeException("Unexpected celltype (" + cellType + ")"); } - throw new RuntimeException("Unexpected celltype (" + cellType + ")"); } diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaError.java b/src/java/org/apache/poi/ss/usermodel/FormulaError.java index e540d69a07..6415fc0364 100644 --- a/src/java/org/apache/poi/ss/usermodel/FormulaError.java +++ b/src/java/org/apache/poi/ss/usermodel/FormulaError.java @@ -17,6 +17,9 @@ package org.apache.poi.ss.usermodel; import java.util.Map; + +import org.apache.poi.util.Internal; + import java.util.HashMap; /** @@ -25,6 +28,9 @@ import java.util.HashMap; * See also OOO's excelfileformat.pdf (2.5.6) */ public enum FormulaError { + @Internal + _NO_ERROR(-1, "(no error)"), + /** * Intended to indicate when two areas are required to intersect, but do not. * <p>Example: diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java index 951b06af1c..1f7c8fd833 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 a2bacb487f..b17edcd303 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<Cell> { /** * Use this to create new cells within the row and return it. * <p> - * 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 <code>setCellValue</code> or <code>setCellType</code>. * * @param column - the column number this cell represents @@ -50,14 +50,30 @@ public interface Row extends Iterable<Cell> { * @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) - * @see Cell#CELL_TYPE_BLANK - * @see Cell#CELL_TYPE_BOOLEAN - * @see Cell#CELL_TYPE_ERROR - * @see Cell#CELL_TYPE_FORMULA - * @see Cell#CELL_TYPE_NUMERIC - * @see Cell#CELL_TYPE_STRING + * @see CellType#BLANK + * @see CellType#BOOLEAN + * @see CellType#ERROR + * @see CellType#FORMULA + * @see CellType#NUMERIC + * @see CellType#STRING + * @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead. */ Cell createCell(int column, int type); + /** + * Use this to create new cells within the row and return it. + * <p> + * 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 d73c3d6184..291ab549e6 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 923c5aed84..719dd636e5 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 d187b66ca1..f9d3c876f1 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 Comparator<String>{ String value =""; switch (cell.getCellType()) { - case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; - case XSSFCell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue(); break; - case XSSFCell.CELL_TYPE_ERROR: value = cell.getErrorCellString(); break; - case XSSFCell.CELL_TYPE_FORMULA: - if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { + case STRING: value = cell.getStringCellValue(); break; + case BOOLEAN: value += cell.getBooleanCellValue(); break; + case ERROR: value = cell.getErrorCellString(); break; + case FORMULA: + if (cell.getCachedFormulaResultType() == CellType.STRING) { value = cell.getStringCellValue(); } else { if (DateUtil.isCellDateFormatted(cell)) { @@ -295,7 +295,7 @@ public class XSSFExportToXml implements Comparator<String>{ } break; - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = getFormattedDate(cell); } else { diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index cf1b419902..5a6ac555e0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.formula.eval.ErrorEval; 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.DateUtil; import org.apache.poi.ss.usermodel.FormulaError; @@ -55,7 +56,7 @@ public class SXSSFCell implements Cell { private CellStyle _style; private Property _firstProperty; - public SXSSFCell(SXSSFRow row,int cellType) + public SXSSFCell(SXSSFRow row,CellType cellType) { _row=row; setType(cellType); @@ -119,16 +120,27 @@ public class SXSSFCell 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) { + ensureType(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) + { ensureType(cellType); } @@ -136,29 +148,23 @@ public class SXSSFCell 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() + public CellType getCellType() { return _value.getType(); } /** * 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 (_value.getType() != CELL_TYPE_FORMULA) { + if (_value.getType() != CellType.FORMULA) { throw new IllegalStateException("Only formula cells have cached results"); } @@ -182,8 +188,8 @@ public class SXSSFCell implements Cell { } else if (Double.isNaN(value)){ setCellErrorValue(FormulaError.NUM.getCode()); } else { - ensureTypeOrFormulaType(CELL_TYPE_NUMERIC); - if(_value.getType()==CELL_TYPE_FORMULA) + ensureTypeOrFormulaType(CellType.NUMERIC); + if(_value.getType()==CellType.FORMULA) ((NumericFormulaValue)_value).setPreEvaluatedValue(value); else ((NumericValue)_value).setValue(value); @@ -197,7 +203,7 @@ public class SXSSFCell implements Cell { * <b>Note</b> - There is actually no 'DATE' cell type in Excel. In many * cases (when entering date values), Excel automatically adjusts the * <i>cell style</i> 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. * @@ -208,7 +214,7 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(Date value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -235,7 +241,7 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(Calendar value) { if(value == null) { - setCellType(Cell.CELL_TYPE_BLANK); + setCellType(CellType.BLANK); return; } @@ -267,7 +273,7 @@ public class SXSSFCell implements Cell { ((RichTextValue)_value).setValue(xvalue); } else { - setCellType(CELL_TYPE_BLANK); + setCellType(CellType.BLANK); } } @@ -283,18 +289,18 @@ public class SXSSFCell implements Cell { public void setCellValue(String value) { if (value != null) { - ensureTypeOrFormulaType(CELL_TYPE_STRING); + ensureTypeOrFormulaType(CellType.STRING); if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); } - if(_value.getType()==CELL_TYPE_FORMULA) + if(_value.getType()==CellType.FORMULA) ((StringFormulaValue)_value).setPreEvaluatedValue(value); else ((PlainStringValue)_value).setValue(value); } else { - setCellType(CELL_TYPE_BLANK); + setCellType(CellType.BLANK); } } @@ -313,7 +319,7 @@ public class SXSSFCell implements Cell { public void setCellFormula(String formula) throws FormulaParseException { if(formula == null) { - setType(Cell.CELL_TYPE_BLANK); + setType(CellType.BLANK); return; } @@ -324,13 +330,13 @@ public class SXSSFCell implements Cell { * Return a formula for the cell, for example, <code>SUM(C4:E4)</code> * * @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; * </p> * @return the value of the cell as a number - * @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 CellType.STRING * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @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. * </p> * @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 CellType.STRING * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @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 { * </p> * @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 CellType.BOOLEAN, CellType.BLANK or CellType.FORMULA */ @Override public boolean getBooleanCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return false; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_BOOLEAN) - throw typeMismatch(CELL_TYPE_BOOLEAN, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.BOOLEAN) + throw typeMismatch(CellType.BOOLEAN, CellType.FORMULA, false); return ((BooleanFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { return ((BooleanValue)_value).getValue(); } default: - throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); + throw typeMismatch(CellType.BOOLEAN, cellType, false); } } @@ -528,30 +534,30 @@ public class SXSSFCell implements Cell { * </p> * * @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 CellType.ERROR * @see org.apache.poi.ss.usermodel.FormulaError for error codes */ @Override public byte getErrorCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return 0; - case CELL_TYPE_FORMULA: + case FORMULA: { FormulaValue fv=(FormulaValue)_value; - if(fv.getFormulaType()!=CELL_TYPE_ERROR) - throw typeMismatch(CELL_TYPE_ERROR, CELL_TYPE_FORMULA, false); + if(fv.getFormulaType()!=CellType.ERROR) + throw typeMismatch(CellType.ERROR, CellType.FORMULA, false); return ((ErrorFormulaValue)_value).getPreEvaluatedValue(); } - case CELL_TYPE_ERROR: + case ERROR: { return ((ErrorValue)_value).getValue(); } default: - throw typeMismatch(CELL_TYPE_ERROR, cellType, false); + throw typeMismatch(CellType.ERROR, cellType, false); } } @@ -709,22 +715,22 @@ public class SXSSFCell implements Cell { @Override 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(getErrorCellValue()); - case CELL_TYPE_FORMULA: + case 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 getNumericCellValue() + ""; - case CELL_TYPE_STRING: + case STRING: return getRichStringCellValue().toString(); default: return "Unknown Cell Type: " + getCellType(); @@ -806,39 +812,39 @@ public class SXSSFCell implements Cell { } /*package*/ void ensurePlainStringType() { - if(_value.getType()!=CELL_TYPE_STRING + if(_value.getType()!=CellType.STRING ||((StringValue)_value).isRichText()) _value=new PlainStringValue(); } /*package*/ void ensureRichTextStringType() { - if(_value.getType()!=CELL_TYPE_STRING + if(_value.getType()!=CellType.STRING ||!((StringValue)_value).isRichText()) _value=new RichTextValue(); } - /*package*/ void ensureType(int type) + /*package*/ void ensureType(CellType type) { if(_value.getType()!=type) setType(type); } - /*package*/ void ensureFormulaType(int type) + /*package*/ void ensureFormulaType(CellType type) { - if(_value.getType()!=CELL_TYPE_FORMULA + if(_value.getType()!=CellType.FORMULA ||((FormulaValue)_value).getFormulaType()!=type) setFormulaType(type); } /* * Sets the cell type to type if it is different */ - /*package*/ void ensureTypeOrFormulaType(int type) + /*package*/ void ensureTypeOrFormulaType(CellType type) { if(_value.getType()==type) { - if(type==CELL_TYPE_STRING&&((StringValue)_value).isRichText()) - setType(CELL_TYPE_STRING); + if(type==CellType.STRING&&((StringValue)_value).isRichText()) + setType(CellType.STRING); return; } - if(_value.getType()==CELL_TYPE_FORMULA) + if(_value.getType()==CellType.FORMULA) { if(((FormulaValue)_value).getFormulaType()==type) return; @@ -854,16 +860,16 @@ public class SXSSFCell implements Cell { * @param type the cell type to set * @throws IllegalArgumentException if type is not a recognized type */ - /*package*/ void setType(int type) + /*package*/ void setType(CellType type) { switch(type) { - case CELL_TYPE_NUMERIC: + case NUMERIC: { _value=new NumericValue(); break; } - case CELL_TYPE_STRING: + case STRING: { PlainStringValue sval = new PlainStringValue(); if(_value != null){ @@ -874,17 +880,17 @@ public class SXSSFCell implements Cell { _value = sval; break; } - case CELL_TYPE_FORMULA: + case FORMULA: { _value=new NumericFormulaValue(); break; } - case CELL_TYPE_BLANK: + case BLANK: { _value=new BlankValue(); break; } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { BooleanValue bval = new BooleanValue(); if(_value != null){ @@ -895,7 +901,7 @@ public class SXSSFCell implements Cell { _value = bval; break; } - case CELL_TYPE_ERROR: + case ERROR: { _value=new ErrorValue(); break; @@ -906,26 +912,26 @@ public class SXSSFCell implements Cell { } } } - /*package*/ void setFormulaType(int type) + /*package*/ void setFormulaType(CellType type) { switch(type) { - case CELL_TYPE_NUMERIC: + case NUMERIC: { _value=new NumericFormulaValue(); break; } - case CELL_TYPE_STRING: + case STRING: { _value=new StringFormulaValue(); break; } - case CELL_TYPE_BOOLEAN: + case BOOLEAN: { _value=new BooleanFormulaValue(); break; } - case CELL_TYPE_ERROR: + case ERROR: { _value=new ErrorFormulaValue(); break; @@ -938,77 +944,64 @@ public class SXSSFCell implements Cell { } //TODO: implement this correctly @NotImplemented - /*package*/ int computeTypeFromFormula(String formula) + /*package*/ CellType computeTypeFromFormula(String formula) { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } //COPIED FROM https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java since the functions are declared private there /** * 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 expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) { + String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode + + " " + (isFormulaCell ? "formula " : "") + "cell"; return new IllegalStateException(msg); } -/** - * 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 boolean convertCellValueToBoolean() { - int cellType = getCellType(); + CellType cellType = getCellType(); - if (cellType == CELL_TYPE_FORMULA) { + if (cellType == CellType.FORMULA) { cellType = getCachedFormulaResultType(); } switch (cellType) { - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return getBooleanCellValue(); - case CELL_TYPE_STRING: + case STRING: String text = getStringCellValue(); return Boolean.parseBoolean(text); - case CELL_TYPE_NUMERIC: + case NUMERIC: return getNumericCellValue() != 0; - case CELL_TYPE_ERROR: - case CELL_TYPE_BLANK: + case ERROR: + 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(); return convertCellValueToString(cellType); } - private String convertCellValueToString(int cellType) { + private String convertCellValueToString(CellType cellType) { switch (cellType) { - case CELL_TYPE_BLANK: + case BLANK: return ""; - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return getBooleanCellValue() ? "TRUE" : "FALSE"; - case CELL_TYPE_STRING: + case STRING: return getStringCellValue(); - case CELL_TYPE_NUMERIC: - return Double.toString( getNumericCellValue() ); - case CELL_TYPE_ERROR: + case NUMERIC: + return Double.toString( getNumericCellValue() ); + case ERROR: byte errVal = getErrorCellValue(); return FormulaError.forInt(errVal).getString(); - case CELL_TYPE_FORMULA: + case FORMULA: if (_value != null) { FormulaValue fv = (FormulaValue)_value; - if (fv.getFormulaType() != CELL_TYPE_FORMULA) { + if (fv.getFormulaType() != CellType.FORMULA) { return convertCellValueToString(fv.getFormulaType()); } } @@ -1066,14 +1059,14 @@ public class SXSSFCell implements Cell { } interface Value { - int getType(); + CellType getType(); } static class NumericValue implements Value { double _value; - public int getType() + public CellType getType() { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } void setValue(double value) { @@ -1086,11 +1079,11 @@ public class SXSSFCell implements Cell { } static abstract class StringValue implements Value { - public int getType() + public CellType getType() { - return CELL_TYPE_STRING; + return CellType.STRING; } -//We cannot introduce a new type CELL_TYPE_RICH_TEXT because the types are public so we have to make rich text as a type of string +//We cannot introduce a new type CellType.RICH_TEXT because the types are public so we have to make rich text as a type of string abstract boolean isRichText(); // using the POI style which seems to avoid "instanceof". } static class PlainStringValue extends StringValue @@ -1114,9 +1107,9 @@ public class SXSSFCell implements Cell { { RichTextString _value; @Override - public int getType() + public CellType getType() { - return CELL_TYPE_STRING; + return CellType.STRING; } void setValue(RichTextString value) { @@ -1135,9 +1128,9 @@ public class SXSSFCell implements Cell { static abstract class FormulaValue implements Value { String _value; - public int getType() + public CellType getType() { - return CELL_TYPE_FORMULA; + return CellType.FORMULA; } void setValue(String value) { @@ -1147,15 +1140,15 @@ public class SXSSFCell implements Cell { { return _value; } - abstract int getFormulaType(); + abstract CellType getFormulaType(); } static class NumericFormulaValue extends FormulaValue { double _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_NUMERIC; + return CellType.NUMERIC; } void setPreEvaluatedValue(double value) { @@ -1170,9 +1163,9 @@ public class SXSSFCell implements Cell { { String _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_STRING; + return CellType.STRING; } void setPreEvaluatedValue(String value) { @@ -1187,9 +1180,9 @@ public class SXSSFCell implements Cell { { boolean _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_BOOLEAN; + return CellType.BOOLEAN; } void setPreEvaluatedValue(boolean value) { @@ -1204,9 +1197,9 @@ public class SXSSFCell implements Cell { { byte _preEvaluatedValue; @Override - int getFormulaType() + CellType getFormulaType() { - return CELL_TYPE_ERROR; + return CellType.ERROR; } void setPreEvaluatedValue(byte value) { @@ -1219,17 +1212,17 @@ public class SXSSFCell implements Cell { } static class BlankValue implements Value { - public int getType() + public CellType getType() { - return CELL_TYPE_BLANK; + return CellType.BLANK; } } static class BooleanValue implements Value { boolean _value; - public int getType() + public CellType getType() { - return CELL_TYPE_BOOLEAN; + return CellType.BOOLEAN; } void setValue(boolean value) { @@ -1243,9 +1236,9 @@ public class SXSSFCell implements Cell { static class ErrorValue implements Value { byte _value; - public int getType() + public CellType getType() { - return CELL_TYPE_ERROR; + return CellType.ERROR; } void setValue(byte value) { diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java index b7d3d830b4..e5e18ceac0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFEvaluationCell.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.streaming; import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationSheet; +import org.apache.poi.ss.usermodel.CellType; /** * SXSSF wrapper for a cell under evaluation @@ -36,6 +37,7 @@ final class SXSSFEvaluationCell implements EvaluationCell { this(cell, new SXSSFEvaluationSheet(cell.getSheet())); } + @Override public Object getIdentityKey() { // save memory by just using the cell itself as the identity key // Note - this assumes SXSSFCell has not overridden hashCode and equals @@ -45,31 +47,40 @@ final class SXSSFEvaluationCell implements EvaluationCell { public SXSSFCell getSXSSFCell() { 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/streaming/SXSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java index cec3136f61..a1eb965663 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java @@ -26,6 +26,7 @@ import java.util.TreeMap; 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.ss.usermodel.Sheet; import org.apache.poi.util.Internal; @@ -119,7 +120,7 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> /** * Use this to create new cells within the row and return it. * <p> - * 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 <code>setCellValue</code> or <code>setCellType</code>. * * @param column - the column number this cell represents @@ -130,23 +131,40 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> @Override public SXSSFCell createCell(int column) { - return createCell(column, Cell.CELL_TYPE_BLANK); + return createCell(column, CellType.BLANK); } /** * Use this to create new cells within the row and return it. * <p> - * 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. + * <p> + * 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) + */ + @Override + public SXSSFCell createCell(int column, CellType type) + { checkBounds(column); SXSSFCell cell = new SXSSFCell(this, type); _cells.put(column, cell); @@ -250,10 +268,10 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow> case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: - boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK); + boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: - return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java index 4782ca2fd8..455254aa6b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java @@ -31,6 +31,7 @@ import java.util.Iterator; 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.FormulaError;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.POILogFactory;
@@ -201,19 +202,19 @@ public class SheetDataWriter { // APIs
_out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
}
- int cellType = cell.getCellType();
+ CellType cellType = cell.getCellType();
switch (cellType) {
- case Cell.CELL_TYPE_BLANK: {
+ case BLANK: {
_out.write(">");
break;
}
- case Cell.CELL_TYPE_FORMULA: {
+ case FORMULA: {
_out.write(">");
_out.write("<f>");
outputQuotedString(cell.getCellFormula());
_out.write("</f>");
switch (cell.getCachedFormulaResultType()) {
- case Cell.CELL_TYPE_NUMERIC:
+ case NUMERIC:
double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) {
_out.write("<v>" + nval + "</v>");
@@ -224,7 +225,7 @@ public class SheetDataWriter { }
break;
}
- case Cell.CELL_TYPE_STRING: {
+ case STRING: {
if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
@@ -245,17 +246,17 @@ public class SheetDataWriter { }
break;
}
- case Cell.CELL_TYPE_NUMERIC: {
+ case NUMERIC: {
_out.write(" t=\"n\">");
_out.write("<v>" + cell.getNumericCellValue() + "</v>");
break;
}
- case Cell.CELL_TYPE_BOOLEAN: {
+ case BOOLEAN: {
_out.write(" t=\"b\">");
_out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
break;
}
- case Cell.CELL_TYPE_ERROR: {
+ case ERROR: {
FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write(" t=\"e\">");
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java index 817a9b5a82..81359d3857 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java @@ -29,6 +29,7 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; @@ -75,20 +76,21 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work } switch (cell.getCellType()) { - case XSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: return CellValue.valueOf(cell.getBooleanCellValue()); - case XSSFCell.CELL_TYPE_ERROR: + case ERROR: return CellValue.getError(cell.getErrorCellValue()); - case XSSFCell.CELL_TYPE_FORMULA: + case FORMULA: return evaluateFormulaCellValue(cell); - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: return new CellValue(cell.getNumericCellValue()); - case XSSFCell.CELL_TYPE_STRING: + case STRING: return new CellValue(cell.getRichStringCellValue().getString()); - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: return null; + default: + throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); } - throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); } @@ -110,9 +112,9 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work * @param cell The cell to evaluate * @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however) */ - public int evaluateFormulaCell(Cell cell) { - if (cell == null || cell.getCellType() != XSSFCell.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 @@ -129,47 +131,52 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work */ protected void doEvaluateInCell(Cell cell) { if (cell == null) return; - if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { + if (cell.getCellType() == CellType.FORMULA) { CellValue cv = evaluateFormulaCellValue(cell); setCellType(cell, cv); // cell will no longer be a formula cell setCellValue(cell, cv); } } private static void setCellType(Cell cell, CellValue cv) { - int cellType = cv.getCellType(); + CellType cellType = cv.getCellType(); switch (cellType) { - case XSSFCell.CELL_TYPE_BOOLEAN: - case XSSFCell.CELL_TYPE_ERROR: - case XSSFCell.CELL_TYPE_NUMERIC: - case XSSFCell.CELL_TYPE_STRING: + case BOOLEAN: + case ERROR: + case NUMERIC: + case STRING: cell.setCellType(cellType); return; - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: // never happens - blanks eventually get translated to zero - case XSSFCell.CELL_TYPE_FORMULA: + throw new IllegalArgumentException("This should never happen. Blanks eventually get translated to zero."); + case FORMULA: // this will never happen, we have already evaluated the formula + throw new IllegalArgumentException("This should never happen. Formulas should have already been evaluated."); + 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 XSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: cell.setCellValue(cv.getBooleanValue()); break; - case XSSFCell.CELL_TYPE_ERROR: + case ERROR: cell.setCellErrorValue(cv.getErrorValue()); break; - case XSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: cell.setCellValue(cv.getNumberValue()); break; - case XSSFCell.CELL_TYPE_STRING: + case STRING: cell.setCellValue(new XSSFRichTextString(cv.getStringValue())); break; - case XSSFCell.CELL_TYPE_BLANK: + case BLANK: // never happens - blanks eventually get translated to zero - case XSSFCell.CELL_TYPE_FORMULA: + case FORMULA: // this will never happen, we have already evaluated the formula default: throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 37adc7ead4..25d8cc8324 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -32,6 +32,7 @@ import org.apache.poi.ss.formula.ptg.Ptg; 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.Comment; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DateUtil; @@ -70,6 +71,8 @@ public final class XSSFCell implements Cell { private static final String FALSE_AS_STRING = "0"; private static final String TRUE_AS_STRING = "1"; + private static final String FALSE = "FALSE"; + private static final String TRUE = "TRUE"; /** * the xml bean containing information about the cell's location, value, @@ -135,23 +138,14 @@ public final class XSSFCell implements Cell { // Copy cell value (cell type is updated implicitly) if (policy.isCopyCellValue()) { if (srcCell != null) { - int copyCellType = srcCell.getCellType(); - if (copyCellType == Cell.CELL_TYPE_FORMULA && !policy.isCopyCellFormula()) { + CellType copyCellType = srcCell.getCellType(); + if (copyCellType == CellType.FORMULA && !policy.isCopyCellFormula()) { // Copy formula result as value // FIXME: Cached value may be stale copyCellType = srcCell.getCachedFormulaResultType(); } switch (copyCellType) { - case Cell.CELL_TYPE_BOOLEAN: - setCellValue(srcCell.getBooleanCellValue()); - break; - case Cell.CELL_TYPE_ERROR: - setCellErrorValue(srcCell.getErrorCellValue()); - break; - case Cell.CELL_TYPE_FORMULA: - setCellFormula(srcCell.getCellFormula()); - break; - case Cell.CELL_TYPE_NUMERIC: + case NUMERIC: // DataFormat is not copied unless policy.isCopyCellStyle is true if (DateUtil.isCellDateFormatted(srcCell)) { setCellValue(srcCell.getDateCellValue()); @@ -160,12 +154,22 @@ public final class XSSFCell implements Cell { setCellValue(srcCell.getNumericCellValue()); } break; - case Cell.CELL_TYPE_STRING: + case STRING: setCellValue(srcCell.getStringCellValue()); break; - case Cell.CELL_TYPE_BLANK: + case FORMULA: + setCellFormula(srcCell.getCellFormula()); + break; + case BLANK: setBlank(); break; + case BOOLEAN: + setCellValue(srcCell.getBooleanCellValue()); + break; + case ERROR: + setCellErrorValue(srcCell.getErrorCellValue()); + break; + default: throw new IllegalArgumentException("Invalid cell type " + srcCell.getCellType()); } @@ -246,21 +250,21 @@ public final class XSSFCell implements Cell { * </p> * @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} */ @Override public boolean getBooleanCellValue() { - int cellType = getCellType(); + CellType cellType = getCellType(); switch(cellType) { - case CELL_TYPE_BLANK: + case BLANK: return false; - case CELL_TYPE_BOOLEAN: + case BOOLEAN: return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); - case CELL_TYPE_FORMULA: + case FORMULA: //YK: should throw an exception if requesting boolean value from a non-boolean formula return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); default: - throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); + throw typeMismatch(CellType.BOOLEAN, cellType, false); } } @@ -284,31 +288,32 @@ public final class XSSFCell implements Cell { * For formulas or error cells we return the precalculated value; * </p> * @return the value of the cell as a number - * @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 <code>double</code>. * @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, <code>SUM(C4:E4)</code> * * @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. * </p> * @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 <code>double</code>. * @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 { * </p> * * @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 9fd3d9edd6..40b44001ab 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 c4e63c70f9..8196abfb45 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<XSSFRow> { /** * Use this to create new cells within the row and return it. * <p> - * 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 <code>setCellValue</code> or <code>setCellType</code>. * </p> * @param columnIndex - the column number this cell represents @@ -194,7 +195,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { */ @Override public XSSFCell createCell(int columnIndex) { - return createCell(columnIndex, Cell.CELL_TYPE_BLANK); + return createCell(columnIndex, CellType.BLANK); } /** @@ -205,15 +206,29 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { * @return XSSFCell a high level representation of the created cell. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx) - * @see Cell#CELL_TYPE_BLANK - * @see Cell#CELL_TYPE_BOOLEAN - * @see Cell#CELL_TYPE_ERROR - * @see Cell#CELL_TYPE_FORMULA - * @see Cell#CELL_TYPE_NUMERIC - * @see Cell#CELL_TYPE_STRING + * @see CellType#BLANK + * @see CellType#BOOLEAN + * @see CellType#ERROR + * @see CellType#FORMULA + * @see CellType#NUMERIC + * @see CellType#STRING + * @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead. */ @Override public XSSFCell createCell(int columnIndex, int type) { + return createCell(columnIndex, CellType.forInt(type)); + } + /** + * Use this to create new cells within the row and return it. + * + * @param columnIndex - the column number this cell represents + * @param type - the cell's data type + * @return XSSFCell a high level representation of the created cell. + * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 + * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx) + */ + @Override + public XSSFCell createCell(int columnIndex, CellType type) { // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory final Integer colI = new Integer(columnIndex); // NOSONAR CTCell ctCell; @@ -226,8 +241,8 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { } XSSFCell xcell = new XSSFCell(this, ctCell); xcell.setCellNum(columnIndex); - if (type != Cell.CELL_TYPE_BLANK) { - xcell.setCellType(type); + if (type != CellType.BLANK) { + xcell.setCellType(type); } _cells.put(colI, xcell); return xcell; @@ -261,10 +276,10 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: - boolean isBlank = (cell != null && cell.getCellType() == Cell.CELL_TYPE_BLANK); + boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: - return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } @@ -481,7 +496,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { if(xcell.isPartOfArrayFormulaGroup()) { xcell.notifyArrayFormulaChanging(); } - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(cell.getCellType() == CellType.FORMULA) { _sheet.getWorkbook().onDeleteFormula(xcell); } // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory diff --git a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java index 7d19525773..ddb3e78b2e 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java +++ b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java @@ -58,13 +58,14 @@ public class TestCellFormatPart extends CellFormatTestBase { public void testGeneralFormat() throws Exception { runFormatTests("GeneralFormatTests.xlsx", new CellValue() { public Object getValue(Cell cell) { - int type = CellFormat.ultimateType(cell); - if (type == Cell.CELL_TYPE_BOOLEAN) - return cell.getBooleanCellValue(); - else if (type == Cell.CELL_TYPE_NUMERIC) - return cell.getNumericCellValue(); - else - return cell.getStringCellValue(); + switch (CellFormat.ultimateType(cell)) { + case BOOLEAN: + return cell.getBooleanCellValue(); + case NUMERIC: + return cell.getNumericCellValue(); + default: + return cell.getStringCellValue(); + } } }); } @@ -125,10 +126,12 @@ public class TestCellFormatPart extends CellFormatTestBase { public void testTextFormat() throws Exception { runFormatTests("TextFormatTests.xlsx", new CellValue() { public Object getValue(Cell cell) { - if (CellFormat.ultimateType(cell) == Cell.CELL_TYPE_BOOLEAN) - return cell.getBooleanCellValue(); - else - return cell.getStringCellValue(); + switch(CellFormat.ultimateType(cell)) { + case BOOLEAN: + return cell.getBooleanCellValue(); + default: + return cell.getStringCellValue(); + } } }); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java index 55b0725a31..23f4ae3912 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java @@ -35,6 +35,7 @@ import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.formula.eval.TestFormulasFromSpreadsheet; 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.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; @@ -189,7 +190,7 @@ public final class TestFormulaEvaluatorOnXSSF { for (short colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) { Cell c = formulasRow.getCell(colnum); assumeNotNull(c); - assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); + assumeTrue(c.getCellType() == CellType.FORMULA); ignoredFormulaTestCase(c.getCellFormula()); CellValue actValue = evaluator.evaluate(c); @@ -201,33 +202,36 @@ public final class TestFormulaEvaluatorOnXSSF { 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 expectedCellType = expValue.getCellType(); + switch (expectedCellType) { + 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()); // if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values // assertEquals(msg, expValue.getErrorCellValue(), 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); // double delta = Math.abs(expValue.getNumericCellValue()-actValue.getNumberValue()); // double pctExpValue = Math.abs(0.00001*expValue.getNumericCellValue()); // assertTrue(msg, delta <= pctExpValue); 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: " + expectedCellType); } } } @@ -260,10 +264,10 @@ public final class TestFormulaEvaluatorOnXSSF { logger.log(POILogger.WARN, "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/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java index 95fe30803e..a237cefe60 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java @@ -34,6 +34,7 @@ import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.formula.eval.TestFormulasFromSpreadsheet; 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.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; @@ -175,7 +176,7 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF { Cell c = r.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE); assumeNotNull(c); - assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); + assumeTrue(c.getCellType() == CellType.FORMULA); CellValue actValue = evaluator.evaluate(c); @@ -184,33 +185,36 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF { 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 expectedCellType = expValue.getCellType(); + switch (expectedCellType) { + 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()); // if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values // assertEquals(msg, expected.getErrorCellValue(), 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 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); // double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue()); // double pctExpected = Math.abs(0.00001*expected.getNumericCellValue()); // assertTrue(msg, delta <= pctExpected); 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: " + expectedCellType); } } @@ -227,10 +231,10 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF { logger.log(POILogger.WARN, "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(); } @@ -251,10 +255,10 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF { logger.log(POILogger.WARN, "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/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 41adc133a8..02253a64eb 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -67,29 +67,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.Function; -import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.CellValue; -import org.apache.poi.ss.usermodel.ClientAnchor; -import org.apache.poi.ss.usermodel.Comment; -import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.DataFormat; -import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.Drawing; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.FormulaError; -import org.apache.poi.ss.usermodel.FormulaEvaluator; -import org.apache.poi.ss.usermodel.Hyperlink; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.Name; -import org.apache.poi.ss.usermodel.PrintSetup; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.SheetConditionalFormatting; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; @@ -322,10 +300,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Sheet s = wb.getSheetAt(i); for(Row r : s) { for(Cell c : r) { - if(c.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(c.getCellType() == CellType.FORMULA) { CellValue cv = eval.evaluate(c); - if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) { + if(cv.getCellType() == CellType.NUMERIC) { // assert that the calculated value agrees with // the cached formula result calculated by Excel String formula = c.getCellFormula(); @@ -446,7 +424,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { cell = sheet.getRow(0).getCell(0); assertEquals("#REF!*#REF!", cell.getCellFormula()); - assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType()); + assertEquals(CellType.ERROR, evaluator.evaluateInCell(cell).getCellType()); assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); Name nm1 = wb.getName("sale_1"); @@ -458,7 +436,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { cell = sheet.getRow(1).getCell(0); assertEquals("sale_1*sale_2", cell.getCellFormula()); - assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType()); + assertEquals(CellType.ERROR, evaluator.evaluateInCell(cell).getCellType()); assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); wb.close(); @@ -606,10 +584,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Otherwise should go sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay sheet.getRow(2).getCell(0).setCellFormula(null); // go - sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay + sheet.getRow(3).getCell(0).setCellType(CellType.FORMULA); // stay XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); - sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go + sheet.getRow(4).getCell(0).setCellType(CellType.STRING); // go XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); validateCells(sheet); @@ -617,7 +595,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { validateCells(sheet); XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); - sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go + sheet.getRow(6).getCell(0).setCellType(CellType.BLANK); // go XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); sheet.getRow(7).getCell(0).setCellValue((String) null); // go @@ -664,7 +642,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Sheet sheet = wb.getSheetAt(0); for(Row row : sheet){ for(Cell cell : row){ - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){ + if(cell.getCellType() == CellType.FORMULA){ formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells } } @@ -1718,7 +1696,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Get wrong cell by row 8 & column 7 Cell cell = sheet.getRow(8).getCell(7); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); // Check the value - will be zero as it is <c><v/></c> assertEquals(0.0, cell.getNumericCellValue(), 0.001); @@ -2204,11 +2182,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Sheet sheet = wb.getSheet("Sheet1"); Cell cell = sheet.getRow(5).getCell(4); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("E4+E5", cell.getCellFormula()); CellValue value = evaluator.evaluate(cell); - assertEquals(Cell.CELL_TYPE_ERROR, value.getCellType()); + assertEquals(CellType.ERROR, value.getCellType()); assertEquals(-60, value.getErrorValue()); assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString()); assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString()); @@ -2563,7 +2541,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { if(cell == null){ cell = row.createCell(cellnum); } else { - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(cell.getCellType() == CellType.FORMULA) { cell.setCellFormula(null); cell.getCellStyle().setDataFormat((short) 0); } @@ -2629,13 +2607,13 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) { - assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType()); + assertEquals(CellType.FORMULA, intF.getCellType()); if (null == expectedResultOrNull) { - assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType()); + assertEquals(CellType.ERROR, intF.getCachedFormulaResultType()); expectedResultOrNull = "#VALUE!"; } else { - assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); + assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType()); } assertEquals(expectedFormula, intF.getCellFormula()); @@ -2676,7 +2654,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { Sheet sheet = wb.getSheet("Sheet1"); for(Row aRow : sheet) { Cell cell = aRow.getCell(1); - if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + if(cell.getCellType() == CellType.FORMULA) { String formula = cell.getCellFormula(); //System.out.println("formula: " + formula); assertNotNull(formula); @@ -2980,16 +2958,16 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { row = worksheet.getRow(2); cell = row.getCell(1); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); - assertEquals(-1, evaluator.evaluateFormulaCell(cell)); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals(CellType._UNINITIALIZED, evaluator.evaluateFormulaCell(cell)); // A3 row = worksheet.getRow(2); cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("IF(ISBLANK(B3),\"\",B3)", cell.getCellFormula()); - assertEquals(Cell.CELL_TYPE_STRING, evaluator.evaluateFormulaCell(cell)); + assertEquals(CellType.STRING, evaluator.evaluateFormulaCell(cell)); CellValue value = evaluator.evaluate(cell); assertEquals("", value.getStringValue()); @@ -2997,9 +2975,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { row = worksheet.getRow(4); cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula()); - assertEquals(Cell.CELL_TYPE_NUMERIC, evaluator.evaluateFormulaCell(cell)); + assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCell(cell)); value = evaluator.evaluate(cell); assertEquals(1.0, value.getNumberValue(), 0.1); diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java index fd4c3dc03d..62f3d14231 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java @@ -123,31 +123,31 @@ public abstract class AbstractExcelConverter final String value; switch ( cell.getCellType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: switch ( cell.getCachedFormulaResultType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if ( str == null || str.length() <= 0 ) return false; value = str.toString(); break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: HSSFCellStyle style = cell.getCellStyle(); double nval = cell.getNumericCellValue(); short df = style.getDataFormat(); String dfs = style.getDataFormatString(); value = _formatter.formatRawCellContents(nval, df, dfs); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = String.valueOf( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: @@ -155,16 +155,16 @@ public abstract class AbstractExcelConverter break; } break; - case HSSFCell.CELL_TYPE_BLANK: + case BLANK: value = ExcelToHtmlUtils.EMPTY; break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: value = _formatter.formatCellValue( cell ); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = String.valueOf( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java index 6000485122..7119718964 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java @@ -209,14 +209,14 @@ public class ExcelToFoConverter extends AbstractExcelConverter String value; switch ( cell.getCellType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: switch ( cell.getCachedFormulaResultType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if ( str != null && str.length() > 0 ) { @@ -227,16 +227,16 @@ public class ExcelToFoConverter extends AbstractExcelConverter value = ExcelToHtmlUtils.EMPTY; } break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: double nValue = cell.getNumericCellValue(); short df = cellStyle.getDataFormat(); String dfs = cellStyle.getDataFormatString(); value = _formatter.formatRawCellContents(nValue, df, dfs ); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = Boolean.toString( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: @@ -248,16 +248,16 @@ public class ExcelToFoConverter extends AbstractExcelConverter break; } break; - case HSSFCell.CELL_TYPE_BLANK: + case BLANK: value = ExcelToHtmlUtils.EMPTY; break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: value = _formatter.formatCellValue( cell ); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = Boolean.toString( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java index 222e362c3c..b34b2ed8e3 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java @@ -296,14 +296,14 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter String value; switch ( cell.getCellType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; - case HSSFCell.CELL_TYPE_FORMULA: + case FORMULA: switch ( cell.getCachedFormulaResultType() ) { - case HSSFCell.CELL_TYPE_STRING: + case STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if ( str != null && str.length() > 0 ) { @@ -314,16 +314,16 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter value = ExcelToHtmlUtils.EMPTY; } break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: double nValue = cell.getNumericCellValue(); short df = cellStyle.getDataFormat(); String dfs = cellStyle.getDataFormatString(); value = _formatter.formatRawCellContents(nValue, df, dfs); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = String.valueOf( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: @@ -335,16 +335,16 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter break; } break; - case HSSFCell.CELL_TYPE_BLANK: + case BLANK: value = ExcelToHtmlUtils.EMPTY; break; - case HSSFCell.CELL_TYPE_NUMERIC: + case NUMERIC: value = _formatter.formatCellValue( cell ); break; - case HSSFCell.CELL_TYPE_BOOLEAN: + case BOOLEAN: value = String.valueOf( cell.getBooleanCellValue() ); break; - case HSSFCell.CELL_TYPE_ERROR: + case ERROR: value = ErrorEval.getText( cell.getErrorCellValue() ); break; default: diff --git a/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java b/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java index 82fc4d6927..2f6a14b769 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java @@ -17,12 +17,12 @@ package org.apache.poi.hssf.record; -import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.formula.ptg.AttrPtg; import org.apache.poi.ss.formula.ptg.FuncVarPtg; import org.apache.poi.ss.formula.ptg.IntPtg; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.RefPtg; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FormulaError; import junit.framework.AssertionFailedError; @@ -83,7 +83,7 @@ public final class TestFormulaRecord extends TestCase { FormulaRecord record = new FormulaRecord(TestcaseRecordInputStream.create(FormulaRecord.sid, formulaByte)); assertEquals("Row", 0, record.getRow()); assertEquals("Column", 0, record.getColumn()); - assertEquals(HSSFCell.CELL_TYPE_ERROR, record.getCachedResultType()); + assertEquals(CellType.ERROR.getCode(), record.getCachedResultType()); byte[] output = record.serialize(); assertEquals("Output size", 33, output.length); //includes sid+recordlength diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index a65721151b..d588115b65 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -72,6 +72,7 @@ import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; import org.apache.poi.ss.usermodel.BorderStyle; 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.ClientAnchor.AnchorType; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.FormulaEvaluator; @@ -173,7 +174,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { cell = row.createCell(3); // Write test - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); setCellText(cell, "a test"); // change existing numeric cell value @@ -468,7 +469,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFRow row = sheet.getRow(i); if (row != null) { HSSFCell cell = row .getCell(0); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals(CellType.STRING, cell.getCellType()); count++; } } @@ -1111,7 +1112,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { c3.getNumericCellValue(); fail("exception should have been thrown"); } catch (IllegalStateException e) { - assertEquals("Cannot get a numeric value from a text formula cell", e.getMessage()); + assertEquals("Cannot get a NUMERIC value from a STRING formula cell", e.getMessage()); } @@ -1166,13 +1167,13 @@ public final class TestBugs extends BaseTestBugzillaIssues { } private static void confirmCachedValue(double expectedValue, HSSFCell cell) { - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType()); + assertEquals(CellType.FORMULA, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType()); assertEquals(expectedValue, cell.getNumericCellValue(), 0.0); } private static void confirmCachedValue(String expectedValue, HSSFCell cell) { - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCachedFormulaResultType()); + assertEquals(CellType.FORMULA, cell.getCellType()); + assertEquals(CellType.STRING, cell.getCachedFormulaResultType()); assertEquals(expectedValue, cell.getRichStringCellValue().getString()); } @@ -1287,7 +1288,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { s = wb.getSheet("OneVariable Table Completed"); r = s.getRow(3); c = r.getCell(4); - assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); + assertEquals(CellType.FORMULA, c.getCellType()); // TODO - check the formula once tables and // arrays are properly supported @@ -1297,7 +1298,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { s = wb.getSheet("TwoVariable Table Example"); r = s.getRow(3); c = r.getCell(4); - assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); + assertEquals(CellType.FORMULA, c.getCellType()); // TODO - check the formula once tables and // arrays are properly supported @@ -1823,26 +1824,26 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFRow row; row = s.getRow(0); - assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); + assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(1); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(2); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("Sheet1!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(3); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(4); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); @@ -1852,7 +1853,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { // Add 5 row = s.createRow(5); - row.createCell(1, Cell.CELL_TYPE_FORMULA); + row.createCell(1, CellType.FORMULA); row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1"); row.getCell(1).setCellValue(234.0); @@ -1863,32 +1864,32 @@ public final class TestBugs extends BaseTestBugzillaIssues { s = wb2.getSheetAt(0); row = s.getRow(0); - assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); + assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); assertEquals(112.0, row.getCell(1).getNumericCellValue(),0); row = s.getRow(1); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(2); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("Sheet1!B1", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(3); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula()); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); // TODO - Fix these so they work... /*row = s.getRow(4); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula()); assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0); row = s.getRow(5); - assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType()); + assertEquals(CellType.FORMULA, row.getCell(1).getCellType()); assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula()); assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/ @@ -2087,13 +2088,13 @@ public final class TestBugs extends BaseTestBugzillaIssues { HSSFWorkbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Bug50416"); Row row1 = sheet.createRow(0); - Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING); + Cell cellA_1 = row1.createCell(0,CellType.STRING); cellA_1.setCellValue("Cell A,1"); Row row2 = sheet.createRow(1); - Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING); + Cell cellA_2 = row2.createCell(0,CellType.STRING); cellA_2.setCellValue("Cell A,2"); Row row3 = sheet.createRow(2); - Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING); + Cell cellA_3 = row3.createCell(0,CellType.STRING); cellA_3.setCellValue("Cell A,3"); // Test the last Row number it currently correct @@ -2541,7 +2542,7 @@ public final class TestBugs extends BaseTestBugzillaIssues { row.createCell(2).setCellValue(cal); row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3)); row.createCell(4).setCellValue(true); - row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR); + row.createCell(5).setCellType(CellType.ERROR); row.createCell(6).setCellValue("added cells."); } @@ -2754,13 +2755,13 @@ public final class TestBugs extends BaseTestBugzillaIssues { } private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) { - assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType()); + assertEquals(CellType.FORMULA, intF.getCellType()); if (null == expectedResultOrNull) { - assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType()); + assertEquals(CellType.ERROR, intF.getCachedFormulaResultType()); expectedResultOrNull = "#VALUE!"; } else { - assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); + assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType()); } assertEquals(expectedFormula, intF.getCellFormula()); @@ -2986,12 +2987,12 @@ public final class TestBugs extends BaseTestBugzillaIssues { Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(0); Cell cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula()); assertEquals("", cell.getStringCellValue()); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(CellType.BLANK, cell.getCellType()); try { assertNull(cell.getCellFormula()); fail("Should throw an exception here"); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 1ac9e5d112..ed27a21f9e 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -36,6 +36,7 @@ import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.StringRecord; import org.apache.poi.ss.usermodel.BaseTestCell; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; @@ -417,14 +418,7 @@ public final class TestHSSFCell extends BaseTestCell { cell.getCachedFormulaResultType(); fail("Should catch exception"); } catch (IllegalStateException e) { - // expected here - } - - try { - assertNotNull(new HSSFCell(wb, sheet, 0, (short)0, Cell.CELL_TYPE_ERROR+1 )); - fail("Should catch exception"); - } catch (RuntimeException e) { - // expected here + // expected here } cell.removeCellComment(); @@ -440,36 +434,36 @@ public final class TestHSSFCell extends BaseTestCell { Row row = sheet.createRow(0); Cell cell = row.createCell(0); - cell.setCellType(Cell.CELL_TYPE_BLANK); + cell.setCellType(CellType.BLANK); assertNull(null, cell.getDateCellValue()); assertFalse(cell.getBooleanCellValue()); assertEquals("", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); assertEquals("", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); cell.setCellValue(1.2); - cell.setCellType(Cell.CELL_TYPE_NUMERIC); + cell.setCellType(CellType.NUMERIC); assertEquals("1.2", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellType(CellType.BOOLEAN); assertEquals("TRUE", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellType(CellType.BOOLEAN); cell.setCellValue("" + FormulaError.VALUE.name()); - cell.setCellType(Cell.CELL_TYPE_ERROR); + cell.setCellType(CellType.ERROR); assertEquals("#VALUE!", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_ERROR); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellType(CellType.ERROR); + cell.setCellType(CellType.BOOLEAN); assertEquals("FALSE", cell.toString()); cell.setCellValue(1.2); - cell.setCellType(Cell.CELL_TYPE_NUMERIC); + cell.setCellType(CellType.NUMERIC); assertEquals("1.2", cell.toString()); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); - cell.setCellType(Cell.CELL_TYPE_STRING); - cell.setCellType(Cell.CELL_TYPE_ERROR); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.BOOLEAN); + cell.setCellType(CellType.STRING); + cell.setCellType(CellType.ERROR); + cell.setCellType(CellType.STRING); cell.setCellValue(1.2); - cell.setCellType(Cell.CELL_TYPE_NUMERIC); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.NUMERIC); + cell.setCellType(CellType.STRING); assertEquals("1.2", cell.toString()); cell.setCellValue((String)null); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index 64bfb23701..405347a9bc 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -30,7 +30,7 @@ import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; -import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -384,16 +384,15 @@ public final class TestWorkbook { * OBJECTIVE: Test that HSSF can read a simple spreadsheet with and RKRecord and correctly * identify the cell as numeric and convert it to a NumberRecord. <P> * SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell. <P> - * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell<P> + * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly identifies the cell<P> */ @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 6afcc445e3..18f79f1a69 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 85ddfe01c3..8457a07d94 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 9e52a4efd7..bb9b6a85ba 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 e222220cef..4fed4d48f9 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 "<blank>";
- case HSSFCell.CELL_TYPE_BOOLEAN: return Boolean.toString(expecedCell.getBooleanCellValue());
- case HSSFCell.CELL_TYPE_NUMERIC: return Double.toString(expecedCell.getNumericCellValue());
- case HSSFCell.CELL_TYPE_STRING: return expecedCell.getRichStringCellValue().getString();
+ case BLANK: return "<blank>";
+ case BOOLEAN: return Boolean.toString(expecedCell.getBooleanCellValue());
+ case NUMERIC: return Double.toString(expecedCell.getNumericCellValue());
+ case STRING: return expecedCell.getRichStringCellValue().getString();
+ default: fail("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")");
}
- fail("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")");
return "";
}
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java index 4a1e02bc48..68f9873159 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -527,7 +527,7 @@ public abstract class BaseTestBugzillaIssues { for(int rn=0; rn<= topRow; rn++) { Row r = s.createRow(rn); for(int cn=0; cn<leftmostColumn; cn++) { - Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC); + Cell c = r.createCell(cn, CellType.NUMERIC); c.setCellValue(100*rn + cn); } } @@ -654,16 +654,16 @@ public abstract class BaseTestBugzillaIssues { Row r1 = s.createRow(0); for (int i=0; i<3; i++) { - r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0); + r1.createCell(i, CellType.NUMERIC).setCellValue(0); } for (int i=3; i<6; i++) { - r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1); + r1.createCell(i, CellType.NUMERIC).setCellValue(1); } for (int i=6; i<9; i++) { - r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0.12345); + r1.createCell(i, CellType.NUMERIC).setCellValue(0.12345); } for (int i=9; i<12; i++) { - r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1.2345); + r1.createCell(i, CellType.NUMERIC).setCellValue(1.2345); } for (int i=0; i<12; i+=3) { r1.getCell(i).setCellStyle(iPercent); @@ -714,11 +714,11 @@ public abstract class BaseTestBugzillaIssues { // C1 is a string, with different text r1.createCell(2).setCellValue("This some other text"); // D1 is a blank cell - r1.createCell(3, Cell.CELL_TYPE_BLANK); + r1.createCell(3, CellType.BLANK); // E1 is null // A2 will hold our test formulas - Cell cf = r2.createCell(0, Cell.CELL_TYPE_FORMULA); + Cell cf = r2.createCell(0, CellType.FORMULA); // First up, check that TRUE and ISLOGICAL both behave @@ -872,10 +872,10 @@ public abstract class BaseTestBugzillaIssues { // Create the references - Cell c1 = r1.createCell(0, Cell.CELL_TYPE_FORMULA); + Cell c1 = r1.createCell(0, CellType.FORMULA); c1.setCellFormula(refLocal); - Cell c2 = r1.createCell(1, Cell.CELL_TYPE_FORMULA); + Cell c2 = r1.createCell(1, CellType.FORMULA); c2.setCellFormula(refHttp); @@ -952,7 +952,7 @@ public abstract class BaseTestBugzillaIssues { assertEquals(1, cArray.length);*/ Cell cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); } { // overwrite the row @@ -1119,29 +1119,29 @@ public abstract class BaseTestBugzillaIssues { Row r = s.createRow(0); // Setup - Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC); + Cell cn = r.createCell(0, CellType.NUMERIC); cn.setCellValue(1.2); - Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING); + Cell cs = r.createCell(1, CellType.STRING); cs.setCellValue("Testing"); - Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA); + Cell cfn = r.createCell(2, CellType.FORMULA); cfn.setCellFormula("A1"); - Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA); + Cell cfs = r.createCell(3, CellType.FORMULA); cfs.setCellFormula("B1"); FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); - assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType()); + assertEquals(CellType.NUMERIC, fe.evaluate(cfn).getCellType()); + assertEquals(CellType.STRING, fe.evaluate(cfs).getCellType()); fe.evaluateFormulaCell(cfn); fe.evaluateFormulaCell(cfs); // Now test - assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType()); - assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType()); - assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType()); + assertEquals(CellType.NUMERIC, cn.getCellType()); + assertEquals(CellType.STRING, cs.getCellType()); + assertEquals(CellType.FORMULA, cfn.getCellType()); + assertEquals(CellType.NUMERIC, cfn.getCachedFormulaResultType()); + assertEquals(CellType.FORMULA, cfs.getCellType()); + assertEquals(CellType.STRING, cfs.getCachedFormulaResultType()); // Different ways of retrieving assertEquals(1.2, cn.getNumericCellValue(), 0); @@ -1195,7 +1195,7 @@ public abstract class BaseTestBugzillaIssues { cell = row.createCell(1); // also verify that setting formulas to null works - cell.setCellType(Cell.CELL_TYPE_FORMULA); + cell.setCellType(CellType.FORMULA); cell.setCellValue((String)null); wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); @@ -1205,7 +1205,7 @@ public abstract class BaseTestBugzillaIssues { value == null || value.length() == 0); // set some value - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); cell.setCellValue("somevalue"); value = cell.getStringCellValue(); @@ -1383,7 +1383,7 @@ public abstract class BaseTestBugzillaIssues { Sheet s = wb.createSheet(); Cell cell = s.createRow(0).createCell(0); cell.setCellValue((String)null); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(CellType.BLANK, cell.getCellType()); _testDataProvider.trackAllColumnsForAutosizing(s); @@ -1554,8 +1554,8 @@ public abstract class BaseTestBugzillaIssues { Cell cell = row.getCell(cellId); System.out.println("Formula:" + cell.getCellFormula()); - if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { - int formulaResultType = cell.getCachedFormulaResultType(); + if (CellType.FORMULA == cell.getCellType()) { + CellType formulaResultType = cell.getCachedFormulaResultType(); System.out.println("Formula Result Type:" + formulaResultType); } @@ -1569,8 +1569,8 @@ public abstract class BaseTestBugzillaIssues { cell = row.getCell(cellId); System.out.println("Formula:" + cell.getCellFormula()); - if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { - int formulaResultType = cell.getCachedFormulaResultType(); + if (CellType.FORMULA == cell.getCellType()) { + CellType formulaResultType = cell.getCachedFormulaResultType(); System.out.println("Formula Result Type:" + formulaResultType); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index d701d7c58b..a1c91481a5 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -60,76 +60,78 @@ public abstract class BaseTestCell { cell.setCellValue(1.2); assertEquals(1.2, cell.getNumericCellValue(), 0.0001); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING, + CellType.FORMULA, CellType.ERROR); cell.setCellValue(false); assertEquals(false, cell.getBooleanCellValue()); - assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); + assertEquals(CellType.BOOLEAN, cell.getCellType()); cell.setCellValue(true); assertEquals(true, cell.getBooleanCellValue()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING, + CellType.FORMULA, CellType.ERROR); cell.setCellValue(factory.createRichTextString("Foo")); assertEquals("Foo", cell.getRichStringCellValue().getString()); assertEquals("Foo", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertEquals(CellType.STRING, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN, + CellType.FORMULA, CellType.ERROR); cell.setCellValue("345"); assertEquals("345", cell.getRichStringCellValue().getString()); assertEquals("345", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertEquals(CellType.STRING, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN, + CellType.FORMULA, CellType.ERROR); Calendar c = LocaleUtil.getLocaleCalendar(); c.setTimeInMillis(123456789); cell.setCellValue(c.getTime()); assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING, + CellType.FORMULA, CellType.ERROR); cell.setCellValue(c); assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING, + CellType.FORMULA, CellType.ERROR); cell.setCellErrorValue(FormulaError.NA.getCode()); assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue()); - assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING); + assertEquals(CellType.ERROR, cell.getCellType()); + assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN, + CellType.FORMULA, CellType.STRING); book.close(); } - private static void assertProhibitedValueAccess(Cell cell, int ... types){ - for(int type : types){ + private static void assertProhibitedValueAccess(Cell cell, CellType ... types) { + for(CellType type : types){ try { switch (type) { - case Cell.CELL_TYPE_NUMERIC: + case NUMERIC: cell.getNumericCellValue(); break; - case Cell.CELL_TYPE_STRING: + case STRING: cell.getStringCellValue(); break; - case Cell.CELL_TYPE_BOOLEAN: + case BOOLEAN: cell.getBooleanCellValue(); break; - case Cell.CELL_TYPE_FORMULA: + case FORMULA: cell.getCellFormula(); break; - case Cell.CELL_TYPE_ERROR: + case ERROR: cell.getErrorCellValue(); break; + default: + fail("Should get exception when reading cell type (" + type + ")."); } - fail("Should get exception when reading cell type (" + type + ")."); + } catch (IllegalStateException e){ // expected during successful test assertTrue(e.getMessage().startsWith("Cannot get a")); @@ -174,13 +176,13 @@ public abstract class BaseTestCell { c = r.getCell(1); assertEquals(0, c.getRowIndex()); assertEquals(1, c.getColumnIndex()); - assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType()); + assertEquals(CellType.BOOLEAN, c.getCellType()); assertEquals("B1 value", true, c.getBooleanCellValue()); c = r.getCell(2); assertEquals(0, c.getRowIndex()); assertEquals(2, c.getColumnIndex()); - assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType()); + assertEquals(CellType.BOOLEAN, c.getCellType()); assertEquals("C1 value", false, c.getBooleanCellValue()); wb2.close(); @@ -224,13 +226,13 @@ public abstract class BaseTestCell { c = r.getCell(1); assertEquals(0, c.getRowIndex()); assertEquals(1, c.getColumnIndex()); - assertEquals(Cell.CELL_TYPE_ERROR, c.getCellType()); + assertEquals(CellType.ERROR, c.getCellType()); assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue()); c = r.getCell(2); assertEquals(0, c.getRowIndex()); assertEquals(2, c.getColumnIndex()); - assertEquals(Cell.CELL_TYPE_ERROR, c.getCellType()); + assertEquals(CellType.ERROR, c.getCellType()); assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue()); wb2.close(); @@ -270,7 +272,7 @@ public abstract class BaseTestCell { r = s.getRow(0); c = r.getCell(0); - assertEquals("Formula Cell at 0,0", Cell.CELL_TYPE_FORMULA, c.getCellType()); + assertEquals("Formula Cell at 0,0", CellType.FORMULA, c.getCellType()); cs = c.getCellStyle(); assertNotNull("Formula Cell Style", cs); @@ -345,25 +347,25 @@ public abstract class BaseTestCell { Cell c1 = r.createCell(0); c1.setCellFormula("NA()"); assertEquals(0.0, c1.getNumericCellValue(), 0.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); + assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType()); c1.setCellValue(10); assertEquals(10.0, c1.getNumericCellValue(), 0.0); - assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType()); - assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); + assertEquals(CellType.FORMULA, c1.getCellType()); + assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType()); Cell c2 = r.createCell(1); c2.setCellFormula("NA()"); assertEquals(0.0, c2.getNumericCellValue(), 0.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType()); + assertEquals(CellType.NUMERIC, c2.getCachedFormulaResultType()); c2.setCellValue("I changed!"); assertEquals("I changed!", c2.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType()); + assertEquals(CellType.FORMULA, c2.getCellType()); + assertEquals(CellType.STRING, c2.getCachedFormulaResultType()); //calglin Cell.setCellFormula(null) for a non-formula cell Cell c3 = r.createCell(2); c3.setCellFormula(null); - assertEquals(Cell.CELL_TYPE_BLANK, c3.getCellType()); + assertEquals(CellType.BLANK, c3.getCellType()); wb.close(); } @@ -418,21 +420,21 @@ public abstract class BaseTestCell { Cell cell = createACell(wb); cell.setCellValue("TRUE"); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals(CellType.STRING, cell.getCellType()); // test conversion of cell from text to boolean - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellType(CellType.BOOLEAN); - assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); + assertEquals(CellType.BOOLEAN, cell.getCellType()); assertEquals(true, cell.getBooleanCellValue()); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); assertEquals("TRUE", cell.getRichStringCellValue().getString()); // 'false' text to bool and back cell.setCellValue("FALSE"); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); - assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); + cell.setCellType(CellType.BOOLEAN); + assertEquals(CellType.BOOLEAN, cell.getCellType()); assertEquals(false, cell.getBooleanCellValue()); - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); assertEquals("FALSE", cell.getRichStringCellValue().getString()); wb.close(); @@ -446,7 +448,7 @@ public abstract class BaseTestCell { cell.setCellValue(true); // test conversion of cell from boolean to text - cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellType(CellType.STRING); assertEquals("TRUE", cell.getRichStringCellValue().getString()); wb.close(); @@ -523,7 +525,7 @@ public abstract class BaseTestCell { fe.clearAllCachedResultValues(); fe.evaluateFormulaCell(cellA1); assertEquals("DEF", cellA1.getStringCellValue()); - cellA1.setCellType(Cell.CELL_TYPE_STRING); + cellA1.setCellType(CellType.STRING); assertEquals("DEF", cellA1.getStringCellValue()); cellA1.setCellFormula("25.061"); @@ -531,7 +533,7 @@ public abstract class BaseTestCell { fe.evaluateFormulaCell(cellA1); confirmCannotReadString(cellA1); assertEquals(25.061, cellA1.getNumericCellValue(), 0.0); - cellA1.setCellType(Cell.CELL_TYPE_STRING); + cellA1.setCellType(CellType.STRING); assertEquals("25.061", cellA1.getStringCellValue()); cellA1.setCellFormula("TRUE"); @@ -539,7 +541,7 @@ public abstract class BaseTestCell { fe.evaluateFormulaCell(cellA1); confirmCannotReadString(cellA1); assertEquals(true, cellA1.getBooleanCellValue()); - cellA1.setCellType(Cell.CELL_TYPE_STRING); + cellA1.setCellType(CellType.STRING); assertEquals("TRUE", cellA1.getStringCellValue()); cellA1.setCellFormula("#NAME?"); @@ -547,14 +549,14 @@ public abstract class BaseTestCell { fe.evaluateFormulaCell(cellA1); confirmCannotReadString(cellA1); assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue())); - cellA1.setCellType(Cell.CELL_TYPE_STRING); + cellA1.setCellType(CellType.STRING); assertEquals("#NAME?", cellA1.getStringCellValue()); wb.close(); } private static void confirmCannotReadString(Cell cell) { - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_STRING); + assertProhibitedValueAccess(cell, CellType.STRING); } /** @@ -567,7 +569,7 @@ public abstract class BaseTestCell { Cell cell = createACell(wb); cell.setCellFormula("1=1"); cell.setCellValue(true); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellType(CellType.BOOLEAN); assertTrue("Identified bug 46479d", cell.getBooleanCellValue()); assertEquals(true, cell.getBooleanCellValue()); @@ -585,19 +587,19 @@ public abstract class BaseTestCell { Cell cell; Row row = workSheet.createRow(0); - cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC); + cell = row.createCell(0, CellType.NUMERIC); cell.setCellValue(1.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); assertEquals(1.0, cell.getNumericCellValue(), 0.0); - cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); + cell = row.createCell(1, CellType.NUMERIC); cell.setCellValue(2.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); assertEquals(2.0, cell.getNumericCellValue(), 0.0); - cell = row.createCell(2, Cell.CELL_TYPE_FORMULA); + cell = row.createCell(2, CellType.FORMULA); cell.setCellFormula("SUM(A1:B1)"); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("SUM(A1:B1)", cell.getCellFormula()); //serialize and check again @@ -605,15 +607,15 @@ public abstract class BaseTestCell { wb1.close(); row = wb2.getSheetAt(0).getRow(0); cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); assertEquals(1.0, cell.getNumericCellValue(), 0.0); cell = row.getCell(1); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); assertEquals(2.0, cell.getNumericCellValue(), 0.0); cell = row.getCell(2); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals(CellType.FORMULA, cell.getCellType()); assertEquals("SUM(A1:B1)", cell.getCellFormula()); wb2.close(); } @@ -628,7 +630,7 @@ public abstract class BaseTestCell { } /** - * Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style + * Make sure that cell.setCellType(CellType.BLANK) preserves the cell style */ @Test public void testSetBlank_bug47028() throws Exception { @@ -637,7 +639,7 @@ public abstract class BaseTestCell { Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); cell.setCellStyle(style); int i1 = cell.getCellStyle().getIndex(); - cell.setCellType(Cell.CELL_TYPE_BLANK); + cell.setCellType(CellType.BLANK); int i2 = cell.getCellStyle().getIndex(); assertEquals(i1, i2); wb.close(); @@ -670,17 +672,17 @@ public abstract class BaseTestCell { Cell cell0 = row.createCell(0); cell0.setCellValue(Double.NaN); - assertEquals("Double.NaN should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell0.getCellType()); + assertEquals("Double.NaN should change cell type to CELL_TYPE_ERROR", CellType.ERROR, cell0.getCellType()); assertEquals("Double.NaN should change cell value to #NUM!", FormulaError.NUM, forInt(cell0.getErrorCellValue())); Cell cell1 = row.createCell(1); cell1.setCellValue(Double.POSITIVE_INFINITY); - assertEquals("Double.POSITIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell1.getCellType()); + assertEquals("Double.POSITIVE_INFINITY should change cell type to CELL_TYPE_ERROR", CellType.ERROR, cell1.getCellType()); assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell1.getErrorCellValue())); Cell cell2 = row.createCell(2); cell2.setCellValue(Double.NEGATIVE_INFINITY); - assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell2.getCellType()); + assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", CellType.ERROR, cell2.getCellType()); assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell2.getErrorCellValue())); Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); @@ -688,15 +690,15 @@ public abstract class BaseTestCell { row = wb2.getSheetAt(0).getRow(0); cell0 = row.getCell(0); - assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType()); + assertEquals(CellType.ERROR, cell0.getCellType()); assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue())); cell1 = row.getCell(1); - assertEquals(Cell.CELL_TYPE_ERROR, cell1.getCellType()); + assertEquals(CellType.ERROR, cell1.getCellType()); assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue())); cell2 = row.getCell(2); - assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType()); + assertEquals(CellType.ERROR, cell2.getCellType()); assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue())); wb2.close(); } @@ -897,21 +899,21 @@ public abstract class BaseTestCell { RichTextString nullStr = null; cell.setCellValue(nullStr); assertEquals("", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(CellType.BLANK, cell.getCellType()); cell = sheet.createRow(0).createCell(1); cell.setCellValue(1.2d); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(CellType.NUMERIC, cell.getCellType()); cell.setCellValue(nullStr); assertEquals("", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(CellType.BLANK, cell.getCellType()); cell = sheet.createRow(0).createCell(1); cell.setCellValue(wb.getCreationHelper().createRichTextString("Test")); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals(CellType.STRING, cell.getCellType()); cell.setCellValue(nullStr); assertEquals("", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(CellType.BLANK, cell.getCellType()); wb.close(); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java index d7439223be..34aa9f5dbd 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java @@ -216,7 +216,7 @@ public abstract class BaseTestFormulaEvaluator { FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); Sheet sheet = wb.createSheet("Sheet1"); Row r = sheet.createRow(0); - Cell c = r.createCell(0, Cell.CELL_TYPE_FORMULA); + Cell c = r.createCell(0, CellType.FORMULA); // Create a value and check it c.setCellFormula("Date(2011,10,6)"); |