diff options
author | Javen O'Neal <onealj@apache.org> | 2016-07-04 09:45:46 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-07-04 09:45:46 +0000 |
commit | fb8c79d96c2f0ddffb13f5b96c0f65b66c77ce5f (patch) | |
tree | 969ae610e5625f89327a7c90c8e112db46e769f0 /src/java/org/apache/poi | |
parent | 77bf864c07485013123d93d0a152e00906a40742 (diff) | |
download | poi-fb8c79d96c2f0ddffb13f5b96c0f65b66c77ce5f.tar.gz poi-fb8c79d96c2f0ddffb13f5b96c0f65b66c77ce5f.zip |
bug 59791: convert Cell Type to an enum
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751237 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
21 files changed, 477 insertions, 325 deletions
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) { |