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
This commit is contained in:
Javen O'Neal 2016-07-04 09:45:46 +00:00
parent 8df4c77647
commit d1d6ea97fb
54 changed files with 1337 additions and 1120 deletions

View File

@ -187,15 +187,15 @@ public final class HSSFReadWrite {
switch (cell.getCellType()) { switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
value = "FORMULA value=" + cell.getCellFormula(); value = "FORMULA value=" + cell.getCellFormula();
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue(); value = "NUMERIC value=" + cell.getNumericCellValue();
break; break;
case HSSFCell.CELL_TYPE_STRING: case STRING:
value = "STRING value=" + cell.getStringCellValue(); value = "STRING value=" + cell.getStringCellValue();
break; break;

View File

@ -152,23 +152,23 @@ public class SVTableCellEditor extends AbstractCellEditor implements TableCellEd
//Set the value that is rendered for the cell //Set the value that is rendered for the cell
switch (cell.getCellType()) { switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK: case BLANK:
editor.setText(""); editor.setText("");
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
if (cell.getBooleanCellValue()) { if (cell.getBooleanCellValue()) {
editor.setText("true"); editor.setText("true");
} else { } else {
editor.setText("false"); editor.setText("false");
} }
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
editor.setText(Double.toString(cell.getNumericCellValue())); editor.setText(Double.toString(cell.getNumericCellValue()));
break; break;
case HSSFCell.CELL_TYPE_STRING: case STRING:
editor.setText(cell.getRichStringCellValue().getString()); editor.setText(cell.getRichStringCellValue().getString());
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
default: default:
editor.setText("?"); editor.setText("?");
} }

View File

@ -166,17 +166,17 @@ public class SVTableCellRenderer extends JLabel
//Set the value that is rendered for the cell //Set the value that is rendered for the cell
switch (c.getCellType()) { switch (c.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK: case BLANK:
setValue(""); setValue("");
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
if (c.getBooleanCellValue()) { if (c.getBooleanCellValue()) {
setValue("true"); setValue("true");
} else { } else {
setValue("false"); setValue("false");
} }
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
short format = s.getDataFormat(); short format = s.getDataFormat();
double numericValue = c.getNumericCellValue(); double numericValue = c.getNumericCellValue();
if (cellFormatter.useRedColor(format, numericValue)) if (cellFormatter.useRedColor(format, numericValue))
@ -184,10 +184,10 @@ public class SVTableCellRenderer extends JLabel
else setForeground(null); else setForeground(null);
setValue(cellFormatter.format(format, c.getNumericCellValue())); setValue(cellFormatter.format(format, c.getNumericCellValue()));
break; break;
case HSSFCell.CELL_TYPE_STRING: case STRING:
setValue(c.getRichStringCellValue().getString()); setValue(c.getRichStringCellValue().getString());
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
default: default:
setValue("?"); setValue("?");
} }

View File

@ -25,6 +25,7 @@ import java.util.Locale;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell; 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.Color;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -178,25 +179,28 @@ public class ExcelComparator {
private void compareDataInCell(Locator loc1, Locator loc2) { private void compareDataInCell(Locator loc1, Locator loc2) {
if (isCellTypeMatches(loc1, loc2)) { if (isCellTypeMatches(loc1, loc2)) {
switch(loc1.cell.getCellType()) { final CellType loc1cellType = loc1.cell.getCellType();
case Cell.CELL_TYPE_BLANK: switch(loc1cellType) {
case Cell.CELL_TYPE_STRING: case BLANK:
case Cell.CELL_TYPE_ERROR: case STRING:
isCellContentMatches(loc1,loc2); case ERROR:
break; isCellContentMatches(loc1,loc2);
case Cell.CELL_TYPE_BOOLEAN: break;
isCellContentMatchesForBoolean(loc1,loc2); case BOOLEAN:
break; isCellContentMatchesForBoolean(loc1,loc2);
case Cell.CELL_TYPE_FORMULA: break;
isCellContentMatchesForFormula(loc1,loc2); case FORMULA:
break; isCellContentMatchesForFormula(loc1,loc2);
case Cell.CELL_TYPE_NUMERIC: break;
if (DateUtil.isCellDateFormatted(loc1.cell)) { case NUMERIC:
isCellContentMatchesForDate(loc1,loc2); if (DateUtil.isCellDateFormatted(loc1.cell)) {
} else { isCellContentMatchesForDate(loc1,loc2);
isCellContentMatchesForNumeric(loc1,loc2); } else {
} isCellContentMatchesForNumeric(loc1,loc2);
break; }
break;
default:
throw new IllegalStateException("Unexpected cell type: " + loc1cellType);
} }
} }
@ -577,13 +581,12 @@ public class ExcelComparator {
* Checks if cell type matches. * Checks if cell type matches.
*/ */
private boolean isCellTypeMatches(Locator loc1, Locator loc2) { private boolean isCellTypeMatches(Locator loc1, Locator loc2) {
int type1 = loc1.cell.getCellType(); CellType type1 = loc1.cell.getCellType();
int type2 = loc2.cell.getCellType(); CellType type2 = loc2.cell.getCellType();
if (type1 == type2) return true; if (type1 == type2) return true;
addMessage(loc1, loc2, addMessage(loc1, loc2,
"Cell Data-Type does not Match in :: ", "Cell Data-Type does not Match in :: ",
Integer.toString(type1), type1.name(), type2.name()
Integer.toString(type2)
); );
return false; return false;
} }

View File

@ -16,7 +16,6 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.examples.html; 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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.format.CellFormat; import org.apache.poi.ss.format.CellFormat;
@ -336,9 +335,9 @@ public class ToHtml {
} }
} }
private static int ultimateCellType(Cell c) { private static CellType ultimateCellType(Cell c) {
int type = c.getCellType(); CellType type = c.getCellType();
if (type == Cell.CELL_TYPE_FORMULA) if (type == CellType.FORMULA)
type = c.getCachedFormulaResultType(); type = c.getCachedFormulaResultType();
return type; return type;
} }
@ -443,12 +442,12 @@ public class ToHtml {
private String tagStyle(Cell cell, CellStyle style) { private String tagStyle(Cell cell, CellStyle style) {
if (style.getAlignment() == ALIGN_GENERAL) { if (style.getAlignment() == ALIGN_GENERAL) {
switch (ultimateCellType(cell)) { switch (ultimateCellType(cell)) {
case HSSFCell.CELL_TYPE_STRING: case STRING:
return "style=\"text-align: left;\""; return "style=\"text-align: left;\"";
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
return "style=\"text-align: center;\""; return "style=\"text-align: center;\"";
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
default: default:
// "right" is the default // "right" is the default
break; break;

View File

@ -323,42 +323,40 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
outputContents = _includeBlankCells; outputContents = _includeBlankCells;
} else { } else {
switch(cell.getCellType()) { switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING: case STRING:
text.append(cell.getRichStringCellValue().getString()); text.append(cell.getRichStringCellValue().getString());
break; break;
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
text.append( text.append(_formatter.formatCellValue(cell));
_formatter.formatCellValue(cell)
);
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
text.append(cell.getBooleanCellValue()); text.append(cell.getBooleanCellValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue())); text.append(ErrorEval.getText(cell.getErrorCellValue()));
break; break;
case Cell.CELL_TYPE_FORMULA: case FORMULA:
if(!_shouldEvaluateFormulas) { if(!_shouldEvaluateFormulas) {
text.append(cell.getCellFormula()); text.append(cell.getCellFormula());
} else { } else {
switch(cell.getCachedFormulaResultType()) { switch(cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_STRING: case STRING:
HSSFRichTextString str = cell.getRichStringCellValue(); HSSFRichTextString str = cell.getRichStringCellValue();
if(str != null && str.length() > 0) { if(str != null && str.length() > 0) {
text.append(str.toString()); text.append(str.toString());
} }
break; break;
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
HSSFCellStyle style = cell.getCellStyle(); HSSFCellStyle style = cell.getCellStyle();
double nVal = cell.getNumericCellValue(); double nVal = cell.getNumericCellValue();
short df = style.getDataFormat(); short df = style.getDataFormat();
String dfs = style.getDataFormatString(); String dfs = style.getDataFormatString();
text.append(_formatter.formatRawCellContents(nVal, df, dfs)); text.append(_formatter.formatRawCellContents(nVal, df, dfs));
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
text.append(cell.getBooleanCellValue()); text.append(cell.getBooleanCellValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue())); text.append(ErrorEval.getText(cell.getErrorCellValue()));
break; break;
default: default:

View File

@ -43,7 +43,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentNode; import org.apache.poi.poifs.filesystem.DocumentNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.NotOLE2FileException; 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; 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... // Biff 2 and 5+ share the same SID, due to a bug...
if (biffVersion == 5) { if (biffVersion == 5) {
FormulaRecord fr = new FormulaRecord(ris); FormulaRecord fr = new FormulaRecord(ris);
if (fr.getCachedResultType() == Cell.CELL_TYPE_NUMERIC) { if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
handleNumericCell(text, fr.getValue()); handleNumericCell(text, fr.getValue());
} }
} else { } else {
OldFormulaRecord fr = new OldFormulaRecord(ris); OldFormulaRecord fr = new OldFormulaRecord(ris);
if (fr.getCachedResultType() == Cell.CELL_TYPE_NUMERIC) { if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
handleNumericCell(text, fr.getValue()); handleNumericCell(text, fr.getValue());
} }
} }

View File

@ -17,10 +17,10 @@
package org.apache.poi.hssf.record; 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.Formula;
import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.ptg.Ptg; 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.BitField;
import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.HexDump; 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 VARIABLE_DATA_LENGTH = 6;
private static final int DATA_INDEX = 2; 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 STRING = 0;
public static final int BOOLEAN = 1; public static final int BOOLEAN = 1;
public static final int ERROR_CODE = 2; public static final int ERROR_CODE = 2;
@ -146,10 +148,10 @@ public final class FormulaRecord extends CellRecord implements Cloneable {
public int getValueType() { public int getValueType() {
int typeCode = getTypeCode(); int typeCode = getTypeCode();
switch (typeCode) { switch (typeCode) {
case STRING: return HSSFCell.CELL_TYPE_STRING; case STRING: return CellType.STRING.getCode();
case BOOLEAN: return HSSFCell.CELL_TYPE_BOOLEAN; case BOOLEAN: return CellType.BOOLEAN.getCode();
case ERROR_CODE: return HSSFCell.CELL_TYPE_ERROR; case ERROR_CODE: return CellType.ERROR.getCode();
case EMPTY: return HSSFCell.CELL_TYPE_STRING; // is this correct? case EMPTY: return CellType.STRING.getCode(); // is this correct?
} }
throw new IllegalStateException("Unexpected type id (" + typeCode + ")"); throw new IllegalStateException("Unexpected type id (" + typeCode + ")");
} }
@ -241,7 +243,7 @@ public final class FormulaRecord extends CellRecord implements Cloneable {
public int getCachedResultType() { public int getCachedResultType() {
if (specialCachedValue == null) { if (specialCachedValue == null) {
return HSSFCell.CELL_TYPE_NUMERIC; return CellType.NUMERIC.getCode();
} }
return specialCachedValue.getValueType(); return specialCachedValue.getValueType();
} }

View File

@ -21,6 +21,7 @@ import org.apache.poi.hssf.record.FormulaRecord.SpecialCachedValue;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.formula.Formula; import org.apache.poi.ss.formula.Formula;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.CellType;
/** /**
* Formula Record (0x0006 / 0x0206 / 0x0406) - holds a formula in * Formula Record (0x0006 / 0x0206 / 0x0406) - holds a formula in
@ -63,7 +64,7 @@ public final class OldFormulaRecord extends OldCellRecord {
public int getCachedResultType() { public int getCachedResultType() {
if (specialCachedValue == null) { if (specialCachedValue == null) {
return HSSFCell.CELL_TYPE_NUMERIC; return CellType.NUMERIC.getCode();
} }
return specialCachedValue.getValueType(); return specialCachedValue.getValueType();
} }

View File

@ -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.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.Comment;
import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
@ -80,7 +81,7 @@ public class HSSFCell implements Cell {
private final HSSFWorkbook _book; private final HSSFWorkbook _book;
private final HSSFSheet _sheet; private final HSSFSheet _sheet;
private int _cellType; private CellType _cellType;
private HSSFRichTextString _stringValue; private HSSFRichTextString _stringValue;
private CellValueRecordInterface _record; private CellValueRecordInterface _record;
private HSSFComment _comment; 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 * Creates new Cell - Should only be called by HSSFRow. This creates a cell
* from scratch. * from scratch.
* <p> * <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 * can be changed/overwritten by calling setCellValue with the appropriate
* type as a parameter although conversions from one type to another may be * type as a parameter although conversions from one type to another may be
* prohibited. * prohibited.
@ -109,10 +110,10 @@ public class HSSFCell implements Cell {
_sheet = sheet; _sheet = sheet;
// Relying on the fact that by default the cellType is set to 0 which // 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. // creates a new blank cell.
short xfindex = sheet.getSheet().getXFIndexForColAt(col); 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 sheet - Sheet record of the sheet containing this cell
* @param row - the row of this cell * @param row - the row of this cell
* @param col - the column for this cell * @param col - the column for this cell
* @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK, * @param type - Type of cell
* CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
* Type of cell
* @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(int,int) * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(int,int)
*/ */
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col, protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
int type) CellType type)
{ {
checkBounds(col); checkBounds(col);
_cellType = -1; // Force 'setCellType' to create a first Record _cellType = CellType._UNINITIALIZED; // Force 'setCellType' to create a first Record
_stringValue = null; _stringValue = null;
_book = book; _book = book;
_sheet = sheet; _sheet = sheet;
@ -176,14 +175,14 @@ public class HSSFCell implements Cell {
_sheet = sheet; _sheet = sheet;
switch (_cellType) switch (_cellType)
{ {
case CELL_TYPE_STRING : case STRING :
_stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval); _stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval);
break; break;
case CELL_TYPE_BLANK : case BLANK :
break; break;
case CELL_TYPE_FORMULA : case FORMULA :
_stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue()); _stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
break; break;
@ -196,23 +195,23 @@ public class HSSFCell implements Cell {
/** /**
* used internally -- given a cell value record, figure out its type * 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) { if (cval instanceof FormulaRecordAggregate) {
return HSSFCell.CELL_TYPE_FORMULA; return CellType.FORMULA;
} }
// all others are plain BIFF records // all others are plain BIFF records
Record record = ( Record ) cval; Record record = ( Record ) cval;
switch (record.getSid()) { switch (record.getSid()) {
case NumberRecord.sid : return HSSFCell.CELL_TYPE_NUMERIC; case NumberRecord.sid : return CellType.NUMERIC;
case BlankRecord.sid : return HSSFCell.CELL_TYPE_BLANK; case BlankRecord.sid : return CellType.BLANK;
case LabelSSTRecord.sid : return HSSFCell.CELL_TYPE_STRING; case LabelSSTRecord.sid : return CellType.STRING;
case BoolErrRecord.sid : case BoolErrRecord.sid :
BoolErrRecord boolErrRecord = ( BoolErrRecord ) record; BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
return boolErrRecord.isBoolean() return boolErrRecord.isBoolean()
? HSSFCell.CELL_TYPE_BOOLEAN ? CellType.BOOLEAN
: HSSFCell.CELL_TYPE_ERROR; : CellType.ERROR;
} }
throw new RuntimeException("Bad cell value rec (" + cval.getClass().getName() + ")"); throw new RuntimeException("Bad cell value rec (" + cval.getClass().getName() + ")");
} }
@ -255,19 +254,29 @@ public class HSSFCell implements Cell {
return new CellAddress(this); return new CellAddress(this);
} }
/** /**
* Set the cells type (numeric, formula or string). * Set the cells type (numeric, formula or string).
* If the cell currently contains a value, the value will * If the cell currently contains a value, the value will
* be converted to match the new type, if possible. * be converted to match the new type, if possible.
* @see #CELL_TYPE_NUMERIC * @see CellType#NUMERIC
* @see #CELL_TYPE_STRING * @see CellType#STRING
* @see #CELL_TYPE_FORMULA * @see CellType#FORMULA
* @see #CELL_TYPE_BLANK * @see CellType#BLANK
* @see #CELL_TYPE_BOOLEAN * @see CellType#BOOLEAN
* @see #CELL_TYPE_ERROR * @see CellType#ERROR
* @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/ */
@Override
public void setCellType(int cellType) { 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(); notifyFormulaChanging();
if(isPartOfArrayFormulaGroup()){ if(isPartOfArrayFormulaGroup()){
notifyArrayFormulaChanging(); 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) switch (cellType)
{ {
case CELL_TYPE_FORMULA : case FORMULA :
FormulaRecordAggregate frec; FormulaRecordAggregate frec;
if (cellType != _cellType) { if (cellType != _cellType) {
@ -315,7 +319,7 @@ public class HSSFCell implements Cell {
_record = frec; _record = frec;
break; break;
case CELL_TYPE_NUMERIC : case NUMERIC :
NumberRecord nrec = null; NumberRecord nrec = null;
if (cellType != _cellType) if (cellType != _cellType)
@ -336,7 +340,7 @@ public class HSSFCell implements Cell {
_record = nrec; _record = nrec;
break; break;
case CELL_TYPE_STRING : case STRING :
LabelSSTRecord lrec; LabelSSTRecord lrec;
if (cellType == _cellType) { if (cellType == _cellType) {
@ -352,7 +356,7 @@ public class HSSFCell implements Cell {
if(str == null) { if(str == null) {
// bug 55668: don't try to store null-string when formula // bug 55668: don't try to store null-string when formula
// results in empty/null value // results in empty/null value
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex); setCellType(CellType.BLANK, false, row, col, styleIndex);
return; return;
} else { } else {
int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str)); int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
@ -365,7 +369,7 @@ public class HSSFCell implements Cell {
_record = lrec; _record = lrec;
break; break;
case CELL_TYPE_BLANK : case BLANK :
BlankRecord brec = null; BlankRecord brec = null;
if (cellType != _cellType) if (cellType != _cellType)
@ -384,7 +388,7 @@ public class HSSFCell implements Cell {
_record = brec; _record = brec;
break; break;
case CELL_TYPE_BOOLEAN : case BOOLEAN :
BoolErrRecord boolRec = null; BoolErrRecord boolRec = null;
if (cellType != _cellType) if (cellType != _cellType)
@ -405,7 +409,7 @@ public class HSSFCell implements Cell {
_record = boolRec; _record = boolRec;
break; break;
case CELL_TYPE_ERROR : case ERROR :
BoolErrRecord errRec = null; BoolErrRecord errRec = null;
if (cellType != _cellType) if (cellType != _cellType)
@ -429,7 +433,7 @@ public class HSSFCell implements Cell {
throw new IllegalStateException("Invalid cell type: " + cellType); throw new IllegalStateException("Invalid cell type: " + cellType);
} }
if (cellType != _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); _sheet.getSheet().replaceValueRecord(_record);
} }
@ -438,14 +442,9 @@ public class HSSFCell implements Cell {
/** /**
* get the cells type (numeric, formula or string) * 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
*/ */
@Override
public int getCellType() public CellType getCellType()
{ {
return _cellType; return _cellType;
} }
@ -458,6 +457,7 @@ public class HSSFCell implements Cell {
* will change the cell to a numeric cell and set its value. * will change the cell to a numeric cell and set its value.
*/ */
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
@Override
public void setCellValue(double value) { public void setCellValue(double value) {
if(Double.isInfinite(value)) { if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities, // Excel does not support positive/negative infinities,
@ -474,12 +474,12 @@ public class HSSFCell implements Cell {
switch (_cellType) { switch (_cellType) {
default: default:
setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex); setCellType(CellType.NUMERIC, false, row, col, styleIndex);
// fall through // fall through
case CELL_TYPE_NUMERIC: case NUMERIC:
(( NumberRecord ) _record).setValue(value); (( NumberRecord ) _record).setValue(value);
break; break;
case CELL_TYPE_FORMULA: case FORMULA:
((FormulaRecordAggregate)_record).setCachedDoubleResult(value); ((FormulaRecordAggregate)_record).setCachedDoubleResult(value);
break; break;
} }
@ -550,7 +550,7 @@ public class HSSFCell implements Cell {
if (value == null) if (value == null)
{ {
notifyFormulaChanging(); notifyFormulaChanging();
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex); setCellType(CellType.BLANK, false, row, col, styleIndex);
return; return;
} }
@ -558,7 +558,7 @@ public class HSSFCell implements Cell {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); 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 // Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting. // note - formulas do not preserve text formatting.
FormulaRecordAggregate fr = (FormulaRecordAggregate) _record; FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
@ -573,8 +573,8 @@ public class HSSFCell implements Cell {
// If we get here, we're not dealing with a formula, // If we get here, we're not dealing with a formula,
// so handle things as a normal rich text cell // so handle things as a normal rich text cell
if (_cellType != CELL_TYPE_STRING) { if (_cellType != CellType.STRING) {
setCellType(CELL_TYPE_STRING, false, row, col, styleIndex); setCellType(CellType.STRING, false, row, col, styleIndex);
} }
int index = 0; int index = 0;
@ -598,12 +598,12 @@ public class HSSFCell implements Cell {
if (formula==null) { if (formula==null) {
notifyFormulaChanging(); notifyFormulaChanging();
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex); setCellType(CellType.BLANK, false, row, col, styleIndex);
return; return;
} }
int sheetIndex = _book.getSheetIndex(_sheet); int sheetIndex = _book.getSheetIndex(_sheet);
Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex); 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; FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
FormulaRecord frec = agg.getFormulaRecord(); FormulaRecord frec = agg.getFormulaRecord();
frec.setOptions((short) 2); frec.setOptions((short) 2);
@ -627,34 +627,18 @@ public class HSSFCell implements Cell {
public String getCellFormula() { public String getCellFormula() {
if (!(_record instanceof FormulaRecordAggregate)) { if (!(_record instanceof FormulaRecordAggregate)) {
throw typeMismatch(CELL_TYPE_FORMULA, _cellType, true); throw typeMismatch(CellType.FORMULA, _cellType, true);
} }
return HSSFFormulaParser.toFormulaString(_book, ((FormulaRecordAggregate)_record).getFormulaTokens()); return HSSFFormulaParser.toFormulaString(_book, ((FormulaRecordAggregate)_record).getFormulaTokens());
} }
/** private static RuntimeException typeMismatch(CellType expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) {
* Used to help format error messages String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode
*/ + " " + (isFormulaCell ? "formula " : "") + "cell";
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";
return new IllegalStateException(msg); return new IllegalStateException(msg);
} }
private static void checkFormulaCachedValueType(int expectedTypeCode, FormulaRecord fr) { private static void checkFormulaCachedValueType(CellType expectedTypeCode, FormulaRecord fr) {
int cachedValueType = fr.getCachedResultType(); CellType cachedValueType = CellType.forInt(fr.getCachedResultType());
if (cachedValueType != expectedTypeCode) { if (cachedValueType != expectedTypeCode) {
throw typeMismatch(expectedTypeCode, cachedValueType, true); throw typeMismatch(expectedTypeCode, cachedValueType, true);
} }
@ -671,17 +655,17 @@ public class HSSFCell implements Cell {
public double getNumericCellValue() { public double getNumericCellValue() {
switch(_cellType) { switch(_cellType) {
case CELL_TYPE_BLANK: case BLANK:
return 0.0; return 0.0;
case CELL_TYPE_NUMERIC: case NUMERIC:
return ((NumberRecord)_record).getValue(); return ((NumberRecord)_record).getValue();
default: default:
throw typeMismatch(CELL_TYPE_NUMERIC, _cellType, false); throw typeMismatch(CellType.NUMERIC, _cellType, false);
case CELL_TYPE_FORMULA: case FORMULA:
break; break;
} }
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord(); FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
checkFormulaCachedValueType(CELL_TYPE_NUMERIC, fr); checkFormulaCachedValueType(CellType.NUMERIC, fr);
return fr.getValue(); return fr.getValue();
} }
@ -694,7 +678,7 @@ public class HSSFCell implements Cell {
*/ */
public Date getDateCellValue() { public Date getDateCellValue() {
if (_cellType == CELL_TYPE_BLANK) { if (_cellType == CellType.BLANK) {
return null; return null;
} }
double value = getNumericCellValue(); double value = getNumericCellValue();
@ -723,17 +707,17 @@ public class HSSFCell implements Cell {
public HSSFRichTextString getRichStringCellValue() { public HSSFRichTextString getRichStringCellValue() {
switch(_cellType) { switch(_cellType) {
case CELL_TYPE_BLANK: case BLANK:
return new HSSFRichTextString(""); return new HSSFRichTextString("");
case CELL_TYPE_STRING: case STRING:
return _stringValue; return _stringValue;
default: default:
throw typeMismatch(CELL_TYPE_STRING, _cellType, false); throw typeMismatch(CellType.STRING, _cellType, false);
case CELL_TYPE_FORMULA: case FORMULA:
break; break;
} }
FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record); FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
checkFormulaCachedValueType(CELL_TYPE_STRING, fra.getFormulaRecord()); checkFormulaCachedValueType(CellType.STRING, fra.getFormulaRecord());
String strVal = fra.getStringValue(); String strVal = fra.getStringValue();
return new HSSFRichTextString(strVal == null ? "" : strVal); return new HSSFRichTextString(strVal == null ? "" : strVal);
} }
@ -753,12 +737,12 @@ public class HSSFCell implements Cell {
switch (_cellType) { switch (_cellType) {
default: default:
setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex); setCellType(CellType.BOOLEAN, false, row, col, styleIndex);
// fall through // fall through
case CELL_TYPE_BOOLEAN: case BOOLEAN:
(( BoolErrRecord ) _record).setValue(value); (( BoolErrRecord ) _record).setValue(value);
break; break;
case CELL_TYPE_FORMULA: case FORMULA:
((FormulaRecordAggregate)_record).setCachedBooleanResult(value); ((FormulaRecordAggregate)_record).setCachedBooleanResult(value);
break; break;
} }
@ -793,12 +777,12 @@ public class HSSFCell implements Cell {
short styleIndex=_record.getXFIndex(); short styleIndex=_record.getXFIndex();
switch (_cellType) { switch (_cellType) {
default: default:
setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex); setCellType(CellType.ERROR, false, row, col, styleIndex);
// fall through // fall through
case CELL_TYPE_ERROR: case ERROR:
(( BoolErrRecord ) _record).setValue(error); (( BoolErrRecord ) _record).setValue(error);
break; break;
case CELL_TYPE_FORMULA: case FORMULA:
((FormulaRecordAggregate)_record).setCachedErrorResult(error.getCode()); ((FormulaRecordAggregate)_record).setCachedErrorResult(error.getCode());
break; break;
} }
@ -816,24 +800,24 @@ public class HSSFCell implements Cell {
private boolean convertCellValueToBoolean() { private boolean convertCellValueToBoolean() {
switch (_cellType) { switch (_cellType) {
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return (( BoolErrRecord ) _record).getBooleanValue(); return (( BoolErrRecord ) _record).getBooleanValue();
case CELL_TYPE_STRING: case STRING:
int sstIndex = ((LabelSSTRecord)_record).getSSTIndex(); int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
String text = _book.getWorkbook().getSSTString(sstIndex).getString(); String text = _book.getWorkbook().getSSTString(sstIndex).getString();
return Boolean.valueOf(text).booleanValue(); return Boolean.valueOf(text).booleanValue();
case CELL_TYPE_NUMERIC: case NUMERIC:
return ((NumberRecord)_record).getValue() != 0; return ((NumberRecord)_record).getValue() != 0;
case CELL_TYPE_FORMULA: case FORMULA:
// use cached formula result if it's the right type: // use cached formula result if it's the right type:
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord(); FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr); checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue(); return fr.getCachedBooleanValue();
// Other cases convert to false // Other cases convert to false
// These choices are not well justified. // These choices are not well justified.
case CELL_TYPE_ERROR: case ERROR:
case CELL_TYPE_BLANK: case BLANK:
return false; return false;
} }
throw new RuntimeException("Unexpected cell type (" + _cellType + ")"); throw new RuntimeException("Unexpected cell type (" + _cellType + ")");
@ -841,18 +825,18 @@ public class HSSFCell implements Cell {
private String convertCellValueToString() { private String convertCellValueToString() {
switch (_cellType) { switch (_cellType) {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE"; return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
case CELL_TYPE_STRING: case STRING:
int sstIndex = ((LabelSSTRecord)_record).getSSTIndex(); int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
return _book.getWorkbook().getSSTString(sstIndex).getString(); return _book.getWorkbook().getSSTString(sstIndex).getString();
case CELL_TYPE_NUMERIC: case NUMERIC:
return NumberToTextConverter.toText(((NumberRecord)_record).getValue()); return NumberToTextConverter.toText(((NumberRecord)_record).getValue());
case CELL_TYPE_ERROR: case ERROR:
return FormulaError.forInt(((BoolErrRecord)_record).getErrorValue()).getString(); return FormulaError.forInt(((BoolErrRecord)_record).getErrorValue()).getString();
case CELL_TYPE_FORMULA: case FORMULA:
// should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
// just use cached formula result instead // just use cached formula result instead
break; break;
@ -861,37 +845,40 @@ public class HSSFCell implements Cell {
} }
FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record); FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
FormulaRecord fr = fra.getFormulaRecord(); FormulaRecord fr = fra.getFormulaRecord();
switch (fr.getCachedResultType()) { switch (CellType.forInt(fr.getCachedResultType())) {
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return fr.getCachedBooleanValue() ? "TRUE" : "FALSE"; return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
case CELL_TYPE_STRING: case STRING:
return fra.getStringValue(); return fra.getStringValue();
case CELL_TYPE_NUMERIC: case NUMERIC:
return NumberToTextConverter.toText(fr.getValue()); return NumberToTextConverter.toText(fr.getValue());
case CELL_TYPE_ERROR: case ERROR:
return FormulaError.forInt(fr.getCachedErrorValue()).getString(); 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. * 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. * For blank cells we return a false.
*/ */
@Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
switch(_cellType) { switch(_cellType) {
case CELL_TYPE_BLANK: case BLANK:
return false; return false;
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return (( BoolErrRecord ) _record).getBooleanValue(); return (( BoolErrRecord ) _record).getBooleanValue();
default: case FORMULA:
throw typeMismatch(CELL_TYPE_BOOLEAN, _cellType, false);
case CELL_TYPE_FORMULA:
break; break;
default:
throw typeMismatch(CellType.BOOLEAN, _cellType, false);
} }
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord(); FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr); checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue(); 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. * 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. * For blank cells we return a 0.
*/ */
@Override
public byte getErrorCellValue() { public byte getErrorCellValue() {
switch(_cellType) { switch(_cellType) {
case CELL_TYPE_ERROR: case ERROR:
return (( BoolErrRecord ) _record).getErrorValue(); return (( BoolErrRecord ) _record).getErrorValue();
default: case FORMULA:
throw typeMismatch(CELL_TYPE_ERROR, _cellType, false);
case CELL_TYPE_FORMULA:
break; break;
default:
throw typeMismatch(CellType.ERROR, _cellType, false);
} }
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord(); FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
checkFormulaCachedValueType(CELL_TYPE_ERROR, fr); checkFormulaCachedValueType(CellType.ERROR, fr);
return (byte) fr.getCachedErrorValue(); return (byte) fr.getCachedErrorValue();
} }
@ -1008,15 +996,15 @@ public class HSSFCell implements Cell {
*/ */
public String toString() { public String toString() {
switch (getCellType()) { switch (getCellType()) {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return getBooleanCellValue()?"TRUE":"FALSE"; return getBooleanCellValue()?"TRUE":"FALSE";
case CELL_TYPE_ERROR: case ERROR:
return ErrorEval.getText((( BoolErrRecord ) _record).getErrorValue()); return ErrorEval.getText((( BoolErrRecord ) _record).getErrorValue());
case CELL_TYPE_FORMULA: case FORMULA:
return getCellFormula(); return getCellFormula();
case CELL_TYPE_NUMERIC: case NUMERIC:
//TODO apply the dataformat for this cell //TODO apply the dataformat for this cell
if (HSSFDateUtil.isCellDateFormatted(this)) { if (HSSFDateUtil.isCellDateFormatted(this)) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale()); SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
@ -1024,7 +1012,7 @@ public class HSSFCell implements Cell {
return sdf.format(getDateCellValue()); return sdf.format(getDateCellValue());
} }
return String.valueOf(getNumericCellValue()); return String.valueOf(getNumericCellValue());
case CELL_TYPE_STRING: case STRING:
return getStringCellValue(); return getStringCellValue();
default: default:
return "Unknown Cell Type: " + getCellType(); return "Unknown Cell Type: " + getCellType();
@ -1142,22 +1130,23 @@ public class HSSFCell implements Cell {
/** /**
* Only valid for formula cells * Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula * on the cached value of the formula
*/ */
public int getCachedFormulaResultType() { public CellType getCachedFormulaResultType() {
if (_cellType != CELL_TYPE_FORMULA) { if (_cellType != CellType.FORMULA) {
throw new IllegalStateException("Only formula cells have cached results"); 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) { void setCellArrayFormula(CellRangeAddress range) {
int row = _record.getRow(); int row = _record.getRow();
short col = _record.getColumn(); short col = _record.getColumn();
short styleIndex = _record.getXFIndex(); short styleIndex = _record.getXFIndex();
setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex); setCellType(CellType.FORMULA, false, row, col, styleIndex);
// Billet for formula in rec // Billet for formula in rec
Ptg[] ptgsForCell = {new ExpPtg(range.getFirstRow(), range.getFirstColumn())}; Ptg[] ptgsForCell = {new ExpPtg(range.getFirstRow(), range.getFirstColumn())};
@ -1166,7 +1155,7 @@ public class HSSFCell implements Cell {
} }
public CellRangeAddress getArrayFormulaRange() { public CellRangeAddress getArrayFormulaRange() {
if (_cellType != CELL_TYPE_FORMULA) { if (_cellType != CellType.FORMULA) {
String ref = new CellReference(this).formatAsString(); String ref = new CellReference(this).formatAsString();
throw new IllegalStateException("Cell " + ref throw new IllegalStateException("Cell " + ref
+ " is not part of an array formula."); + " is not part of an array formula.");
@ -1175,7 +1164,7 @@ public class HSSFCell implements Cell {
} }
public boolean isPartOfArrayFormulaGroup() { public boolean isPartOfArrayFormulaGroup() {
if (_cellType != CELL_TYPE_FORMULA) { if (_cellType != CellType.FORMULA) {
return false; return false;
} }
return ((FormulaRecordAggregate)_record).isPartOfArrayFormula(); return ((FormulaRecordAggregate)_record).isPartOfArrayFormula();

View File

@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet; import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.usermodel.CellType;
/** /**
* HSSF wrapper for a cell under evaluation * HSSF wrapper for a cell under evaluation
*/ */
@ -34,6 +35,7 @@ final class HSSFEvaluationCell implements EvaluationCell {
public HSSFEvaluationCell(HSSFCell cell) { public HSSFEvaluationCell(HSSFCell cell) {
this(cell, new HSSFEvaluationSheet(cell.getSheet())); this(cell, new HSSFEvaluationSheet(cell.getSheet()));
} }
@Override
public Object getIdentityKey() { public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key // save memory by just using the cell itself as the identity key
// Note - this assumes HSSFCell has not overridden hashCode and equals // Note - this assumes HSSFCell has not overridden hashCode and equals
@ -43,31 +45,40 @@ final class HSSFEvaluationCell implements EvaluationCell {
public HSSFCell getHSSFCell() { public HSSFCell getHSSFCell() {
return _cell; return _cell;
} }
@Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
return _cell.getBooleanCellValue(); return _cell.getBooleanCellValue();
} }
public int getCellType() { @Override
public CellType getCellType() {
return _cell.getCellType(); return _cell.getCellType();
} }
@Override
public int getColumnIndex() { public int getColumnIndex() {
return _cell.getColumnIndex(); return _cell.getColumnIndex();
} }
@Override
public int getErrorCellValue() { public int getErrorCellValue() {
return _cell.getErrorCellValue(); return _cell.getErrorCellValue();
} }
@Override
public double getNumericCellValue() { public double getNumericCellValue() {
return _cell.getNumericCellValue(); return _cell.getNumericCellValue();
} }
@Override
public int getRowIndex() { public int getRowIndex() {
return _cell.getRowIndex(); return _cell.getRowIndex();
} }
@Override
public EvaluationSheet getSheet() { public EvaluationSheet getSheet() {
return _evalSheet; return _evalSheet;
} }
@Override
public String getStringCellValue() { public String getStringCellValue() {
return _cell.getRichStringCellValue().getString(); return _cell.getRichStringCellValue().getString();
} }
public int getCachedFormulaResultType() { @Override
public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType(); return _cell.getCachedFormulaResultType();
} }
} }

View File

@ -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.eval.ValueEval;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row; 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 * @return <code>null</code> if the supplied cell is <code>null</code> or blank
*/ */
@Override @Override
public CellValue evaluate(Cell cell) { public CellValue evaluate(Cell cell) {
if (cell == null) { if (cell == null) {
return null; return null;
} }
switch (cell.getCellType()) { switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return CellValue.valueOf(cell.getBooleanCellValue()); return CellValue.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR: case ERROR:
return CellValue.getError(cell.getErrorCellValue()); return CellValue.getError(cell.getErrorCellValue());
case Cell.CELL_TYPE_FORMULA: case FORMULA:
return evaluateFormulaCellValue(cell); return evaluateFormulaCellValue(cell);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
return new CellValue(cell.getNumericCellValue()); return new CellValue(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING: case STRING:
return new CellValue(cell.getRichStringCellValue().getString()); return new CellValue(cell.getRichStringCellValue().getString());
case Cell.CELL_TYPE_BLANK: case BLANK:
return null; 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> * @return -1 for non-formula cells, or the type of the <em>formula result</em>
*/ */
@Override @Override
public int evaluateFormulaCell(Cell cell) { public CellType evaluateFormulaCell(Cell cell) {
if (cell == null || cell.getCellType() != Cell.CELL_TYPE_FORMULA) { if (cell == null || cell.getCellType() != CellType.FORMULA) {
return -1; return CellType._UNINITIALIZED;
} }
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is changed // cell remains a formula cell, but the cached value is changed
@ -241,7 +244,7 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
return null; return null;
} }
HSSFCell result = (HSSFCell) cell; HSSFCell result = (HSSFCell) cell;
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if (cell.getCellType() == CellType.FORMULA) {
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
setCellValue(cell, cv); setCellValue(cell, cv);
setCellType(cell, cv); // cell will no longer be a formula cell setCellType(cell, cv); // cell will no longer be a formula cell
@ -249,40 +252,42 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
return result; return result;
} }
private static void setCellType(Cell cell, CellValue cv) { private static void setCellType(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); CellType cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
case Cell.CELL_TYPE_ERROR: case ERROR:
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
case Cell.CELL_TYPE_STRING: case STRING:
cell.setCellType(cellType); cell.setCellType(cellType);
return; return;
case Cell.CELL_TYPE_BLANK: case BLANK:
// never happens - blanks eventually get translated to zero // 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 // 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) { private static void setCellValue(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); CellType cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
cell.setCellValue(cv.getBooleanValue()); cell.setCellValue(cv.getBooleanValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
cell.setCellErrorValue(cv.getErrorValue()); cell.setCellErrorValue(cv.getErrorValue());
break; break;
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
cell.setCellValue(cv.getNumberValue()); cell.setCellValue(cv.getNumberValue());
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
cell.setCellValue(new HSSFRichTextString(cv.getStringValue())); cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
break; break;
case Cell.CELL_TYPE_BLANK: case BLANK:
// never happens - blanks eventually get translated to zero // 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 // this will never happen, we have already evaluated the formula
default: default:
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
@ -325,7 +330,7 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
for(Row r : sheet) { for(Row r : sheet) {
for (Cell c : r) { for (Cell c : r) {
if (c.getCellType() == Cell.CELL_TYPE_FORMULA) { if (c.getCellType() == CellType.FORMULA) {
evaluator.evaluateFormulaCell(c); evaluator.evaluateFormulaCell(c);
} }
} }

View File

@ -26,6 +26,7 @@ import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.Row;
import org.apache.poi.util.Configurator; import org.apache.poi.util.Configurator;
@ -110,16 +111,36 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
@Override @Override
public HSSFCell createCell(int column) 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. * Use this to create new cells within the row and return it.
* <p> * <p>
* The cell that is returned will be of the requested type. * The cell that is returned will be of the requested type.
* The type can be changed either through calling setCellValue * The type can be changed either through calling setCellValue
* or setCellType, but there is a small overhead to doing this, * 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 * @param columnIndex - the column number this cell represents
* *
@ -128,7 +149,7 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
* the maximum number of columns supported by the Excel binary format (.xls) * the maximum number of columns supported by the Excel binary format (.xls)
*/ */
@Override @Override
public HSSFCell createCell(int columnIndex, int type) public HSSFCell createCell(int columnIndex, CellType type)
{ {
short shortCellNum = (short)columnIndex; short shortCellNum = (short)columnIndex;
if(columnIndex > 0x7FFF) { if(columnIndex > 0x7FFF) {
@ -367,10 +388,10 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
case RETURN_NULL_AND_BLANK: case RETURN_NULL_AND_BLANK:
return cell; return cell;
case RETURN_BLANK_AS_NULL: 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; return (isBlank) ? null : cell;
case CREATE_NULL_AS_BLANK: case CREATE_NULL_AS_BLANK:
return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
default: default:
throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
} }

View File

@ -29,6 +29,7 @@ import java.util.regex.Pattern;
import javax.swing.JLabel; import javax.swing.JLabel;
import org.apache.poi.ss.usermodel.Cell; 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.ConditionalFormatting;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule; import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
@ -265,11 +266,11 @@ public class CellFormat {
*/ */
public CellFormatResult apply(Cell c) { public CellFormatResult apply(Cell c) {
switch (ultimateType(c)) { switch (ultimateType(c)) {
case Cell.CELL_TYPE_BLANK: case BLANK:
return apply(""); return apply("");
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return apply(c.getBooleanCellValue()); return apply(c.getBooleanCellValue());
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
Double value = c.getNumericCellValue(); Double value = c.getNumericCellValue();
if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) { if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
if (DateUtil.isValidExcelDate(value)) { if (DateUtil.isValidExcelDate(value)) {
@ -280,7 +281,7 @@ public class CellFormat {
} else { } else {
return apply(value); return apply(value);
} }
case Cell.CELL_TYPE_STRING: case STRING:
return apply(c.getStringCellValue()); return apply(c.getStringCellValue());
default: default:
return apply("?"); return apply("?");
@ -335,26 +336,26 @@ public class CellFormat {
*/ */
public CellFormatResult apply(JLabel label, Cell c) { public CellFormatResult apply(JLabel label, Cell c) {
switch (ultimateType(c)) { switch (ultimateType(c)) {
case Cell.CELL_TYPE_BLANK: case BLANK:
return apply(label, ""); return apply(label, "");
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return apply(label, c.getBooleanCellValue()); return apply(label, c.getBooleanCellValue());
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
Double value = c.getNumericCellValue(); Double value = c.getNumericCellValue();
if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) { if (getApplicableFormatPart(value).getCellFormatType() == CellFormatType.DATE) {
if (DateUtil.isValidExcelDate(value)) { if (DateUtil.isValidExcelDate(value)) {
return apply(label, c.getDateCellValue(), value); return apply(label, c.getDateCellValue(), value);
} else {
return apply(label, INVALID_VALUE_FOR_FORMAT);
}
} else { } else {
return apply(label, INVALID_VALUE_FOR_FORMAT); return apply(label, value);
} }
} else { case STRING:
return apply(label, value); 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. * @return The ultimate type of this cell.
*/ */
public static int ultimateType(Cell cell) { public static CellType ultimateType(Cell cell) {
int type = cell.getCellType(); CellType type = cell.getCellType();
if (type == Cell.CELL_TYPE_FORMULA) if (type == CellType.FORMULA)
return cell.getCachedFormulaResultType(); return cell.getCachedFormulaResultType();
else else
return type; return type;

View File

@ -17,6 +17,8 @@
package org.apache.poi.ss.formula; 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 * Abstracts a cell for the purpose of formula evaluation. This interface represents both formula
* and non-formula cells.<br/> * and non-formula cells.<br/>
@ -35,12 +37,12 @@ public interface EvaluationCell {
EvaluationSheet getSheet(); EvaluationSheet getSheet();
int getRowIndex(); int getRowIndex();
int getColumnIndex(); int getColumnIndex();
int getCellType(); CellType getCellType();
double getNumericCellValue(); double getNumericCellValue();
String getStringCellValue(); String getStringCellValue();
boolean getBooleanCellValue(); boolean getBooleanCellValue();
int getErrorCellValue(); int getErrorCellValue();
int getCachedFormulaResultType(); CellType getCachedFormulaResultType();
} }

View File

@ -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.ptg.UnknownPtg;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder; import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder; 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.ss.util.CellReference;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
@ -276,7 +276,7 @@ public final class WorkbookEvaluator {
// avoid tracking dependencies to cells that have constant definition // avoid tracking dependencies to cells that have constant definition
boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true
: !_stabilityClassifier.isCellFinal(sheetIndex, rowIndex, columnIndex); : !_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); ValueEval result = getValueFromNonFormulaCell(srcCell);
if (shouldCellDependencyBeRecorded) { if (shouldCellDependencyBeRecorded) {
tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result); tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);
@ -315,22 +315,22 @@ public final class WorkbookEvaluator {
if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) { if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) {
logInfo(re.getCause().getMessage() + " - Continuing with cached value!"); logInfo(re.getCause().getMessage() + " - Continuing with cached value!");
switch(srcCell.getCachedFormulaResultType()) { switch(srcCell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
result = new NumberEval(srcCell.getNumericCellValue()); result = new NumberEval(srcCell.getNumericCellValue());
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
result = new StringEval(srcCell.getStringCellValue()); result = new StringEval(srcCell.getStringCellValue());
break; break;
case Cell.CELL_TYPE_BLANK: case BLANK:
result = BlankEval.instance; result = BlankEval.instance;
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
result = BoolEval.valueOf(srcCell.getBooleanCellValue()); result = BoolEval.valueOf(srcCell.getBooleanCellValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
result = ErrorEval.valueOf(srcCell.getErrorCellValue()); result = ErrorEval.valueOf(srcCell.getErrorCellValue());
break; break;
case Cell.CELL_TYPE_FORMULA: case FORMULA:
default: default:
throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!"); throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
} }
@ -385,20 +385,22 @@ public final class WorkbookEvaluator {
if (cell == null) { if (cell == null) {
return BlankEval.instance; return BlankEval.instance;
} }
int cellType = cell.getCellType(); CellType cellType = cell.getCellType();
switch (cellType) { switch (cellType) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
return new NumberEval(cell.getNumericCellValue()); return new NumberEval(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING: case STRING:
return new StringEval(cell.getStringCellValue()); return new StringEval(cell.getStringCellValue());
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return BoolEval.valueOf(cell.getBooleanCellValue()); return BoolEval.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_BLANK: case BLANK:
return BlankEval.instance; return BlankEval.instance;
case Cell.CELL_TYPE_ERROR: case ERROR:
return ErrorEval.valueOf(cell.getErrorCellValue()); return ErrorEval.valueOf(cell.getErrorCellValue());
default:
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }

View File

@ -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.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet; import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.usermodel.Cell; 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 * 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 */ /** corresponding cell from master workbook */
private final EvaluationCell _masterCell; private final EvaluationCell _masterCell;
private boolean _booleanValue; private boolean _booleanValue;
private int _cellType; private CellType _cellType;
private int _errorValue; private int _errorValue;
private double _numberValue; private double _numberValue;
private String _stringValue; private String _stringValue;
@ -59,64 +60,64 @@ final class ForkedEvaluationCell implements EvaluationCell {
Class<? extends ValueEval> cls = value.getClass(); Class<? extends ValueEval> cls = value.getClass();
if (cls == NumberEval.class) { if (cls == NumberEval.class) {
_cellType = Cell.CELL_TYPE_NUMERIC; _cellType = CellType.NUMERIC;
_numberValue = ((NumberEval)value).getNumberValue(); _numberValue = ((NumberEval)value).getNumberValue();
return; return;
} }
if (cls == StringEval.class) { if (cls == StringEval.class) {
_cellType = Cell.CELL_TYPE_STRING; _cellType = CellType.STRING;
_stringValue = ((StringEval)value).getStringValue(); _stringValue = ((StringEval)value).getStringValue();
return; return;
} }
if (cls == BoolEval.class) { if (cls == BoolEval.class) {
_cellType = Cell.CELL_TYPE_BOOLEAN; _cellType = CellType.BOOLEAN;
_booleanValue = ((BoolEval)value).getBooleanValue(); _booleanValue = ((BoolEval)value).getBooleanValue();
return; return;
} }
if (cls == ErrorEval.class) { if (cls == ErrorEval.class) {
_cellType = Cell.CELL_TYPE_ERROR; _cellType = CellType.ERROR;
_errorValue = ((ErrorEval)value).getErrorCode(); _errorValue = ((ErrorEval)value).getErrorCode();
return; return;
} }
if (cls == BlankEval.class) { if (cls == BlankEval.class) {
_cellType = Cell.CELL_TYPE_BLANK; _cellType = CellType.BLANK;
return; return;
} }
throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")"); throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
} }
public void copyValue(Cell destCell) { public void copyValue(Cell destCell) {
switch (_cellType) { switch (_cellType) {
case Cell.CELL_TYPE_BLANK: destCell.setCellType(Cell.CELL_TYPE_BLANK); return; case BLANK: destCell.setCellType(CellType.BLANK); return;
case Cell.CELL_TYPE_NUMERIC: destCell.setCellValue(_numberValue); return; case NUMERIC: destCell.setCellValue(_numberValue); return;
case Cell.CELL_TYPE_BOOLEAN: destCell.setCellValue(_booleanValue); return; case BOOLEAN: destCell.setCellValue(_booleanValue); return;
case Cell.CELL_TYPE_STRING: destCell.setCellValue(_stringValue); return; case STRING: destCell.setCellValue(_stringValue); return;
case Cell.CELL_TYPE_ERROR: destCell.setCellErrorValue((byte)_errorValue); 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) { if (_cellType != expectedCellType) {
throw new RuntimeException("Wrong data type (" + _cellType + ")"); throw new RuntimeException("Wrong data type (" + _cellType + ")");
} }
} }
public int getCellType() { public CellType getCellType() {
return _cellType; return _cellType;
} }
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
checkCellType(Cell.CELL_TYPE_BOOLEAN); checkCellType(CellType.BOOLEAN);
return _booleanValue; return _booleanValue;
} }
public int getErrorCellValue() { public int getErrorCellValue() {
checkCellType(Cell.CELL_TYPE_ERROR); checkCellType(CellType.ERROR);
return _errorValue; return _errorValue;
} }
public double getNumericCellValue() { public double getNumericCellValue() {
checkCellType(Cell.CELL_TYPE_NUMERIC); checkCellType(CellType.NUMERIC);
return _numberValue; return _numberValue;
} }
public String getStringCellValue() { public String getStringCellValue() {
checkCellType(Cell.CELL_TYPE_STRING); checkCellType(CellType.STRING);
return _stringValue; return _stringValue;
} }
public EvaluationSheet getSheet() { public EvaluationSheet getSheet() {
@ -128,7 +129,7 @@ final class ForkedEvaluationCell implements EvaluationCell {
public int getColumnIndex() { public int getColumnIndex() {
return _masterCell.getColumnIndex(); return _masterCell.getColumnIndex();
} }
public int getCachedFormulaResultType() { public CellType getCachedFormulaResultType() {
return _masterCell.getCachedFormulaResultType(); return _masterCell.getCachedFormulaResultType();
} }

View File

@ -33,7 +33,6 @@ import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationWorkbook; import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.IStabilityClassifier; import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
/** /**
@ -114,20 +113,21 @@ public final class ForkedEvaluator {
EvaluationCell cell = _sewb.getEvaluationCell(sheetName, rowIndex, columnIndex); EvaluationCell cell = _sewb.getEvaluationCell(sheetName, rowIndex, columnIndex);
switch (cell.getCellType()) { switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return BoolEval.valueOf(cell.getBooleanCellValue()); return BoolEval.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR: case ERROR:
return ErrorEval.valueOf(cell.getErrorCellValue()); return ErrorEval.valueOf(cell.getErrorCellValue());
case Cell.CELL_TYPE_FORMULA: case FORMULA:
return _evaluator.evaluate(cell); return _evaluator.evaluate(cell);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
return new NumberEval(cell.getNumericCellValue()); return new NumberEval(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING: case STRING:
return new StringEval(cell.getStringCellValue()); return new StringEval(cell.getStringCellValue());
case Cell.CELL_TYPE_BLANK: case BLANK:
return null; 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 * Coordinates several formula evaluators together so that formulas that involve external

View File

@ -43,43 +43,49 @@ public interface Cell {
* Numeric Cell type (0) * Numeric Cell type (0)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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) * String Cell type (1)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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) * Formula Cell type (2)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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) * Blank Cell type (3)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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) * Boolean Cell type (4)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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) * Error Cell type (5)
* @see #setCellType(int) * @see #setCellType(int)
* @see #getCellType() * @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 * Returns column index of this cell
@ -127,29 +133,38 @@ public interface Cell {
* @see #CELL_TYPE_BLANK * @see #CELL_TYPE_BLANK
* @see #CELL_TYPE_BOOLEAN * @see #CELL_TYPE_BOOLEAN
* @see #CELL_TYPE_ERROR * @see #CELL_TYPE_ERROR
* @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/ */
void setCellType(int cellType); 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.
* *
* @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 * Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula * on the cached value of the formula
*/ */
int getCachedFormulaResultType(); CellType getCachedFormulaResultType();
/** /**
* Set a numeric value for the cell * 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 * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
* cases (when entering date values), Excel automatically adjusts the * cases (when entering date values), Excel automatically adjusts the
* <i>cell style</i> to some date format, creating the illusion that the cell * <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 * does not attempt to replicate this behaviour. To make a numeric cell
* display as a date, use {@link #setCellStyle(CellStyle)} etc.</p> * 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, for example, <code>SUM(C4:E4)</code>
* *
* @return a formula for the cell * @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(); String getCellFormula();
@ -244,7 +259,7 @@ public interface Cell {
* For formulas or error cells we return the precalculated value; * For formulas or error cells we return the precalculated value;
* </p> * </p>
* @return the value of the cell as a number * @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>. * @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. * @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. * For strings we throw an exception. For blank cells we return a null.
* </p> * </p>
* @return the value of the cell as a date * @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>. * @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. * @see DataFormatter for formatting this date into a string similar to how excel does.
*/ */
@ -309,7 +324,7 @@ public interface Cell {
* </p> * </p>
* @return the value of the cell as a boolean * @return the value of the cell as a boolean
* @throws IllegalStateException if the cell type returned by {@link #getCellType()} * @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(); boolean getBooleanCellValue();
@ -321,13 +336,13 @@ public interface Cell {
* </p> * </p>
* *
* @return the value of the cell as an error code * @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 * @see FormulaError for error codes
*/ */
byte getErrorCellValue(); 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> * the Workbook.</p>
* *
* <p>To change the style of a cell without affecting other cells that use the same style, * <p>To change the style of a cell without affecting other cells that use the same style,

View File

@ -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;
}
}

View File

@ -23,19 +23,18 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
* Mimics the 'data view' of a cell. This allows formula evaluator * Mimics the 'data view' of a cell. This allows formula evaluator
* to return a CellValue instead of precasting the value to String * to return a CellValue instead of precasting the value to String
* or Number or boolean type. * or Number or boolean type.
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*/ */
public final class CellValue { public final class CellValue {
public static final CellValue TRUE = new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, true, null, 0); public static final CellValue TRUE = new CellValue(CellType.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 FALSE= new CellValue(CellType.BOOLEAN, 0.0, false, null, 0);
private final int _cellType; private final CellType _cellType;
private final double _numberValue; private final double _numberValue;
private final boolean _booleanValue; private final boolean _booleanValue;
private final String _textValue; private final String _textValue;
private final int _errorCode; private final int _errorCode;
private CellValue(int cellType, double numberValue, boolean booleanValue, private CellValue(CellType cellType, double numberValue, boolean booleanValue,
String textValue, int errorCode) { String textValue, int errorCode) {
_cellType = cellType; _cellType = cellType;
_numberValue = numberValue; _numberValue = numberValue;
@ -46,16 +45,16 @@ public final class CellValue {
public CellValue(double numberValue) { 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) { public static CellValue valueOf(boolean booleanValue) {
return booleanValue ? TRUE : FALSE; return booleanValue ? TRUE : FALSE;
} }
public CellValue(String stringValue) { 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) { 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. * @return Returns the cellType.
*/ */
public int getCellType() { public CellType getCellType() {
return _cellType; return _cellType;
} }
/** /**
@ -99,15 +98,17 @@ public final class CellValue {
public String formatAsString() { public String formatAsString() {
switch (_cellType) { switch (_cellType) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
return String.valueOf(_numberValue); return String.valueOf(_numberValue);
case Cell.CELL_TYPE_STRING: case STRING:
return '"' + _textValue + '"'; return '"' + _textValue + '"';
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return _booleanValue ? "TRUE" : "FALSE"; return _booleanValue ? "TRUE" : "FALSE";
case Cell.CELL_TYPE_ERROR: case ERROR:
return ErrorEval.getText(_errorCode); return ErrorEval.getText(_errorCode);
default:
return "<error unexpected cell type " + _cellType + ">";
} }
return "<error unexpected cell type " + _cellType + ">";
} }
} }

View File

@ -879,32 +879,33 @@ public class DataFormatter implements Observer {
return ""; return "";
} }
int cellType = cell.getCellType(); CellType cellType = cell.getCellType();
if (cellType == Cell.CELL_TYPE_FORMULA) { if (cellType == CellType.FORMULA) {
if (evaluator == null) { if (evaluator == null) {
return cell.getCellFormula(); return cell.getCellFormula();
} }
cellType = evaluator.evaluateFormulaCell(cell); cellType = evaluator.evaluateFormulaCell(cell);
} }
switch (cellType) { switch (cellType) {
case Cell.CELL_TYPE_NUMERIC : case NUMERIC :
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
return getFormattedDateString(cell); return getFormattedDateString(cell);
} }
return getFormattedNumberString(cell); return getFormattedNumberString(cell);
case Cell.CELL_TYPE_STRING : case STRING :
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
case Cell.CELL_TYPE_BOOLEAN : case BOOLEAN :
return String.valueOf(cell.getBooleanCellValue()); return String.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_BLANK : case BLANK :
return ""; return "";
case Cell.CELL_TYPE_ERROR: case ERROR:
return FormulaError.forInt(cell.getErrorCellValue()).getString(); return FormulaError.forInt(cell.getErrorCellValue()).getString();
default:
throw new RuntimeException("Unexpected celltype (" + cellType + ")");
} }
throw new RuntimeException("Unexpected celltype (" + cellType + ")");
} }

View File

@ -17,6 +17,9 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import java.util.Map; import java.util.Map;
import org.apache.poi.util.Internal;
import java.util.HashMap; import java.util.HashMap;
/** /**
@ -25,6 +28,9 @@ import java.util.HashMap;
* See also OOO's excelfileformat.pdf (2.5.6) * See also OOO's excelfileformat.pdf (2.5.6)
*/ */
public enum FormulaError { public enum FormulaError {
@Internal
_NO_ERROR(-1, "(no error)"),
/** /**
* Intended to indicate when two areas are required to intersect, but do not. * Intended to indicate when two areas are required to intersect, but do not.
* <p>Example: * <p>Example:

View File

@ -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 * 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. * 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 * If cell contains formula, it evaluates the formula, and

View File

@ -27,7 +27,7 @@ public interface Row extends Iterable<Cell> {
/** /**
* Use this to create new cells within the row and return it. * Use this to create new cells within the row and return it.
* <p> * <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>. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
* *
* @param column - the column number this cell represents * @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. * @return Cell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex &lt; 0 or greater than a maximum number of supported columns * @throws IllegalArgumentException if columnIndex &lt; 0 or greater than a maximum number of supported columns
* (255 for *.xls, 1048576 for *.xlsx) * (255 for *.xls, 1048576 for *.xlsx)
* @see Cell#CELL_TYPE_BLANK * @see CellType#BLANK
* @see Cell#CELL_TYPE_BOOLEAN * @see CellType#BOOLEAN
* @see Cell#CELL_TYPE_ERROR * @see CellType#ERROR
* @see Cell#CELL_TYPE_FORMULA * @see CellType#FORMULA
* @see Cell#CELL_TYPE_NUMERIC * @see CellType#NUMERIC
* @see Cell#CELL_TYPE_STRING * @see CellType#STRING
* @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead.
*/ */
Cell createCell(int column, int type); 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 &lt; 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. * Remove the Cell from this row.

View File

@ -28,6 +28,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.CellValue;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
@ -79,7 +80,7 @@ public class SheetUtil {
public void setIgnoreMissingWorkbooks(boolean ignore) {} public void setIgnoreMissingWorkbooks(boolean ignore) {}
public void evaluateAll() {} public void evaluateAll() {}
public int evaluateFormulaCell(Cell cell) { public CellType evaluateFormulaCell(Cell cell) {
return cell.getCachedFormulaResultType(); return cell.getCachedFormulaResultType();
} }
}; };
@ -119,15 +120,16 @@ public class SheetUtil {
} }
CellStyle style = cell.getCellStyle(); 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 // 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()); Font font = wb.getFontAt(style.getFontIndex());
double width = -1; double width = -1;
if (cellType == Cell.CELL_TYPE_STRING) { if (cellType == CellType.STRING) {
RichTextString rt = cell.getRichStringCellValue(); RichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n"); String[] lines = rt.getString().split("\\n");
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
@ -144,14 +146,14 @@ public class SheetUtil {
} }
} else { } else {
String sval = null; 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 to get it formatted to look the same as excel
try { try {
sval = formatter.formatCellValue(cell, dummyEvaluator); sval = formatter.formatCellValue(cell, dummyEvaluator);
} catch (Exception e) { } catch (Exception e) {
sval = String.valueOf(cell.getNumericCellValue()); 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); sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
} }
if(sval != null) { if(sval != null) {

View File

@ -26,6 +26,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.Comment;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.HeaderFooter; import org.apache.poi.ss.usermodel.HeaderFooter;
@ -161,19 +162,19 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
Cell cell = ri.next(); Cell cell = ri.next();
// Is it a formula one? // Is it a formula one?
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if(cell.getCellType() == CellType.FORMULA) {
if (formulasNotResults) { if (formulasNotResults) {
String contents = cell.getCellFormula(); String contents = cell.getCellFormula();
checkMaxTextSize(text, contents); checkMaxTextSize(text, contents);
text.append(contents); text.append(contents);
} else { } else {
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { if (cell.getCachedFormulaResultType() == CellType.STRING) {
handleStringCell(text, cell); handleStringCell(text, cell);
} else { } else {
handleNonStringCell(text, cell, formatter); handleNonStringCell(text, cell, formatter);
} }
} }
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { } else if(cell.getCellType() == CellType.STRING) {
handleStringCell(text, cell); handleStringCell(text, cell);
} else { } else {
handleNonStringCell(text, cell, formatter); handleNonStringCell(text, cell, formatter);
@ -235,12 +236,12 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
} }
private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) { private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
int type = cell.getCellType(); CellType type = cell.getCellType();
if (type == Cell.CELL_TYPE_FORMULA) { if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultType(); type = cell.getCachedFormulaResultType();
} }
if (type == Cell.CELL_TYPE_NUMERIC) { if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle(); CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) { if (cs != null && cs.getDataFormatString() != null) {

View File

@ -42,7 +42,7 @@ import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; import javax.xml.validation.Validator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 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.ss.usermodel.DateUtil;
import org.apache.poi.util.DocumentHelper; import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
@ -280,11 +280,11 @@ public class XSSFExportToXml implements Comparator<String>{
String value =""; String value ="";
switch (cell.getCellType()) { switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case STRING: value = cell.getStringCellValue(); break;
case XSSFCell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue(); break; case BOOLEAN: value += cell.getBooleanCellValue(); break;
case XSSFCell.CELL_TYPE_ERROR: value = cell.getErrorCellString(); break; case ERROR: value = cell.getErrorCellString(); break;
case XSSFCell.CELL_TYPE_FORMULA: case FORMULA:
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { if (cell.getCachedFormulaResultType() == CellType.STRING) {
value = cell.getStringCellValue(); value = cell.getStringCellValue();
} else { } else {
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
@ -295,7 +295,7 @@ public class XSSFExportToXml implements Comparator<String>{
} }
break; break;
case XSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
value = getFormattedDate(cell); value = getFormattedDate(cell);
} else { } else {

View File

@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.Comment;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.FormulaError;
@ -55,7 +56,7 @@ public class SXSSFCell implements Cell {
private CellStyle _style; private CellStyle _style;
private Property _firstProperty; private Property _firstProperty;
public SXSSFCell(SXSSFRow row,int cellType) public SXSSFCell(SXSSFRow row,CellType cellType)
{ {
_row=row; _row=row;
setType(cellType); setType(cellType);
@ -119,15 +120,26 @@ public class SXSSFCell implements Cell {
* Set the cells type (numeric, formula or string) * Set the cells type (numeric, formula or string)
* *
* @throws IllegalArgumentException if the specified cell type is invalid * @throws IllegalArgumentException if the specified cell type is invalid
* @see #CELL_TYPE_NUMERIC * @see CellType#NUMERIC
* @see #CELL_TYPE_STRING * @see CellType#STRING
* @see #CELL_TYPE_FORMULA * @see CellType#FORMULA
* @see #CELL_TYPE_BLANK * @see CellType#BLANK
* @see #CELL_TYPE_BOOLEAN * @see CellType#BOOLEAN
* @see #CELL_TYPE_ERROR * @see CellType#ERROR
* @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/ */
@Override @Override
public void setCellType(int cellType) 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); ensureType(cellType);
} }
@ -136,29 +148,23 @@ public class SXSSFCell implements Cell {
* Return the cell type. * Return the cell type.
* *
* @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 @Override
public int getCellType() public CellType getCellType()
{ {
return _value.getType(); return _value.getType();
} }
/** /**
* Only valid for formula cells * Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula * on the cached value of the formula
*/ */
@Override @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"); throw new IllegalStateException("Only formula cells have cached results");
} }
@ -182,8 +188,8 @@ public class SXSSFCell implements Cell {
} else if (Double.isNaN(value)){ } else if (Double.isNaN(value)){
setCellErrorValue(FormulaError.NUM.getCode()); setCellErrorValue(FormulaError.NUM.getCode());
} else { } else {
ensureTypeOrFormulaType(CELL_TYPE_NUMERIC); ensureTypeOrFormulaType(CellType.NUMERIC);
if(_value.getType()==CELL_TYPE_FORMULA) if(_value.getType()==CellType.FORMULA)
((NumericFormulaValue)_value).setPreEvaluatedValue(value); ((NumericFormulaValue)_value).setPreEvaluatedValue(value);
else else
((NumericValue)_value).setValue(value); ((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 * <b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
* cases (when entering date values), Excel automatically adjusts the * cases (when entering date values), Excel automatically adjusts the
* <i>cell style</i> to some date format, creating the illusion that the cell * <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 * does not attempt to replicate this behaviour. To make a numeric cell
* display as a date, use {@link #setCellStyle(CellStyle)} etc. * display as a date, use {@link #setCellStyle(CellStyle)} etc.
* *
@ -208,7 +214,7 @@ public class SXSSFCell implements Cell {
@Override @Override
public void setCellValue(Date value) { public void setCellValue(Date value) {
if(value == null) { if(value == null) {
setCellType(Cell.CELL_TYPE_BLANK); setCellType(CellType.BLANK);
return; return;
} }
@ -235,7 +241,7 @@ public class SXSSFCell implements Cell {
@Override @Override
public void setCellValue(Calendar value) { public void setCellValue(Calendar value) {
if(value == null) { if(value == null) {
setCellType(Cell.CELL_TYPE_BLANK); setCellType(CellType.BLANK);
return; return;
} }
@ -267,7 +273,7 @@ public class SXSSFCell implements Cell {
((RichTextValue)_value).setValue(xvalue); ((RichTextValue)_value).setValue(xvalue);
} else { } else {
setCellType(CELL_TYPE_BLANK); setCellType(CellType.BLANK);
} }
} }
@ -283,18 +289,18 @@ public class SXSSFCell implements Cell {
public void setCellValue(String value) public void setCellValue(String value)
{ {
if (value != null) { if (value != null) {
ensureTypeOrFormulaType(CELL_TYPE_STRING); ensureTypeOrFormulaType(CellType.STRING);
if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); 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); ((StringFormulaValue)_value).setPreEvaluatedValue(value);
else else
((PlainStringValue)_value).setValue(value); ((PlainStringValue)_value).setValue(value);
} else { } else {
setCellType(CELL_TYPE_BLANK); setCellType(CellType.BLANK);
} }
} }
@ -313,7 +319,7 @@ public class SXSSFCell implements Cell {
public void setCellFormula(String formula) throws FormulaParseException public void setCellFormula(String formula) throws FormulaParseException
{ {
if(formula == null) { if(formula == null) {
setType(Cell.CELL_TYPE_BLANK); setType(CellType.BLANK);
return; 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, for example, <code>SUM(C4:E4)</code>
* *
* @return a formula for the cell * @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 @Override
public String getCellFormula() public String getCellFormula()
{ {
if(_value.getType()!=CELL_TYPE_FORMULA) if(_value.getType()!=CellType.FORMULA)
throw typeMismatch(CELL_TYPE_FORMULA,_value.getType(),false); throw typeMismatch(CellType.FORMULA,_value.getType(),false);
return ((FormulaValue)_value).getValue(); return ((FormulaValue)_value).getValue();
} }
@ -341,29 +347,29 @@ public class SXSSFCell implements Cell {
* For formulas or error cells we return the precalculated value; * For formulas or error cells we return the precalculated value;
* </p> * </p>
* @return the value of the cell as a number * @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>. * @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. * @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 @Override
public double getNumericCellValue() public double getNumericCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) switch(cellType)
{ {
case CELL_TYPE_BLANK: case BLANK:
return 0.0; return 0.0;
case CELL_TYPE_FORMULA: case FORMULA:
{ {
FormulaValue fv=(FormulaValue)_value; FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_NUMERIC) if(fv.getFormulaType()!=CellType.NUMERIC)
throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, false); throw typeMismatch(CellType.NUMERIC, CellType.FORMULA, false);
return ((NumericFormulaValue)_value).getPreEvaluatedValue(); return ((NumericFormulaValue)_value).getPreEvaluatedValue();
} }
case CELL_TYPE_NUMERIC: case NUMERIC:
return ((NumericValue)_value).getValue(); return ((NumericValue)_value).getValue();
default: 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. * For strings we throw an exception. For blank cells we return a null.
* </p> * </p>
* @return the value of the cell as a date * @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>. * @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. * @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does.
*/ */
@Override @Override
public Date getDateCellValue() public Date getDateCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
if (cellType == CELL_TYPE_BLANK) if (cellType == CellType.BLANK)
{ {
return null; return null;
} }
@ -402,9 +408,9 @@ public class SXSSFCell implements Cell {
@Override @Override
public RichTextString getRichStringCellValue() public RichTextString getRichStringCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
if(getCellType() != CELL_TYPE_STRING) if(getCellType() != CellType.STRING)
throw typeMismatch(CELL_TYPE_STRING, cellType, false); throw typeMismatch(CellType.STRING, cellType, false);
StringValue sval = (StringValue)_value; StringValue sval = (StringValue)_value;
if(sval.isRichText()) if(sval.isRichText())
@ -427,19 +433,19 @@ public class SXSSFCell implements Cell {
@Override @Override
public String getStringCellValue() public String getStringCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) switch(cellType)
{ {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_FORMULA: case FORMULA:
{ {
FormulaValue fv=(FormulaValue)_value; FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_STRING) if(fv.getFormulaType()!=CellType.STRING)
throw typeMismatch(CELL_TYPE_STRING, CELL_TYPE_FORMULA, false); throw typeMismatch(CellType.STRING, CellType.FORMULA, false);
return ((StringFormulaValue)_value).getPreEvaluatedValue(); return ((StringFormulaValue)_value).getPreEvaluatedValue();
} }
case CELL_TYPE_STRING: case STRING:
{ {
if(((StringValue)_value).isRichText()) if(((StringValue)_value).isRichText())
return ((RichTextValue)_value).getValue().getString(); return ((RichTextValue)_value).getValue().getString();
@ -447,7 +453,7 @@ public class SXSSFCell implements Cell {
return ((PlainStringValue)_value).getValue(); return ((PlainStringValue)_value).getValue();
} }
default: default:
throw typeMismatch(CELL_TYPE_STRING, cellType, false); throw typeMismatch(CellType.STRING, cellType, false);
} }
} }
@ -461,8 +467,8 @@ public class SXSSFCell implements Cell {
@Override @Override
public void setCellValue(boolean value) public void setCellValue(boolean value)
{ {
ensureTypeOrFormulaType(CELL_TYPE_BOOLEAN); ensureTypeOrFormulaType(CellType.BOOLEAN);
if(_value.getType()==CELL_TYPE_FORMULA) if(_value.getType()==CellType.FORMULA)
((BooleanFormulaValue)_value).setPreEvaluatedValue(value); ((BooleanFormulaValue)_value).setPreEvaluatedValue(value);
else else
((BooleanValue)_value).setValue(value); ((BooleanValue)_value).setValue(value);
@ -480,8 +486,8 @@ public class SXSSFCell implements Cell {
@Override @Override
public void setCellErrorValue(byte value) public void setCellErrorValue(byte value)
{ {
ensureType(CELL_TYPE_ERROR); ensureType(CellType.ERROR);
if(_value.getType()==CELL_TYPE_FORMULA) if(_value.getType()==CellType.FORMULA)
((ErrorFormulaValue)_value).setPreEvaluatedValue(value); ((ErrorFormulaValue)_value).setPreEvaluatedValue(value);
else else
((ErrorValue)_value).setValue(value); ((ErrorValue)_value).setValue(value);
@ -494,29 +500,29 @@ public class SXSSFCell implements Cell {
* </p> * </p>
* @return the value of the cell as a boolean * @return the value of the cell as a boolean
* @throws IllegalStateException if the cell type returned by {@link #getCellType()} * @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 @Override
public boolean getBooleanCellValue() public boolean getBooleanCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) switch(cellType)
{ {
case CELL_TYPE_BLANK: case BLANK:
return false; return false;
case CELL_TYPE_FORMULA: case FORMULA:
{ {
FormulaValue fv=(FormulaValue)_value; FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_BOOLEAN) if(fv.getFormulaType()!=CellType.BOOLEAN)
throw typeMismatch(CELL_TYPE_BOOLEAN, CELL_TYPE_FORMULA, false); throw typeMismatch(CellType.BOOLEAN, CellType.FORMULA, false);
return ((BooleanFormulaValue)_value).getPreEvaluatedValue(); return ((BooleanFormulaValue)_value).getPreEvaluatedValue();
} }
case CELL_TYPE_BOOLEAN: case BOOLEAN:
{ {
return ((BooleanValue)_value).getValue(); return ((BooleanValue)_value).getValue();
} }
default: default:
throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); throw typeMismatch(CellType.BOOLEAN, cellType, false);
} }
} }
@ -528,30 +534,30 @@ public class SXSSFCell implements Cell {
* </p> * </p>
* *
* @return the value of the cell as an error code * @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 * @see org.apache.poi.ss.usermodel.FormulaError for error codes
*/ */
@Override @Override
public byte getErrorCellValue() public byte getErrorCellValue()
{ {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) switch(cellType)
{ {
case CELL_TYPE_BLANK: case BLANK:
return 0; return 0;
case CELL_TYPE_FORMULA: case FORMULA:
{ {
FormulaValue fv=(FormulaValue)_value; FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_ERROR) if(fv.getFormulaType()!=CellType.ERROR)
throw typeMismatch(CELL_TYPE_ERROR, CELL_TYPE_FORMULA, false); throw typeMismatch(CellType.ERROR, CellType.FORMULA, false);
return ((ErrorFormulaValue)_value).getPreEvaluatedValue(); return ((ErrorFormulaValue)_value).getPreEvaluatedValue();
} }
case CELL_TYPE_ERROR: case ERROR:
{ {
return ((ErrorValue)_value).getValue(); return ((ErrorValue)_value).getValue();
} }
default: default:
throw typeMismatch(CELL_TYPE_ERROR, cellType, false); throw typeMismatch(CellType.ERROR, cellType, false);
} }
} }
@ -709,22 +715,22 @@ public class SXSSFCell implements Cell {
@Override @Override
public String toString() { public String toString() {
switch (getCellType()) { switch (getCellType()) {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE"; return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_ERROR: case ERROR:
return ErrorEval.getText(getErrorCellValue()); return ErrorEval.getText(getErrorCellValue());
case CELL_TYPE_FORMULA: case FORMULA:
return getCellFormula(); return getCellFormula();
case CELL_TYPE_NUMERIC: case NUMERIC:
if (DateUtil.isCellDateFormatted(this)) { if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale()); DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone()); sdf.setTimeZone(LocaleUtil.getUserTimeZone());
return sdf.format(getDateCellValue()); return sdf.format(getDateCellValue());
} }
return getNumericCellValue() + ""; return getNumericCellValue() + "";
case CELL_TYPE_STRING: case STRING:
return getRichStringCellValue().toString(); return getRichStringCellValue().toString();
default: default:
return "Unknown Cell Type: " + getCellType(); return "Unknown Cell Type: " + getCellType();
@ -806,39 +812,39 @@ public class SXSSFCell implements Cell {
} }
/*package*/ void ensurePlainStringType() /*package*/ void ensurePlainStringType()
{ {
if(_value.getType()!=CELL_TYPE_STRING if(_value.getType()!=CellType.STRING
||((StringValue)_value).isRichText()) ||((StringValue)_value).isRichText())
_value=new PlainStringValue(); _value=new PlainStringValue();
} }
/*package*/ void ensureRichTextStringType() /*package*/ void ensureRichTextStringType()
{ {
if(_value.getType()!=CELL_TYPE_STRING if(_value.getType()!=CellType.STRING
||!((StringValue)_value).isRichText()) ||!((StringValue)_value).isRichText())
_value=new RichTextValue(); _value=new RichTextValue();
} }
/*package*/ void ensureType(int type) /*package*/ void ensureType(CellType type)
{ {
if(_value.getType()!=type) if(_value.getType()!=type)
setType(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) ||((FormulaValue)_value).getFormulaType()!=type)
setFormulaType(type); setFormulaType(type);
} }
/* /*
* Sets the cell type to type if it is different * 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(_value.getType()==type)
{ {
if(type==CELL_TYPE_STRING&&((StringValue)_value).isRichText()) if(type==CellType.STRING&&((StringValue)_value).isRichText())
setType(CELL_TYPE_STRING); setType(CellType.STRING);
return; return;
} }
if(_value.getType()==CELL_TYPE_FORMULA) if(_value.getType()==CellType.FORMULA)
{ {
if(((FormulaValue)_value).getFormulaType()==type) if(((FormulaValue)_value).getFormulaType()==type)
return; return;
@ -854,16 +860,16 @@ public class SXSSFCell implements Cell {
* @param type the cell type to set * @param type the cell type to set
* @throws IllegalArgumentException if type is not a recognized type * @throws IllegalArgumentException if type is not a recognized type
*/ */
/*package*/ void setType(int type) /*package*/ void setType(CellType type)
{ {
switch(type) switch(type)
{ {
case CELL_TYPE_NUMERIC: case NUMERIC:
{ {
_value=new NumericValue(); _value=new NumericValue();
break; break;
} }
case CELL_TYPE_STRING: case STRING:
{ {
PlainStringValue sval = new PlainStringValue(); PlainStringValue sval = new PlainStringValue();
if(_value != null){ if(_value != null){
@ -874,17 +880,17 @@ public class SXSSFCell implements Cell {
_value = sval; _value = sval;
break; break;
} }
case CELL_TYPE_FORMULA: case FORMULA:
{ {
_value=new NumericFormulaValue(); _value=new NumericFormulaValue();
break; break;
} }
case CELL_TYPE_BLANK: case BLANK:
{ {
_value=new BlankValue(); _value=new BlankValue();
break; break;
} }
case CELL_TYPE_BOOLEAN: case BOOLEAN:
{ {
BooleanValue bval = new BooleanValue(); BooleanValue bval = new BooleanValue();
if(_value != null){ if(_value != null){
@ -895,7 +901,7 @@ public class SXSSFCell implements Cell {
_value = bval; _value = bval;
break; break;
} }
case CELL_TYPE_ERROR: case ERROR:
{ {
_value=new ErrorValue(); _value=new ErrorValue();
break; break;
@ -906,26 +912,26 @@ public class SXSSFCell implements Cell {
} }
} }
} }
/*package*/ void setFormulaType(int type) /*package*/ void setFormulaType(CellType type)
{ {
switch(type) switch(type)
{ {
case CELL_TYPE_NUMERIC: case NUMERIC:
{ {
_value=new NumericFormulaValue(); _value=new NumericFormulaValue();
break; break;
} }
case CELL_TYPE_STRING: case STRING:
{ {
_value=new StringFormulaValue(); _value=new StringFormulaValue();
break; break;
} }
case CELL_TYPE_BOOLEAN: case BOOLEAN:
{ {
_value=new BooleanFormulaValue(); _value=new BooleanFormulaValue();
break; break;
} }
case CELL_TYPE_ERROR: case ERROR:
{ {
_value=new ErrorFormulaValue(); _value=new ErrorFormulaValue();
break; break;
@ -938,77 +944,64 @@ public class SXSSFCell implements Cell {
} }
//TODO: implement this correctly //TODO: implement this correctly
@NotImplemented @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 //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 * Used to help format error messages
*/ */
private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) { private static RuntimeException typeMismatch(CellType expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) {
String msg = "Cannot get a " String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode
+ getCellTypeName(expectedTypeCode) + " value from a " + " " + (isFormulaCell ? "formula " : "") + "cell";
+ getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg); 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();
if (cellType == CELL_TYPE_FORMULA) { private boolean convertCellValueToBoolean() {
CellType cellType = getCellType();
if (cellType == CellType.FORMULA) {
cellType = getCachedFormulaResultType(); cellType = getCachedFormulaResultType();
} }
switch (cellType) { switch (cellType) {
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return getBooleanCellValue(); return getBooleanCellValue();
case CELL_TYPE_STRING: case STRING:
String text = getStringCellValue(); String text = getStringCellValue();
return Boolean.parseBoolean(text); return Boolean.parseBoolean(text);
case CELL_TYPE_NUMERIC: case NUMERIC:
return getNumericCellValue() != 0; return getNumericCellValue() != 0;
case CELL_TYPE_ERROR: case ERROR:
case CELL_TYPE_BLANK: case BLANK:
return false; return false;
default: throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
private String convertCellValueToString() { private String convertCellValueToString() {
int cellType = getCellType(); CellType cellType = getCellType();
return convertCellValueToString(cellType); return convertCellValueToString(cellType);
} }
private String convertCellValueToString(int cellType) { private String convertCellValueToString(CellType cellType) {
switch (cellType) { switch (cellType) {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE"; return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_STRING: case STRING:
return getStringCellValue(); return getStringCellValue();
case CELL_TYPE_NUMERIC: case NUMERIC:
return Double.toString( getNumericCellValue() ); return Double.toString( getNumericCellValue() );
case CELL_TYPE_ERROR: case ERROR:
byte errVal = getErrorCellValue(); byte errVal = getErrorCellValue();
return FormulaError.forInt(errVal).getString(); return FormulaError.forInt(errVal).getString();
case CELL_TYPE_FORMULA: case FORMULA:
if (_value != null) { if (_value != null) {
FormulaValue fv = (FormulaValue)_value; FormulaValue fv = (FormulaValue)_value;
if (fv.getFormulaType() != CELL_TYPE_FORMULA) { if (fv.getFormulaType() != CellType.FORMULA) {
return convertCellValueToString(fv.getFormulaType()); return convertCellValueToString(fv.getFormulaType());
} }
} }
@ -1066,14 +1059,14 @@ public class SXSSFCell implements Cell {
} }
interface Value interface Value
{ {
int getType(); CellType getType();
} }
static class NumericValue implements Value static class NumericValue implements Value
{ {
double _value; double _value;
public int getType() public CellType getType()
{ {
return CELL_TYPE_NUMERIC; return CellType.NUMERIC;
} }
void setValue(double value) void setValue(double value)
{ {
@ -1086,11 +1079,11 @@ public class SXSSFCell implements Cell {
} }
static abstract class StringValue implements Value 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". abstract boolean isRichText(); // using the POI style which seems to avoid "instanceof".
} }
static class PlainStringValue extends StringValue static class PlainStringValue extends StringValue
@ -1114,9 +1107,9 @@ public class SXSSFCell implements Cell {
{ {
RichTextString _value; RichTextString _value;
@Override @Override
public int getType() public CellType getType()
{ {
return CELL_TYPE_STRING; return CellType.STRING;
} }
void setValue(RichTextString value) void setValue(RichTextString value)
{ {
@ -1135,9 +1128,9 @@ public class SXSSFCell implements Cell {
static abstract class FormulaValue implements Value static abstract class FormulaValue implements Value
{ {
String _value; String _value;
public int getType() public CellType getType()
{ {
return CELL_TYPE_FORMULA; return CellType.FORMULA;
} }
void setValue(String value) void setValue(String value)
{ {
@ -1147,15 +1140,15 @@ public class SXSSFCell implements Cell {
{ {
return _value; return _value;
} }
abstract int getFormulaType(); abstract CellType getFormulaType();
} }
static class NumericFormulaValue extends FormulaValue static class NumericFormulaValue extends FormulaValue
{ {
double _preEvaluatedValue; double _preEvaluatedValue;
@Override @Override
int getFormulaType() CellType getFormulaType()
{ {
return CELL_TYPE_NUMERIC; return CellType.NUMERIC;
} }
void setPreEvaluatedValue(double value) void setPreEvaluatedValue(double value)
{ {
@ -1170,9 +1163,9 @@ public class SXSSFCell implements Cell {
{ {
String _preEvaluatedValue; String _preEvaluatedValue;
@Override @Override
int getFormulaType() CellType getFormulaType()
{ {
return CELL_TYPE_STRING; return CellType.STRING;
} }
void setPreEvaluatedValue(String value) void setPreEvaluatedValue(String value)
{ {
@ -1187,9 +1180,9 @@ public class SXSSFCell implements Cell {
{ {
boolean _preEvaluatedValue; boolean _preEvaluatedValue;
@Override @Override
int getFormulaType() CellType getFormulaType()
{ {
return CELL_TYPE_BOOLEAN; return CellType.BOOLEAN;
} }
void setPreEvaluatedValue(boolean value) void setPreEvaluatedValue(boolean value)
{ {
@ -1204,9 +1197,9 @@ public class SXSSFCell implements Cell {
{ {
byte _preEvaluatedValue; byte _preEvaluatedValue;
@Override @Override
int getFormulaType() CellType getFormulaType()
{ {
return CELL_TYPE_ERROR; return CellType.ERROR;
} }
void setPreEvaluatedValue(byte value) void setPreEvaluatedValue(byte value)
{ {
@ -1219,17 +1212,17 @@ public class SXSSFCell implements Cell {
} }
static class BlankValue implements Value static class BlankValue implements Value
{ {
public int getType() public CellType getType()
{ {
return CELL_TYPE_BLANK; return CellType.BLANK;
} }
} }
static class BooleanValue implements Value static class BooleanValue implements Value
{ {
boolean _value; boolean _value;
public int getType() public CellType getType()
{ {
return CELL_TYPE_BOOLEAN; return CellType.BOOLEAN;
} }
void setValue(boolean value) void setValue(boolean value)
{ {
@ -1243,9 +1236,9 @@ public class SXSSFCell implements Cell {
static class ErrorValue implements Value static class ErrorValue implements Value
{ {
byte _value; byte _value;
public int getType() public CellType getType()
{ {
return CELL_TYPE_ERROR; return CellType.ERROR;
} }
void setValue(byte value) void setValue(byte value)
{ {

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.streaming;
import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet; import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.usermodel.CellType;
/** /**
* SXSSF wrapper for a cell under evaluation * SXSSF wrapper for a cell under evaluation
@ -36,6 +37,7 @@ final class SXSSFEvaluationCell implements EvaluationCell {
this(cell, new SXSSFEvaluationSheet(cell.getSheet())); this(cell, new SXSSFEvaluationSheet(cell.getSheet()));
} }
@Override
public Object getIdentityKey() { public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key // save memory by just using the cell itself as the identity key
// Note - this assumes SXSSFCell has not overridden hashCode and equals // Note - this assumes SXSSFCell has not overridden hashCode and equals
@ -45,31 +47,40 @@ final class SXSSFEvaluationCell implements EvaluationCell {
public SXSSFCell getSXSSFCell() { public SXSSFCell getSXSSFCell() {
return _cell; return _cell;
} }
@Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
return _cell.getBooleanCellValue(); return _cell.getBooleanCellValue();
} }
public int getCellType() { @Override
public CellType getCellType() {
return _cell.getCellType(); return _cell.getCellType();
} }
@Override
public int getColumnIndex() { public int getColumnIndex() {
return _cell.getColumnIndex(); return _cell.getColumnIndex();
} }
@Override
public int getErrorCellValue() { public int getErrorCellValue() {
return _cell.getErrorCellValue(); return _cell.getErrorCellValue();
} }
@Override
public double getNumericCellValue() { public double getNumericCellValue() {
return _cell.getNumericCellValue(); return _cell.getNumericCellValue();
} }
@Override
public int getRowIndex() { public int getRowIndex() {
return _cell.getRowIndex(); return _cell.getRowIndex();
} }
@Override
public EvaluationSheet getSheet() { public EvaluationSheet getSheet() {
return _evalSheet; return _evalSheet;
} }
@Override
public String getStringCellValue() { public String getStringCellValue() {
return _cell.getRichStringCellValue().getString(); return _cell.getRichStringCellValue().getString();
} }
public int getCachedFormulaResultType() { @Override
public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType(); return _cell.getCachedFormulaResultType();
} }
} }

View File

@ -26,6 +26,7 @@ import java.util.TreeMap;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.Internal; 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. * Use this to create new cells within the row and return it.
* <p> * <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>. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
* *
* @param column - the column number this cell represents * @param column - the column number this cell represents
@ -130,13 +131,30 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow>
@Override @Override
public SXSSFCell createCell(int column) 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. * Use this to create new cells within the row and return it.
* <p> * <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. * either through calling setCellValue or setCellType.
* *
* @param column - the column number this cell represents * @param column - the column number this cell represents
@ -145,7 +163,7 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow>
* (255 for *.xls, 1048576 for *.xlsx) * (255 for *.xls, 1048576 for *.xlsx)
*/ */
@Override @Override
public SXSSFCell createCell(int column, int type) public SXSSFCell createCell(int column, CellType type)
{ {
checkBounds(column); checkBounds(column);
SXSSFCell cell = new SXSSFCell(this, type); SXSSFCell cell = new SXSSFCell(this, type);
@ -250,10 +268,10 @@ public class SXSSFRow implements Row, Comparable<SXSSFRow>
case RETURN_NULL_AND_BLANK: case RETURN_NULL_AND_BLANK:
return cell; return cell;
case RETURN_BLANK_AS_NULL: 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; return (isBlank) ? null : cell;
case CREATE_NULL_AS_BLANK: case CREATE_NULL_AS_BLANK:
return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
default: default:
throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
} }

View File

@ -31,6 +31,7 @@ import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.usermodel.FormulaError;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
@ -201,19 +202,19 @@ public class SheetDataWriter {
// APIs // APIs
_out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\""); _out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
} }
int cellType = cell.getCellType(); CellType cellType = cell.getCellType();
switch (cellType) { switch (cellType) {
case Cell.CELL_TYPE_BLANK: { case BLANK: {
_out.write(">"); _out.write(">");
break; break;
} }
case Cell.CELL_TYPE_FORMULA: { case FORMULA: {
_out.write(">"); _out.write(">");
_out.write("<f>"); _out.write("<f>");
outputQuotedString(cell.getCellFormula()); outputQuotedString(cell.getCellFormula());
_out.write("</f>"); _out.write("</f>");
switch (cell.getCachedFormulaResultType()) { switch (cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
double nval = cell.getNumericCellValue(); double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) { if (!Double.isNaN(nval)) {
_out.write("<v>" + nval + "</v>"); _out.write("<v>" + nval + "</v>");
@ -224,7 +225,7 @@ public class SheetDataWriter {
} }
break; break;
} }
case Cell.CELL_TYPE_STRING: { case STRING: {
if (_sharedStringSource != null) { if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue()); XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst()); int sRef = _sharedStringSource.addEntry(rt.getCTRst());
@ -245,17 +246,17 @@ public class SheetDataWriter {
} }
break; break;
} }
case Cell.CELL_TYPE_NUMERIC: { case NUMERIC: {
_out.write(" t=\"n\">"); _out.write(" t=\"n\">");
_out.write("<v>" + cell.getNumericCellValue() + "</v>"); _out.write("<v>" + cell.getNumericCellValue() + "</v>");
break; break;
} }
case Cell.CELL_TYPE_BOOLEAN: { case BOOLEAN: {
_out.write(" t=\"b\">"); _out.write(" t=\"b\">");
_out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>"); _out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
break; break;
} }
case Cell.CELL_TYPE_ERROR: { case ERROR: {
FormulaError error = FormulaError.forInt(cell.getErrorCellValue()); FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write(" t=\"e\">"); _out.write(" t=\"e\">");

View File

@ -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.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
@ -75,20 +76,21 @@ public abstract class BaseXSSFFormulaEvaluator implements FormulaEvaluator, Work
} }
switch (cell.getCellType()) { switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
return CellValue.valueOf(cell.getBooleanCellValue()); return CellValue.valueOf(cell.getBooleanCellValue());
case XSSFCell.CELL_TYPE_ERROR: case ERROR:
return CellValue.getError(cell.getErrorCellValue()); return CellValue.getError(cell.getErrorCellValue());
case XSSFCell.CELL_TYPE_FORMULA: case FORMULA:
return evaluateFormulaCellValue(cell); return evaluateFormulaCellValue(cell);
case XSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
return new CellValue(cell.getNumericCellValue()); return new CellValue(cell.getNumericCellValue());
case XSSFCell.CELL_TYPE_STRING: case STRING:
return new CellValue(cell.getRichStringCellValue().getString()); return new CellValue(cell.getRichStringCellValue().getString());
case XSSFCell.CELL_TYPE_BLANK: case BLANK:
return null; 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 * @param cell The cell to evaluate
* @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however) * @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however)
*/ */
public int evaluateFormulaCell(Cell cell) { public CellType evaluateFormulaCell(Cell cell) {
if (cell == null || cell.getCellType() != XSSFCell.CELL_TYPE_FORMULA) { if (cell == null || cell.getCellType() != CellType.FORMULA) {
return -1; return CellType._UNINITIALIZED;
} }
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is changed // 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) { protected void doEvaluateInCell(Cell cell) {
if (cell == null) return; if (cell == null) return;
if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { if (cell.getCellType() == CellType.FORMULA) {
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
setCellType(cell, cv); // cell will no longer be a formula cell setCellType(cell, cv); // cell will no longer be a formula cell
setCellValue(cell, cv); setCellValue(cell, cv);
} }
} }
private static void setCellType(Cell cell, CellValue cv) { private static void setCellType(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); CellType cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case XSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
case XSSFCell.CELL_TYPE_ERROR: case ERROR:
case XSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
case XSSFCell.CELL_TYPE_STRING: case STRING:
cell.setCellType(cellType); cell.setCellType(cellType);
return; return;
case XSSFCell.CELL_TYPE_BLANK: case BLANK:
// never happens - blanks eventually get translated to zero // 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 // 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) { private static void setCellValue(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); CellType cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case XSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
cell.setCellValue(cv.getBooleanValue()); cell.setCellValue(cv.getBooleanValue());
break; break;
case XSSFCell.CELL_TYPE_ERROR: case ERROR:
cell.setCellErrorValue(cv.getErrorValue()); cell.setCellErrorValue(cv.getErrorValue());
break; break;
case XSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
cell.setCellValue(cv.getNumberValue()); cell.setCellValue(cv.getNumberValue());
break; break;
case XSSFCell.CELL_TYPE_STRING: case STRING:
cell.setCellValue(new XSSFRichTextString(cv.getStringValue())); cell.setCellValue(new XSSFRichTextString(cv.getStringValue()));
break; break;
case XSSFCell.CELL_TYPE_BLANK: case BLANK:
// never happens - blanks eventually get translated to zero // 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 // this will never happen, we have already evaluated the formula
default: default:
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");

View File

@ -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.Cell;
import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.CellStyle; 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.Comment;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil; 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 FALSE_AS_STRING = "0";
private static final String TRUE_AS_STRING = "1"; 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, * 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) // Copy cell value (cell type is updated implicitly)
if (policy.isCopyCellValue()) { if (policy.isCopyCellValue()) {
if (srcCell != null) { if (srcCell != null) {
int copyCellType = srcCell.getCellType(); CellType copyCellType = srcCell.getCellType();
if (copyCellType == Cell.CELL_TYPE_FORMULA && !policy.isCopyCellFormula()) { if (copyCellType == CellType.FORMULA && !policy.isCopyCellFormula()) {
// Copy formula result as value // Copy formula result as value
// FIXME: Cached value may be stale // FIXME: Cached value may be stale
copyCellType = srcCell.getCachedFormulaResultType(); copyCellType = srcCell.getCachedFormulaResultType();
} }
switch (copyCellType) { switch (copyCellType) {
case Cell.CELL_TYPE_BOOLEAN: case NUMERIC:
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:
// DataFormat is not copied unless policy.isCopyCellStyle is true // DataFormat is not copied unless policy.isCopyCellStyle is true
if (DateUtil.isCellDateFormatted(srcCell)) { if (DateUtil.isCellDateFormatted(srcCell)) {
setCellValue(srcCell.getDateCellValue()); setCellValue(srcCell.getDateCellValue());
@ -160,12 +154,22 @@ public final class XSSFCell implements Cell {
setCellValue(srcCell.getNumericCellValue()); setCellValue(srcCell.getNumericCellValue());
} }
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
setCellValue(srcCell.getStringCellValue()); setCellValue(srcCell.getStringCellValue());
break; break;
case Cell.CELL_TYPE_BLANK: case FORMULA:
setCellFormula(srcCell.getCellFormula());
break;
case BLANK:
setBlank(); setBlank();
break; break;
case BOOLEAN:
setCellValue(srcCell.getBooleanCellValue());
break;
case ERROR:
setCellErrorValue(srcCell.getErrorCellValue());
break;
default: default:
throw new IllegalArgumentException("Invalid cell type " + srcCell.getCellType()); throw new IllegalArgumentException("Invalid cell type " + srcCell.getCellType());
} }
@ -246,21 +250,21 @@ public final class XSSFCell implements Cell {
* </p> * </p>
* @return the value of the cell as a boolean * @return the value of the cell as a boolean
* @throws IllegalStateException if the cell type returned by {@link #getCellType()} * @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 @Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) { switch(cellType) {
case CELL_TYPE_BLANK: case BLANK:
return false; return false;
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); 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 //YK: should throw an exception if requesting boolean value from a non-boolean formula
return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV());
default: 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; * For formulas or error cells we return the precalculated value;
* </p> * </p>
* @return the value of the cell as a number * @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>. * @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. * @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
*/ */
@Override @Override
public double getNumericCellValue() { public double getNumericCellValue() {
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType) { switch(cellType) {
case CELL_TYPE_BLANK: case BLANK:
return 0.0; return 0.0;
case CELL_TYPE_FORMULA: case FORMULA:
case CELL_TYPE_NUMERIC: // fall-through
case NUMERIC:
if(_cell.isSetV()) { if(_cell.isSetV()) {
String v = _cell.getV(); String v = _cell.getV();
if (v.isEmpty()) return 0.0; if (v.isEmpty()) return 0.0;
try { try {
return Double.parseDouble(v); return Double.parseDouble(v);
} catch(NumberFormatException e) { } catch(NumberFormatException e) {
throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false); throw typeMismatch(CellType.NUMERIC, CellType.STRING, false);
} }
} else { } else {
return 0.0; return 0.0;
} }
default: 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 @Override
public XSSFRichTextString getRichStringCellValue() { public XSSFRichTextString getRichStringCellValue() {
int cellType = getCellType(); CellType cellType = getCellType();
XSSFRichTextString rt; XSSFRichTextString rt;
switch (cellType) { switch (cellType) {
case CELL_TYPE_BLANK: case BLANK:
rt = new XSSFRichTextString(""); rt = new XSSFRichTextString("");
break; break;
case CELL_TYPE_STRING: case STRING:
if (_cell.getT() == STCellType.INLINE_STR) { if (_cell.getT() == STCellType.INLINE_STR) {
if(_cell.isSetIs()) { if(_cell.isSetIs()) {
//string is expressed directly in the cell definition instead of implementing the shared string table. //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; break;
case CELL_TYPE_FORMULA: case FORMULA:
checkFormulaCachedValueType(CELL_TYPE_STRING, getBaseCellType(false)); checkFormulaCachedValueType(CellType.STRING, getBaseCellType(false));
rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : ""); rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
break; break;
default: default:
throw typeMismatch(CELL_TYPE_STRING, cellType, false); throw typeMismatch(CellType.STRING, cellType, false);
} }
rt.setStylesTableReference(_stylesSource); rt.setStylesTableReference(_stylesSource);
return rt; return rt;
} }
private static void checkFormulaCachedValueType(int expectedTypeCode, int cachedValueType) { private static void checkFormulaCachedValueType(CellType expectedTypeCode, CellType cachedValueType) {
if (cachedValueType != expectedTypeCode) { if (cachedValueType != expectedTypeCode) {
throw typeMismatch(expectedTypeCode, cachedValueType, true); throw typeMismatch(expectedTypeCode, cachedValueType, true);
} }
@ -432,7 +437,7 @@ public final class XSSFCell implements Cell {
@Override @Override
public void setCellValue(RichTextString str) { public void setCellValue(RichTextString str) {
if(str == null || str.getString() == null){ if(str == null || str.getString() == null){
setCellType(Cell.CELL_TYPE_BLANK); setCellType(CellType.BLANK);
return; 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"); throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
} }
int cellType = getCellType(); CellType cellType = getCellType();
switch(cellType){ switch (cellType){
case Cell.CELL_TYPE_FORMULA: case FORMULA:
_cell.setV(str.getString()); _cell.setV(str.getString());
_cell.setT(STCellType.STR); _cell.setT(STCellType.STR);
break; 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, for example, <code>SUM(C4:E4)</code>
* *
* @return a formula for the cell * @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 @Override
public String getCellFormula() { 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 * @param fpb evaluation workbook for reuse, if available, or null to create a new one as needed
* @return a formula for the cell * @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) { protected String getCellFormula(XSSFEvaluationWorkbook fpb) {
int cellType = getCellType(); CellType cellType = getCellType();
if(cellType != CELL_TYPE_FORMULA) throw typeMismatch(CELL_TYPE_FORMULA, cellType, false); if(cellType != CellType.FORMULA) throw typeMismatch(CellType.FORMULA, cellType, false);
CTCellFormula f = _cell.getF(); CTCellFormula f = _cell.getF();
if (isPartOfArrayFormulaGroup() && f == null) { if (isPartOfArrayFormulaGroup() && f == null) {
@ -660,28 +665,22 @@ public final class XSSFCell implements Cell {
* Return the cell type. * Return the cell type.
* *
* @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 @Override
public int getCellType() { public CellType getCellType() {
if (isFormulaCell()) return CELL_TYPE_FORMULA; if (isFormulaCell()) return CellType.FORMULA;
return getBaseCellType(true); return getBaseCellType(true);
} }
/** /**
* Only valid for formula cells * Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING}, * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula * on the cached value of the formula
*/ */
@Override @Override
public int getCachedFormulaResultType() { public CellType getCachedFormulaResultType() {
if (! isFormulaCell()) { if (! isFormulaCell()) {
throw new IllegalStateException("Only formula cells have cached results"); 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 * 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()) { switch (_cell.getT().intValue()) {
case STCellType.INT_B: case STCellType.INT_B:
return CELL_TYPE_BOOLEAN; return CellType.BOOLEAN;
case STCellType.INT_N: case STCellType.INT_N:
if (!_cell.isSetV() && blankCells) { if (!_cell.isSetV() && blankCells) {
// ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as // 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 // The formula evaluator (and perhaps other clients of this interface) needs to
// distinguish blank values which sometimes get translated into zero and sometimes // distinguish blank values which sometimes get translated into zero and sometimes
// empty string, depending on context // empty string, depending on context
return CELL_TYPE_BLANK; return CellType.BLANK;
} }
return CELL_TYPE_NUMERIC; return CellType.NUMERIC;
case STCellType.INT_E: case STCellType.INT_E:
return CELL_TYPE_ERROR; return CellType.ERROR;
case STCellType.INT_S: // String is in shared strings case STCellType.INT_S: // String is in shared strings
case STCellType.INT_INLINE_STR: // String is inline in cell case STCellType.INT_INLINE_STR: // String is inline in cell
case STCellType.INT_STR: case STCellType.INT_STR:
return CELL_TYPE_STRING; return CellType.STRING;
default: default:
throw new IllegalStateException("Illegal cell type: " + this._cell.getT()); 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. * For strings we throw an exception. For blank cells we return a null.
* </p> * </p>
* @return the value of the cell as a date * @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>. * @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. * @see DataFormatter for formatting this date into a string similar to how excel does.
*/ */
@Override @Override
public Date getDateCellValue() { public Date getDateCellValue() {
if (getCellType() == CELL_TYPE_BLANK) { if (getCellType() == CellType.BLANK) {
return null; return null;
} }
@ -749,7 +748,7 @@ public final class XSSFCell implements Cell {
@Override @Override
public void setCellValue(Date value) { public void setCellValue(Date value) {
if(value == null) { if(value == null) {
setCellType(Cell.CELL_TYPE_BLANK); setCellType(CellType.BLANK);
return; return;
} }
@ -776,7 +775,7 @@ public final class XSSFCell implements Cell {
@Override @Override
public void setCellValue(Calendar value) { public void setCellValue(Calendar value) {
if(value == null) { if(value == null) {
setCellType(Cell.CELL_TYPE_BLANK); setCellType(CellType.BLANK);
return; return;
} }
@ -788,12 +787,12 @@ public final class XSSFCell implements Cell {
* Returns the error message, such as #VALUE! * Returns the error message, such as #VALUE!
* *
* @return 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 * @see FormulaError
*/ */
public String getErrorCellString() { public String getErrorCellString() {
int cellType = getBaseCellType(true); CellType cellType = getBaseCellType(true);
if(cellType != CELL_TYPE_ERROR) throw typeMismatch(CELL_TYPE_ERROR, cellType, false); if(cellType != CellType.ERROR) throw typeMismatch(CellType.ERROR, cellType, false);
return _cell.getV(); return _cell.getV();
} }
@ -805,7 +804,7 @@ public final class XSSFCell implements Cell {
* </p> * </p>
* *
* @return the value of the cell as an error code * @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 * @see FormulaError
*/ */
@Override @Override
@ -881,41 +880,40 @@ public final class XSSFCell implements Cell {
* Set the cells type (numeric, formula or string) * Set the cells type (numeric, formula or string)
* *
* @throws IllegalArgumentException if the specified cell type is invalid * @throws IllegalArgumentException if the specified cell type is invalid
* @see #CELL_TYPE_NUMERIC * @see CellType#NUMERIC
* @see #CELL_TYPE_STRING * @see CellType#STRING
* @see #CELL_TYPE_FORMULA * @see CellType#FORMULA
* @see #CELL_TYPE_BLANK * @see CellType#BLANK
* @see #CELL_TYPE_BOOLEAN * @see CellType#BOOLEAN
* @see #CELL_TYPE_ERROR * @see CellType#ERROR
* @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/ */
@Override @Override
public void setCellType(int cellType) { 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()){ if(isPartOfArrayFormulaGroup()){
notifyArrayFormulaChanging(); notifyArrayFormulaChanging();
} }
if(prevType == CELL_TYPE_FORMULA && cellType != CELL_TYPE_FORMULA) { if(prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
getSheet().getWorkbook().onDeleteFormula(this); getSheet().getWorkbook().onDeleteFormula(this);
} }
switch (cellType) { switch (cellType) {
case CELL_TYPE_BLANK: case NUMERIC:
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:
_cell.setT(STCellType.N); _cell.setT(STCellType.N);
break; break;
case CELL_TYPE_ERROR: case STRING:
_cell.setT(STCellType.E); if(prevType != CellType.STRING){
break;
case CELL_TYPE_STRING:
if(prevType != CELL_TYPE_STRING){
String str = convertCellValueToString(); String str = convertCellValueToString();
XSSFRichTextString rt = new XSSFRichTextString(str); XSSFRichTextString rt = new XSSFRichTextString(str);
rt.setStylesTableReference(_stylesSource); rt.setStylesTableReference(_stylesSource);
@ -924,7 +922,7 @@ public final class XSSFCell implements Cell {
} }
_cell.setT(STCellType.S); _cell.setT(STCellType.S);
break; break;
case CELL_TYPE_FORMULA: case FORMULA:
if(!_cell.isSetF()){ if(!_cell.isSetF()){
CTCellFormula f = CTCellFormula.Factory.newInstance(); CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue("0"); f.setStringValue("0");
@ -932,10 +930,24 @@ public final class XSSFCell implements Cell {
if(_cell.isSetT()) _cell.unsetT(); if(_cell.isSetT()) _cell.unsetT();
} }
break; 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: default:
throw new IllegalArgumentException("Illegal cell type: " + cellType); throw new IllegalArgumentException("Illegal cell type: " + cellType);
} }
if (cellType != CELL_TYPE_FORMULA && _cell.isSetF()) { if (cellType != CellType.FORMULA && _cell.isSetF()) {
_cell.unsetF(); _cell.unsetF();
} }
} }
@ -951,23 +963,23 @@ public final class XSSFCell implements Cell {
@Override @Override
public String toString() { public String toString() {
switch (getCellType()) { switch (getCellType()) {
case CELL_TYPE_BLANK: case NUMERIC:
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:
if (DateUtil.isCellDateFormatted(this)) { if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale()); DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone()); sdf.setTimeZone(LocaleUtil.getUserTimeZone());
return sdf.format(getDateCellValue()); return sdf.format(getDateCellValue());
} }
return Double.toString(getNumericCellValue()); return Double.toString(getNumericCellValue());
case CELL_TYPE_STRING: case STRING:
return getRichStringCellValue().toString(); return getRichStringCellValue().toString();
case FORMULA:
return getCellFormula();
case BLANK:
return "";
case BOOLEAN:
return getBooleanCellValue() ? TRUE : FALSE;
case ERROR:
return ErrorEval.getText(getErrorCellValue());
default: default:
return "Unknown Cell Type: " + getCellType(); return "Unknown Cell Type: " + getCellType();
} }
@ -989,28 +1001,12 @@ public final class XSSFCell implements Cell {
return _cell.getV(); 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 * Used to help format error messages
*/ */
private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) { private static RuntimeException typeMismatch(CellType expectedType, CellType actualType, boolean isFormulaCell) {
String msg = "Cannot get a " String msg = "Cannot get a " + expectedType + " value from a " + actualType+ " " + (isFormulaCell ? "formula " : "") + "cell";
+ getCellTypeName(expectedTypeCode) + " value from a "
+ getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg); 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 * TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
*/ */
private boolean convertCellValueToBoolean() { private boolean convertCellValueToBoolean() {
int cellType = getCellType(); CellType cellType = getCellType();
if (cellType == CELL_TYPE_FORMULA) { if (cellType == CellType.FORMULA) {
cellType = getBaseCellType(false); cellType = getBaseCellType(false);
} }
switch (cellType) { switch (cellType) {
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return TRUE_AS_STRING.equals(_cell.getV()); return TRUE_AS_STRING.equals(_cell.getV());
case CELL_TYPE_STRING: case STRING:
int sstIndex = Integer.parseInt(_cell.getV()); int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
String text = rt.getString(); String text = rt.getString();
return Boolean.parseBoolean(text); return Boolean.parseBoolean(text);
case CELL_TYPE_NUMERIC: case NUMERIC:
return Double.parseDouble(_cell.getV()) != 0; return Double.parseDouble(_cell.getV()) != 0;
case CELL_TYPE_ERROR: case ERROR:
case CELL_TYPE_BLANK: // fall-through
case BLANK:
return false; return false;
default:
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
private String convertCellValueToString() { private String convertCellValueToString() {
int cellType = getCellType(); CellType cellType = getCellType();
switch (cellType) { switch (cellType) {
case CELL_TYPE_BLANK: case BLANK:
return ""; return "";
case CELL_TYPE_BOOLEAN: case BOOLEAN:
return TRUE_AS_STRING.equals(_cell.getV()) ? "TRUE" : "FALSE"; return TRUE_AS_STRING.equals(_cell.getV()) ? TRUE : FALSE;
case CELL_TYPE_STRING: case STRING:
int sstIndex = Integer.parseInt(_cell.getV()); int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
return rt.getString(); return rt.getString();
case CELL_TYPE_NUMERIC: case NUMERIC:
case CELL_TYPE_ERROR: case ERROR:
return _cell.getV(); return _cell.getV();
case CELL_TYPE_FORMULA: case FORMULA:
// should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
// just use cached formula result instead // just use cached formula result instead
break; break;
@ -1186,21 +1185,27 @@ public final class XSSFCell implements Cell {
cellType = getBaseCellType(false); cellType = getBaseCellType(false);
String textValue = _cell.getV(); String textValue = _cell.getV();
switch (cellType) { switch (cellType) {
case CELL_TYPE_BOOLEAN: case BOOLEAN:
if (TRUE_AS_STRING.equals(textValue)) { if (TRUE_AS_STRING.equals(textValue)) {
return "TRUE"; return TRUE;
} }
if (FALSE_AS_STRING.equals(textValue)) { if (FALSE_AS_STRING.equals(textValue)) {
return "FALSE"; return FALSE;
} }
throw new IllegalStateException("Unexpected boolean cached formula value '" throw new IllegalStateException("Unexpected boolean cached formula value '"
+ textValue + "'."); + textValue + "'.");
case CELL_TYPE_STRING:
case CELL_TYPE_NUMERIC: case STRING:
case CELL_TYPE_ERROR: // fall-through
case NUMERIC:
// fall-through
case ERROR:
return textValue; return textValue;
default:
throw new IllegalStateException("Unexpected formula result type (" + cellType + ")");
} }
throw new IllegalStateException("Unexpected formula result type (" + cellType + ")");
} }
@Override @Override

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.formula.EvaluationCell; import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet; import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.usermodel.CellType;
/** /**
* XSSF wrapper for a cell under evaluation * XSSF wrapper for a cell under evaluation
@ -37,6 +38,7 @@ final class XSSFEvaluationCell implements EvaluationCell {
this(cell, new XSSFEvaluationSheet(cell.getSheet())); this(cell, new XSSFEvaluationSheet(cell.getSheet()));
} }
@Override
public Object getIdentityKey() { public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key // save memory by just using the cell itself as the identity key
// Note - this assumes XSSFCell has not overridden hashCode and equals // Note - this assumes XSSFCell has not overridden hashCode and equals
@ -46,31 +48,40 @@ final class XSSFEvaluationCell implements EvaluationCell {
public XSSFCell getXSSFCell() { public XSSFCell getXSSFCell() {
return _cell; return _cell;
} }
@Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
return _cell.getBooleanCellValue(); return _cell.getBooleanCellValue();
} }
public int getCellType() { @Override
public CellType getCellType() {
return _cell.getCellType(); return _cell.getCellType();
} }
@Override
public int getColumnIndex() { public int getColumnIndex() {
return _cell.getColumnIndex(); return _cell.getColumnIndex();
} }
@Override
public int getErrorCellValue() { public int getErrorCellValue() {
return _cell.getErrorCellValue(); return _cell.getErrorCellValue();
} }
@Override
public double getNumericCellValue() { public double getNumericCellValue() {
return _cell.getNumericCellValue(); return _cell.getNumericCellValue();
} }
@Override
public int getRowIndex() { public int getRowIndex() {
return _cell.getRowIndex(); return _cell.getRowIndex();
} }
@Override
public EvaluationSheet getSheet() { public EvaluationSheet getSheet() {
return _evalSheet; return _evalSheet;
} }
@Override
public String getStringCellValue() { public String getStringCellValue() {
return _cell.getRichStringCellValue().getString(); return _cell.getRichStringCellValue().getString();
} }
public int getCachedFormulaResultType() { @Override
public CellType getCachedFormulaResultType() {
return _cell.getCachedFormulaResultType(); return _cell.getCachedFormulaResultType();
} }
} }

View File

@ -27,6 +27,7 @@ import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.CellStyle; 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.Row;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; 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. * Use this to create new cells within the row and return it.
* <p> * <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>. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
* </p> * </p>
* @param columnIndex - the column number this cell represents * @param columnIndex - the column number this cell represents
@ -194,7 +195,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
*/ */
@Override @Override
public XSSFCell createCell(int columnIndex) { 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. * @return XSSFCell a high level representation of the created cell.
* @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 * @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) * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx)
* @see Cell#CELL_TYPE_BLANK * @see CellType#BLANK
* @see Cell#CELL_TYPE_BOOLEAN * @see CellType#BOOLEAN
* @see Cell#CELL_TYPE_ERROR * @see CellType#ERROR
* @see Cell#CELL_TYPE_FORMULA * @see CellType#FORMULA
* @see Cell#CELL_TYPE_NUMERIC * @see CellType#NUMERIC
* @see Cell#CELL_TYPE_STRING * @see CellType#STRING
* @deprecated POI 3.15 beta 3. Use {@link #createCell(int, CellType)} instead.
*/ */
@Override @Override
public XSSFCell createCell(int columnIndex, int type) { 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 // 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 final Integer colI = new Integer(columnIndex); // NOSONAR
CTCell ctCell; CTCell ctCell;
@ -226,8 +241,8 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
} }
XSSFCell xcell = new XSSFCell(this, ctCell); XSSFCell xcell = new XSSFCell(this, ctCell);
xcell.setCellNum(columnIndex); xcell.setCellNum(columnIndex);
if (type != Cell.CELL_TYPE_BLANK) { if (type != CellType.BLANK) {
xcell.setCellType(type); xcell.setCellType(type);
} }
_cells.put(colI, xcell); _cells.put(colI, xcell);
return xcell; return xcell;
@ -261,10 +276,10 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
case RETURN_NULL_AND_BLANK: case RETURN_NULL_AND_BLANK:
return cell; return cell;
case RETURN_BLANK_AS_NULL: 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; return (isBlank) ? null : cell;
case CREATE_NULL_AS_BLANK: case CREATE_NULL_AS_BLANK:
return (cell == null) ? createCell(cellnum, Cell.CELL_TYPE_BLANK) : cell; return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
default: default:
throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
} }
@ -481,7 +496,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
if(xcell.isPartOfArrayFormulaGroup()) { if(xcell.isPartOfArrayFormulaGroup()) {
xcell.notifyArrayFormulaChanging(); xcell.notifyArrayFormulaChanging();
} }
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if(cell.getCellType() == CellType.FORMULA) {
_sheet.getWorkbook().onDeleteFormula(xcell); _sheet.getWorkbook().onDeleteFormula(xcell);
} }
// Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory

View File

@ -58,13 +58,14 @@ public class TestCellFormatPart extends CellFormatTestBase {
public void testGeneralFormat() throws Exception { public void testGeneralFormat() throws Exception {
runFormatTests("GeneralFormatTests.xlsx", new CellValue() { runFormatTests("GeneralFormatTests.xlsx", new CellValue() {
public Object getValue(Cell cell) { public Object getValue(Cell cell) {
int type = CellFormat.ultimateType(cell); switch (CellFormat.ultimateType(cell)) {
if (type == Cell.CELL_TYPE_BOOLEAN) case BOOLEAN:
return cell.getBooleanCellValue(); return cell.getBooleanCellValue();
else if (type == Cell.CELL_TYPE_NUMERIC) case NUMERIC:
return cell.getNumericCellValue(); return cell.getNumericCellValue();
else default:
return cell.getStringCellValue(); return cell.getStringCellValue();
}
} }
}); });
} }
@ -125,10 +126,12 @@ public class TestCellFormatPart extends CellFormatTestBase {
public void testTextFormat() throws Exception { public void testTextFormat() throws Exception {
runFormatTests("TextFormatTests.xlsx", new CellValue() { runFormatTests("TextFormatTests.xlsx", new CellValue() {
public Object getValue(Cell cell) { public Object getValue(Cell cell) {
if (CellFormat.ultimateType(cell) == Cell.CELL_TYPE_BOOLEAN) switch(CellFormat.ultimateType(cell)) {
return cell.getBooleanCellValue(); case BOOLEAN:
else return cell.getBooleanCellValue();
return cell.getStringCellValue(); default:
return cell.getStringCellValue();
}
} }
}); });
} }

View File

@ -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.eval.TestFormulasFromSpreadsheet;
import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.formula.functions.TestMathX;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row; 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++) { for (short colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) {
Cell c = formulasRow.getCell(colnum); Cell c = formulasRow.getCell(colnum);
assumeNotNull(c); assumeNotNull(c);
assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); assumeTrue(c.getCellType() == CellType.FORMULA);
ignoredFormulaTestCase(c.getCellFormula()); ignoredFormulaTestCase(c.getCellFormula());
CellValue actValue = evaluator.evaluate(c); 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 + " - Bad setup data expected value is null", expValue);
assertNotNull(msg + " - actual value was null", actValue); assertNotNull(msg + " - actual value was null", actValue);
switch (expValue.getCellType()) { final CellType expectedCellType = expValue.getCellType();
case Cell.CELL_TYPE_BLANK: switch (expectedCellType) {
assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType()); case BLANK:
assertEquals(msg, CellType.BLANK, actValue.getCellType());
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType()); assertEquals(msg, CellType.BOOLEAN, actValue.getCellType());
assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue()); assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType()); assertEquals(msg, CellType.ERROR, actValue.getCellType());
// if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values // if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values
// assertEquals(msg, expValue.getErrorCellValue(), actValue.getErrorValue()); // assertEquals(msg, expValue.getErrorCellValue(), actValue.getErrorValue());
// } // }
break; 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); fail("Cannot expect formula as result of formula evaluation: " + msg);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType()); assertEquals(msg, CellType.NUMERIC, actValue.getCellType());
TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
// double delta = Math.abs(expValue.getNumericCellValue()-actValue.getNumberValue()); // double delta = Math.abs(expValue.getNumericCellValue()-actValue.getNumberValue());
// double pctExpValue = Math.abs(0.00001*expValue.getNumericCellValue()); // double pctExpValue = Math.abs(0.00001*expValue.getNumericCellValue());
// assertTrue(msg, delta <= pctExpValue); // assertTrue(msg, delta <= pctExpValue);
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType()); assertEquals(msg, CellType.STRING, actValue.getCellType());
assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue()); assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue());
break; 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"); logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
} }

View File

@ -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.eval.TestFormulasFromSpreadsheet;
import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.formula.functions.TestMathX;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -175,7 +176,7 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF {
Cell c = r.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE); Cell c = r.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE);
assumeNotNull(c); assumeNotNull(c);
assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); assumeTrue(c.getCellType() == CellType.FORMULA);
CellValue actValue = evaluator.evaluate(c); CellValue actValue = evaluator.evaluate(c);
@ -184,33 +185,36 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF {
assertNotNull(msg + " - actual value was null", actValue); assertNotNull(msg + " - actual value was null", actValue);
switch (expValue.getCellType()) { final CellType expectedCellType = expValue.getCellType();
case Cell.CELL_TYPE_BLANK: switch (expectedCellType) {
assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType()); case BLANK:
assertEquals(msg, CellType.BLANK, actValue.getCellType());
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType()); assertEquals(msg, CellType.BOOLEAN, actValue.getCellType());
assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue()); assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType()); assertEquals(msg, CellType.ERROR, actValue.getCellType());
// if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values // if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values
// assertEquals(msg, expected.getErrorCellValue(), actual.getErrorValue()); // assertEquals(msg, expected.getErrorCellValue(), actual.getErrorValue());
// } // }
break; 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); fail("Cannot expect formula as result of formula evaluation: " + msg);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType()); assertEquals(msg, CellType.NUMERIC, actValue.getCellType());
TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
// double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue()); // double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue());
// double pctExpected = Math.abs(0.00001*expected.getNumericCellValue()); // double pctExpected = Math.abs(0.00001*expected.getNumericCellValue());
// assertTrue(msg, delta <= pctExpected); // assertTrue(msg, delta <= pctExpected);
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType()); assertEquals(msg, CellType.STRING, actValue.getCellType());
assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue()); assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue());
break; 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"); logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); 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"); logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_TEST_NAME + ", can't figure out test name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
} }

View File

@ -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.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.Function; import org.apache.poi.ss.formula.functions.Function;
import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; import org.apache.poi.ss.usermodel.*;
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.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
@ -322,10 +300,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
Sheet s = wb.getSheetAt(i); Sheet s = wb.getSheetAt(i);
for(Row r : s) { for(Row r : s) {
for(Cell c : r) { for(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) { if(c.getCellType() == CellType.FORMULA) {
CellValue cv = eval.evaluate(c); CellValue cv = eval.evaluate(c);
if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) { if(cv.getCellType() == CellType.NUMERIC) {
// assert that the calculated value agrees with // assert that the calculated value agrees with
// the cached formula result calculated by Excel // the cached formula result calculated by Excel
String formula = c.getCellFormula(); String formula = c.getCellFormula();
@ -446,7 +424,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
cell = sheet.getRow(0).getCell(0); cell = sheet.getRow(0).getCell(0);
assertEquals("#REF!*#REF!", cell.getCellFormula()); 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()); assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
Name nm1 = wb.getName("sale_1"); Name nm1 = wb.getName("sale_1");
@ -458,7 +436,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
cell = sheet.getRow(1).getCell(0); cell = sheet.getRow(1).getCell(0);
assertEquals("sale_1*sale_2", cell.getCellFormula()); 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()); assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
wb.close(); wb.close();
@ -606,10 +584,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
// Otherwise should go // Otherwise should go
sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
sheet.getRow(2).getCell(0).setCellFormula(null); // go 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(); 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(); XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
validateCells(sheet); validateCells(sheet);
@ -617,7 +595,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
validateCells(sheet); validateCells(sheet);
XSSFTestDataSamples.writeOutAndReadBack(wb1).close(); 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(); XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
sheet.getRow(7).getCell(0).setCellValue((String) null); // go sheet.getRow(7).getCell(0).setCellValue((String) null); // go
@ -664,7 +642,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
Sheet sheet = wb.getSheetAt(0); Sheet sheet = wb.getSheetAt(0);
for(Row row : sheet){ for(Row row : sheet){
for(Cell cell : row){ for(Cell cell : row){
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){ if(cell.getCellType() == CellType.FORMULA){
formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells 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 // Get wrong cell by row 8 & column 7
Cell cell = sheet.getRow(8).getCell(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> // Check the value - will be zero as it is <c><v/></c>
assertEquals(0.0, cell.getNumericCellValue(), 0.001); assertEquals(0.0, cell.getNumericCellValue(), 0.001);
@ -2204,11 +2182,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
Sheet sheet = wb.getSheet("Sheet1"); Sheet sheet = wb.getSheet("Sheet1");
Cell cell = sheet.getRow(5).getCell(4); Cell cell = sheet.getRow(5).getCell(4);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("E4+E5", cell.getCellFormula()); assertEquals("E4+E5", cell.getCellFormula());
CellValue value = evaluator.evaluate(cell); CellValue value = evaluator.evaluate(cell);
assertEquals(Cell.CELL_TYPE_ERROR, value.getCellType()); assertEquals(CellType.ERROR, value.getCellType());
assertEquals(-60, value.getErrorValue()); assertEquals(-60, value.getErrorValue());
assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString()); assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString());
assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString()); assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString());
@ -2563,7 +2541,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
if(cell == null){ if(cell == null){
cell = row.createCell(cellnum); cell = row.createCell(cellnum);
} else { } else {
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if(cell.getCellType() == CellType.FORMULA) {
cell.setCellFormula(null); cell.setCellFormula(null);
cell.getCellStyle().setDataFormat((short) 0); 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) { 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) { if (null == expectedResultOrNull) {
assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType()); assertEquals(CellType.ERROR, intF.getCachedFormulaResultType());
expectedResultOrNull = "#VALUE!"; expectedResultOrNull = "#VALUE!";
} }
else { else {
assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType());
} }
assertEquals(expectedFormula, intF.getCellFormula()); assertEquals(expectedFormula, intF.getCellFormula());
@ -2676,7 +2654,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
Sheet sheet = wb.getSheet("Sheet1"); Sheet sheet = wb.getSheet("Sheet1");
for(Row aRow : sheet) { for(Row aRow : sheet) {
Cell cell = aRow.getCell(1); Cell cell = aRow.getCell(1);
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if(cell.getCellType() == CellType.FORMULA) {
String formula = cell.getCellFormula(); String formula = cell.getCellFormula();
//System.out.println("formula: " + formula); //System.out.println("formula: " + formula);
assertNotNull(formula); assertNotNull(formula);
@ -2980,16 +2958,16 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
row = worksheet.getRow(2); row = worksheet.getRow(2);
cell = row.getCell(1); cell = row.getCell(1);
assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); assertEquals(CellType.BLANK, cell.getCellType());
assertEquals(-1, evaluator.evaluateFormulaCell(cell)); assertEquals(CellType._UNINITIALIZED, evaluator.evaluateFormulaCell(cell));
// A3 // A3
row = worksheet.getRow(2); row = worksheet.getRow(2);
cell = row.getCell(0); cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("IF(ISBLANK(B3),\"\",B3)", cell.getCellFormula()); 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); CellValue value = evaluator.evaluate(cell);
assertEquals("", value.getStringValue()); assertEquals("", value.getStringValue());
@ -2997,9 +2975,9 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
row = worksheet.getRow(4); row = worksheet.getRow(4);
cell = row.getCell(0); cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula()); assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula());
assertEquals(Cell.CELL_TYPE_NUMERIC, evaluator.evaluateFormulaCell(cell)); assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCell(cell));
value = evaluator.evaluate(cell); value = evaluator.evaluate(cell);
assertEquals(1.0, value.getNumberValue(), 0.1); assertEquals(1.0, value.getNumberValue(), 0.1);

View File

@ -123,31 +123,31 @@ public abstract class AbstractExcelConverter
final String value; final String value;
switch ( cell.getCellType() ) switch ( cell.getCellType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
// XXX: enrich // XXX: enrich
value = cell.getRichStringCellValue().getString(); value = cell.getRichStringCellValue().getString();
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
switch ( cell.getCachedFormulaResultType() ) switch ( cell.getCachedFormulaResultType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
HSSFRichTextString str = cell.getRichStringCellValue(); HSSFRichTextString str = cell.getRichStringCellValue();
if ( str == null || str.length() <= 0 ) if ( str == null || str.length() <= 0 )
return false; return false;
value = str.toString(); value = str.toString();
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
HSSFCellStyle style = cell.getCellStyle(); HSSFCellStyle style = cell.getCellStyle();
double nval = cell.getNumericCellValue(); double nval = cell.getNumericCellValue();
short df = style.getDataFormat(); short df = style.getDataFormat();
String dfs = style.getDataFormatString(); String dfs = style.getDataFormatString();
value = _formatter.formatRawCellContents(nval, df, dfs); value = _formatter.formatRawCellContents(nval, df, dfs);
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = String.valueOf( cell.getBooleanCellValue() ); value = String.valueOf( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:
@ -155,16 +155,16 @@ public abstract class AbstractExcelConverter
break; break;
} }
break; break;
case HSSFCell.CELL_TYPE_BLANK: case BLANK:
value = ExcelToHtmlUtils.EMPTY; value = ExcelToHtmlUtils.EMPTY;
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
value = _formatter.formatCellValue( cell ); value = _formatter.formatCellValue( cell );
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = String.valueOf( cell.getBooleanCellValue() ); value = String.valueOf( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:

View File

@ -209,14 +209,14 @@ public class ExcelToFoConverter extends AbstractExcelConverter
String value; String value;
switch ( cell.getCellType() ) switch ( cell.getCellType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
// XXX: enrich // XXX: enrich
value = cell.getRichStringCellValue().getString(); value = cell.getRichStringCellValue().getString();
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
switch ( cell.getCachedFormulaResultType() ) switch ( cell.getCachedFormulaResultType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
HSSFRichTextString str = cell.getRichStringCellValue(); HSSFRichTextString str = cell.getRichStringCellValue();
if ( str != null && str.length() > 0 ) if ( str != null && str.length() > 0 )
{ {
@ -227,16 +227,16 @@ public class ExcelToFoConverter extends AbstractExcelConverter
value = ExcelToHtmlUtils.EMPTY; value = ExcelToHtmlUtils.EMPTY;
} }
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
double nValue = cell.getNumericCellValue(); double nValue = cell.getNumericCellValue();
short df = cellStyle.getDataFormat(); short df = cellStyle.getDataFormat();
String dfs = cellStyle.getDataFormatString(); String dfs = cellStyle.getDataFormatString();
value = _formatter.formatRawCellContents(nValue, df, dfs ); value = _formatter.formatRawCellContents(nValue, df, dfs );
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = Boolean.toString( cell.getBooleanCellValue() ); value = Boolean.toString( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:
@ -248,16 +248,16 @@ public class ExcelToFoConverter extends AbstractExcelConverter
break; break;
} }
break; break;
case HSSFCell.CELL_TYPE_BLANK: case BLANK:
value = ExcelToHtmlUtils.EMPTY; value = ExcelToHtmlUtils.EMPTY;
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
value = _formatter.formatCellValue( cell ); value = _formatter.formatCellValue( cell );
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = Boolean.toString( cell.getBooleanCellValue() ); value = Boolean.toString( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:

View File

@ -296,14 +296,14 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
String value; String value;
switch ( cell.getCellType() ) switch ( cell.getCellType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
// XXX: enrich // XXX: enrich
value = cell.getRichStringCellValue().getString(); value = cell.getRichStringCellValue().getString();
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case FORMULA:
switch ( cell.getCachedFormulaResultType() ) switch ( cell.getCachedFormulaResultType() )
{ {
case HSSFCell.CELL_TYPE_STRING: case STRING:
HSSFRichTextString str = cell.getRichStringCellValue(); HSSFRichTextString str = cell.getRichStringCellValue();
if ( str != null && str.length() > 0 ) if ( str != null && str.length() > 0 )
{ {
@ -314,16 +314,16 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
value = ExcelToHtmlUtils.EMPTY; value = ExcelToHtmlUtils.EMPTY;
} }
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
double nValue = cell.getNumericCellValue(); double nValue = cell.getNumericCellValue();
short df = cellStyle.getDataFormat(); short df = cellStyle.getDataFormat();
String dfs = cellStyle.getDataFormatString(); String dfs = cellStyle.getDataFormatString();
value = _formatter.formatRawCellContents(nValue, df, dfs); value = _formatter.formatRawCellContents(nValue, df, dfs);
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = String.valueOf( cell.getBooleanCellValue() ); value = String.valueOf( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:
@ -335,16 +335,16 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
break; break;
} }
break; break;
case HSSFCell.CELL_TYPE_BLANK: case BLANK:
value = ExcelToHtmlUtils.EMPTY; value = ExcelToHtmlUtils.EMPTY;
break; break;
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
value = _formatter.formatCellValue( cell ); value = _formatter.formatCellValue( cell );
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case BOOLEAN:
value = String.valueOf( cell.getBooleanCellValue() ); value = String.valueOf( cell.getBooleanCellValue() );
break; break;
case HSSFCell.CELL_TYPE_ERROR: case ERROR:
value = ErrorEval.getText( cell.getErrorCellValue() ); value = ErrorEval.getText( cell.getErrorCellValue() );
break; break;
default: default:

View File

@ -17,12 +17,12 @@
package org.apache.poi.hssf.record; 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.AttrPtg;
import org.apache.poi.ss.formula.ptg.FuncVarPtg; import org.apache.poi.ss.formula.ptg.FuncVarPtg;
import org.apache.poi.ss.formula.ptg.IntPtg; import org.apache.poi.ss.formula.ptg.IntPtg;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefPtg; import org.apache.poi.ss.formula.ptg.RefPtg;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.FormulaError;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
@ -83,7 +83,7 @@ public final class TestFormulaRecord extends TestCase {
FormulaRecord record = new FormulaRecord(TestcaseRecordInputStream.create(FormulaRecord.sid, formulaByte)); FormulaRecord record = new FormulaRecord(TestcaseRecordInputStream.create(FormulaRecord.sid, formulaByte));
assertEquals("Row", 0, record.getRow()); assertEquals("Row", 0, record.getRow());
assertEquals("Column", 0, record.getColumn()); assertEquals("Column", 0, record.getColumn());
assertEquals(HSSFCell.CELL_TYPE_ERROR, record.getCachedResultType()); assertEquals(CellType.ERROR.getCode(), record.getCachedResultType());
byte[] output = record.serialize(); byte[] output = record.serialize();
assertEquals("Output size", 33, output.length); //includes sid+recordlength assertEquals("Output size", 33, output.length); //includes sid+recordlength

View File

@ -72,6 +72,7 @@ import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; 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.ClientAnchor.AnchorType;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
@ -173,7 +174,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
cell = row.createCell(3); cell = row.createCell(3);
// Write test // Write test
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
setCellText(cell, "a test"); setCellText(cell, "a test");
// change existing numeric cell value // change existing numeric cell value
@ -468,7 +469,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
HSSFRow row = sheet.getRow(i); HSSFRow row = sheet.getRow(i);
if (row != null) { if (row != null) {
HSSFCell cell = row .getCell(0); HSSFCell cell = row .getCell(0);
assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(CellType.STRING, cell.getCellType());
count++; count++;
} }
} }
@ -1111,7 +1112,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
c3.getNumericCellValue(); c3.getNumericCellValue();
fail("exception should have been thrown"); fail("exception should have been thrown");
} catch (IllegalStateException e) { } 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) { private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType());
assertEquals(expectedValue, cell.getNumericCellValue(), 0.0); assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
} }
private static void confirmCachedValue(String expectedValue, HSSFCell cell) { private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals(Cell.CELL_TYPE_STRING, cell.getCachedFormulaResultType()); assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
assertEquals(expectedValue, cell.getRichStringCellValue().getString()); assertEquals(expectedValue, cell.getRichStringCellValue().getString());
} }
@ -1287,7 +1288,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
s = wb.getSheet("OneVariable Table Completed"); s = wb.getSheet("OneVariable Table Completed");
r = s.getRow(3); r = s.getRow(3);
c = r.getCell(4); c = r.getCell(4);
assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); assertEquals(CellType.FORMULA, c.getCellType());
// TODO - check the formula once tables and // TODO - check the formula once tables and
// arrays are properly supported // arrays are properly supported
@ -1297,7 +1298,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
s = wb.getSheet("TwoVariable Table Example"); s = wb.getSheet("TwoVariable Table Example");
r = s.getRow(3); r = s.getRow(3);
c = r.getCell(4); c = r.getCell(4);
assertEquals(Cell.CELL_TYPE_FORMULA, c.getCellType()); assertEquals(CellType.FORMULA, c.getCellType());
// TODO - check the formula once tables and // TODO - check the formula once tables and
// arrays are properly supported // arrays are properly supported
@ -1823,26 +1824,26 @@ public final class TestBugs extends BaseTestBugzillaIssues {
HSSFRow row; HSSFRow row;
row = s.getRow(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); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(1); 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("B1", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(2); 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("Sheet1!B1", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(3); 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("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(4); 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("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
@ -1852,7 +1853,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
// Add 5 // Add 5
row = s.createRow(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).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1");
row.getCell(1).setCellValue(234.0); row.getCell(1).setCellValue(234.0);
@ -1863,32 +1864,32 @@ public final class TestBugs extends BaseTestBugzillaIssues {
s = wb2.getSheetAt(0); s = wb2.getSheetAt(0);
row = s.getRow(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); assertEquals(112.0, row.getCell(1).getNumericCellValue(),0);
row = s.getRow(1); 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("B1", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(2); 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("Sheet1!B1", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(3); 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("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
// TODO - Fix these so they work... // TODO - Fix these so they work...
/*row = s.getRow(4); /*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("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0); assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0);
row = s.getRow(5); 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("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/ assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/
@ -2087,13 +2088,13 @@ public final class TestBugs extends BaseTestBugzillaIssues {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Bug50416"); Sheet sheet = workbook.createSheet("Bug50416");
Row row1 = sheet.createRow(0); 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"); cellA_1.setCellValue("Cell A,1");
Row row2 = sheet.createRow(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"); cellA_2.setCellValue("Cell A,2");
Row row3 = sheet.createRow(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"); cellA_3.setCellValue("Cell A,3");
// Test the last Row number it currently correct // 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(2).setCellValue(cal);
row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3)); row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3));
row.createCell(4).setCellValue(true); 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."); 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) { 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) { if (null == expectedResultOrNull) {
assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType()); assertEquals(CellType.ERROR, intF.getCachedFormulaResultType());
expectedResultOrNull = "#VALUE!"; expectedResultOrNull = "#VALUE!";
} }
else { else {
assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType());
} }
assertEquals(expectedFormula, intF.getCellFormula()); assertEquals(expectedFormula, intF.getCellFormula());
@ -2986,12 +2987,12 @@ public final class TestBugs extends BaseTestBugzillaIssues {
Sheet sheet = wb.getSheetAt(0); Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0); Row row = sheet.getRow(0);
Cell cell = row.getCell(0); Cell cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula()); assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula());
assertEquals("", cell.getStringCellValue()); 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 { try {
assertNull(cell.getCellFormula()); assertNull(cell.getCellFormula());
fail("Should throw an exception here"); fail("Should throw an exception here");

View File

@ -36,6 +36,7 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord; import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.ss.usermodel.BaseTestCell; import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.Cell; 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.FormulaError;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -417,14 +418,7 @@ public final class TestHSSFCell extends BaseTestCell {
cell.getCachedFormulaResultType(); cell.getCachedFormulaResultType();
fail("Should catch exception"); fail("Should catch exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// expected here // 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
} }
cell.removeCellComment(); cell.removeCellComment();
@ -440,36 +434,36 @@ public final class TestHSSFCell extends BaseTestCell {
Row row = sheet.createRow(0); Row row = sheet.createRow(0);
Cell cell = row.createCell(0); Cell cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_BLANK); cell.setCellType(CellType.BLANK);
assertNull(null, cell.getDateCellValue()); assertNull(null, cell.getDateCellValue());
assertFalse(cell.getBooleanCellValue()); assertFalse(cell.getBooleanCellValue());
assertEquals("", cell.toString()); assertEquals("", cell.toString());
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
assertEquals("", cell.toString()); assertEquals("", cell.toString());
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(1.2); cell.setCellValue(1.2);
cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellType(CellType.NUMERIC);
assertEquals("1.2", cell.toString()); assertEquals("1.2", cell.toString());
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
assertEquals("TRUE", cell.toString()); assertEquals("TRUE", cell.toString());
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
cell.setCellValue("" + FormulaError.VALUE.name()); cell.setCellValue("" + FormulaError.VALUE.name());
cell.setCellType(Cell.CELL_TYPE_ERROR); cell.setCellType(CellType.ERROR);
assertEquals("#VALUE!", cell.toString()); assertEquals("#VALUE!", cell.toString());
cell.setCellType(Cell.CELL_TYPE_ERROR); cell.setCellType(CellType.ERROR);
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
assertEquals("FALSE", cell.toString()); assertEquals("FALSE", cell.toString());
cell.setCellValue(1.2); cell.setCellValue(1.2);
cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellType(CellType.NUMERIC);
assertEquals("1.2", cell.toString()); assertEquals("1.2", cell.toString());
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
cell.setCellType(Cell.CELL_TYPE_ERROR); cell.setCellType(CellType.ERROR);
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(1.2); cell.setCellValue(1.2);
cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellType(CellType.NUMERIC);
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
assertEquals("1.2", cell.toString()); assertEquals("1.2", cell.toString());
cell.setCellValue((String)null); cell.setCellValue((String)null);

View File

@ -30,7 +30,7 @@ import org.apache.poi.hssf.record.BackupRecord;
import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; 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.Name;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; 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 * 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> * 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> * 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 @Test
public void testReadSheetWithRK() throws IOException { public void testReadSheetWithRK() throws IOException {
HSSFWorkbook wb = openSample("rk.xls"); HSSFWorkbook wb = openSample("rk.xls");
HSSFSheet s = wb.getSheetAt(0); HSSFSheet s = wb.getSheetAt(0);
HSSFCell c = s.getRow(0).getCell(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(); wb.close();
} }

View File

@ -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.Ptg;
import org.apache.poi.ss.formula.ptg.RefErrorPtg; import org.apache.poi.ss.formula.ptg.RefErrorPtg;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Name;
@ -200,7 +201,7 @@ public class TestWorkbookEvaluator {
} catch (RuntimeException e) { } catch (RuntimeException e) {
fail("Missing arg result not being handled correctly."); 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 // adding blank to 1.0 gives 1.0
assertEquals(1.0, cv.getNumberValue(), 0.0); assertEquals(1.0, cv.getNumberValue(), 0.0);
@ -208,7 +209,7 @@ public class TestWorkbookEvaluator {
cell.setCellFormula("\"abc\"&IF(1,,)"); cell.setCellFormula("\"abc\"&IF(1,,)");
fe.notifySetFormula(cell); fe.notifySetFormula(cell);
cv = fe.evaluate(cell); cv = fe.evaluate(cell);
assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType()); assertEquals(CellType.STRING, cv.getCellType());
// adding blank to "abc" gives "abc" // adding blank to "abc" gives "abc"
assertEquals("abc", cv.getStringValue()); assertEquals("abc", cv.getStringValue());
@ -216,7 +217,7 @@ public class TestWorkbookEvaluator {
cell.setCellFormula("\"abc\"&CHOOSE(2,5,,9)"); cell.setCellFormula("\"abc\"&CHOOSE(2,5,,9)");
fe.notifySetFormula(cell); fe.notifySetFormula(cell);
cv = fe.evaluate(cell); cv = fe.evaluate(cell);
assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType()); assertEquals(CellType.STRING, cv.getCellType());
// adding blank to "abc" gives "abc" // adding blank to "abc" gives "abc"
assertEquals("abc", cv.getStringValue()); assertEquals("abc", cv.getStringValue());
} }
@ -242,14 +243,14 @@ public class TestWorkbookEvaluator {
} }
throw new RuntimeException(e); throw new RuntimeException(e);
} }
assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType()); assertEquals(CellType.ERROR, cv.getCellType());
assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv.getErrorValue()); assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv.getErrorValue());
// verify circular refs are still detected properly // verify circular refs are still detected properly
fe.clearAllCachedResultValues(); fe.clearAllCachedResultValues();
cell.setCellFormula("OFFSET(A1,0,0)"); cell.setCellFormula("OFFSET(A1,0,0)");
cv = fe.evaluate(cell); cv = fe.evaluate(cell);
assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType()); assertEquals(CellType.ERROR, cv.getCellType());
assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue()); assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
} finally { } finally {
wb.close(); wb.close();
@ -304,7 +305,7 @@ public class TestWorkbookEvaluator {
// Test IF-Equals Formula Evaluation (bug 58591) // 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(); Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("IFEquals"); Sheet sheet = wb.createSheet("IFEquals");
Row row = sheet.createRow(0); Row row = sheet.createRow(0);
@ -314,27 +315,29 @@ public class TestWorkbookEvaluator {
Cell D1 = row.createCell(3); Cell D1 = row.createCell(3);
switch (a1CellType) { switch (a1CellType) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
A1.setCellValue(1.0); A1.setCellValue(1.0);
// "A1=1" should return true // "A1=1" should return true
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
A1.setCellValue("1"); A1.setCellValue("1");
// "A1=1" should return false // "A1=1" should return false
// "A1=\"1\"" should return true // "A1=\"1\"" should return true
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
A1.setCellValue(true); A1.setCellValue(true);
// "A1=1" should return true // "A1=1" should return true
break; break;
case Cell.CELL_TYPE_FORMULA: case FORMULA:
A1.setCellFormula("1"); A1.setCellFormula("1");
// "A1=1" should return true // "A1=1" should return true
break; break;
case Cell.CELL_TYPE_BLANK: case BLANK:
A1.setCellValue((String) null); A1.setCellValue((String) null);
// "A1=1" should return false // "A1=1" should return false
break; break;
default:
throw new IllegalArgumentException("unexpected cell type: " + a1CellType);
} }
B1.setCellValue(2.0); B1.setCellValue(2.0);
C1.setCellValue(3.0); C1.setCellValue(3.0);
@ -354,7 +357,7 @@ public class TestWorkbookEvaluator {
private void testIFEqualsFormulaEvaluation_evaluate( 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); Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3); Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@ -362,17 +365,17 @@ public class TestWorkbookEvaluator {
CellValue result = eval.evaluate(D1); CellValue result = eval.evaluate(D1);
// Call should not modify the contents // Call should not modify the contents
assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType()); assertEquals(CellType.FORMULA, D1.getCellType());
assertEquals(expectedFormula, D1.getCellFormula()); assertEquals(expectedFormula, D1.getCellFormula());
assertEquals(Cell.CELL_TYPE_NUMERIC, result.getCellType()); assertEquals(CellType.NUMERIC, result.getCellType());
assertEquals(expectedResult, result.getNumberValue(), EPSILON); assertEquals(expectedResult, result.getNumberValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb); testIFEqualsFormulaEvaluation_teardown(wb);
} }
private void testIFEqualsFormulaEvaluation_eval( 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_evaluate(formula, cellType, expectedFormula, expectedValue);
testIFEqualsFormulaEvaluation_evaluateFormulaCell(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_evaluateFormulaCell(formula, cellType, expectedFormula, expectedValue);
testIFEqualsFormulaEvaluation_evaluateInCell(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_evaluateInCell(formula, cellType, expectedFormula, expectedValue);
@ -383,7 +386,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_NumericLiteral() { public void testIFEqualsFormulaEvaluation_NumericLiteral() {
final String formula = "IF(A1=1, 2, 3)"; 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 String expectedFormula = "IF(A1=1,2,3)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -392,7 +395,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_Numeric() { public void testIFEqualsFormulaEvaluation_Numeric() {
final String formula = "IF(A1=1, B1, C1)"; 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 String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -401,7 +404,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_NumericCoerceToString() { public void testIFEqualsFormulaEvaluation_NumericCoerceToString() {
final String formula = "IF(A1&\"\"=\"1\", B1, C1)"; 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 String expectedFormula = "IF(A1&\"\"=\"1\",B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -410,7 +413,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_String() { public void testIFEqualsFormulaEvaluation_String() {
final String formula = "IF(A1=1, B1, C1)"; 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 String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 3.0; final double expectedValue = 3.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -419,7 +422,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_StringCompareToString() { public void testIFEqualsFormulaEvaluation_StringCompareToString() {
final String formula = "IF(A1=\"1\", B1, C1)"; 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 String expectedFormula = "IF(A1=\"1\",B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -428,7 +431,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_StringCoerceToNumeric() { public void testIFEqualsFormulaEvaluation_StringCoerceToNumeric() {
final String formula = "IF(A1+0=1, B1, C1)"; 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 String expectedFormula = "IF(A1+0=1,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -438,7 +441,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_Boolean() { public void testIFEqualsFormulaEvaluation_Boolean() {
final String formula = "IF(A1=1, B1, C1)"; 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 String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -448,7 +451,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_BooleanSimple() { public void testIFEqualsFormulaEvaluation_BooleanSimple() {
final String formula = "3-(A1=1)"; 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 String expectedFormula = "3-(A1=1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -457,7 +460,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_Formula() { public void testIFEqualsFormulaEvaluation_Formula() {
final String formula = "IF(A1=1, B1, C1)"; 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 String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -466,7 +469,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_Blank() { public void testIFEqualsFormulaEvaluation_Blank() {
final String formula = "IF(A1=1, B1, C1)"; 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 String expectedFormula = "IF(A1=1,B1,C1)";
final double expectedValue = 3.0; final double expectedValue = 3.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -475,7 +478,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_BlankCompareToZero() { public void testIFEqualsFormulaEvaluation_BlankCompareToZero() {
final String formula = "IF(A1=0, B1, C1)"; 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 String expectedFormula = "IF(A1=0,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -485,7 +488,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_BlankInverted() { public void testIFEqualsFormulaEvaluation_BlankInverted() {
final String formula = "IF(NOT(A1)=1, B1, C1)"; 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 String expectedFormula = "IF(NOT(A1)=1,B1,C1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -495,7 +498,7 @@ public class TestWorkbookEvaluator {
@Test @Test
public void testIFEqualsFormulaEvaluation_BlankInvertedSimple() { public void testIFEqualsFormulaEvaluation_BlankInvertedSimple() {
final String formula = "3-(NOT(A1)=1)"; 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 String expectedFormula = "3-(NOT(A1)=1)";
final double expectedValue = 2.0; final double expectedValue = 2.0;
testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue); testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
@ -503,25 +506,25 @@ public class TestWorkbookEvaluator {
private void testIFEqualsFormulaEvaluation_evaluateFormulaCell( 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); Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3); Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); 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 // 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(expectedFormula, D1.getCellFormula());
assertEquals(Cell.CELL_TYPE_NUMERIC, resultCellType); assertEquals(CellType.NUMERIC, resultCellType);
assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON); assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb); testIFEqualsFormulaEvaluation_teardown(wb);
} }
private void testIFEqualsFormulaEvaluation_evaluateInCell( 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); Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3); Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@ -534,14 +537,14 @@ public class TestWorkbookEvaluator {
D1.getCellFormula(); D1.getCellFormula();
fail("cell formula should be overwritten with formula result"); fail("cell formula should be overwritten with formula result");
} catch (final IllegalStateException expected) { } } catch (final IllegalStateException expected) { }
assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCellType()); assertEquals(CellType.NUMERIC, D1.getCellType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON); assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb); testIFEqualsFormulaEvaluation_teardown(wb);
} }
private void testIFEqualsFormulaEvaluation_evaluateAll( 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); Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3); Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
@ -549,28 +552,28 @@ public class TestWorkbookEvaluator {
eval.evaluateAll(); eval.evaluateAll();
// Call should modify the contents // Call should modify the contents
assertEquals(Cell.CELL_TYPE_FORMULA, D1.getCellType()); assertEquals(CellType.FORMULA, D1.getCellType());
assertEquals(expectedFormula, D1.getCellFormula()); assertEquals(expectedFormula, D1.getCellFormula());
assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON); assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb); testIFEqualsFormulaEvaluation_teardown(wb);
} }
private void testIFEqualsFormulaEvaluation_evaluateAllFormulaCells( 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); Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3); Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
// Call should modify the contents // 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 // whitespace gets deleted because formula is parsed and re-rendered
assertEquals(expectedFormula, D1.getCellFormula()); assertEquals(expectedFormula, D1.getCellFormula());
assertEquals(Cell.CELL_TYPE_NUMERIC, D1.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON); assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
testIFEqualsFormulaEvaluation_teardown(wb); testIFEqualsFormulaEvaluation_teardown(wb);

View File

@ -31,6 +31,7 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.formula.functions.TestMathX;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@ -173,7 +174,7 @@ public final class TestFormulasFromSpreadsheet {
// iterate across the row for all the evaluation cases // iterate across the row for all the evaluation cases
for (int colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) { for (int colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) {
Cell c = formulasRow.getCell(colnum); Cell c = formulasRow.getCell(colnum);
if (c == null || c.getCellType() != Cell.CELL_TYPE_FORMULA) { if (c == null || c.getCellType() != CellType.FORMULA) {
continue; continue;
} }
@ -186,28 +187,31 @@ public final class TestFormulasFromSpreadsheet {
assertNotNull(msg + " - Bad setup data expected value is null", expValue); assertNotNull(msg + " - Bad setup data expected value is null", expValue);
assertNotNull(msg + " - actual value was null", actValue); assertNotNull(msg + " - actual value was null", actValue);
switch (expValue.getCellType()) { final CellType cellType = expValue.getCellType();
case Cell.CELL_TYPE_BLANK: switch (cellType) {
assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType()); case BLANK:
assertEquals(msg, CellType.BLANK, actValue.getCellType());
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType()); assertEquals(msg, CellType.BOOLEAN, actValue.getCellType());
assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue()); assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType()); assertEquals(msg, CellType.ERROR, actValue.getCellType());
assertEquals(msg, ErrorEval.getText(expValue.getErrorCellValue()), ErrorEval.getText(actValue.getErrorValue())); assertEquals(msg, ErrorEval.getText(expValue.getErrorCellValue()), ErrorEval.getText(actValue.getErrorValue()));
break; 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); fail("Cannot expect formula as result of formula evaluation: " + msg);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType()); assertEquals(msg, CellType.NUMERIC, actValue.getCellType());
TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType()); assertEquals(msg, CellType.STRING, actValue.getCellType());
assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue()); assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue());
break; 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"); System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
} }

View File

@ -30,6 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.formula.functions.TestMathX;
import org.apache.poi.ss.usermodel.Cell; 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.CellValue;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@ -104,29 +105,33 @@ public final class TestMultiSheetEval extends TestCase {
if(actual == null) { if(actual == null) {
throw new AssertionFailedError(msg + " - actual value was null"); throw new AssertionFailedError(msg + " - actual value was null");
} }
final CellType cellType = expected.getCellType();
switch (expected.getCellType()) { switch (cellType) {
case Cell.CELL_TYPE_BLANK: case BLANK:
assertEquals(msg, Cell.CELL_TYPE_BLANK, actual.getCellType()); assertEquals(msg, CellType.BLANK, actual.getCellType());
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actual.getCellType()); assertEquals(msg, CellType.BOOLEAN, actual.getCellType());
assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue()); assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue());
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
assertEquals(msg, Cell.CELL_TYPE_ERROR, actual.getCellType()); assertEquals(msg, CellType.ERROR, actual.getCellType());
assertEquals(msg, ErrorEval.getText(expected.getErrorCellValue()), ErrorEval.getText(actual.getErrorValue())); assertEquals(msg, ErrorEval.getText(expected.getErrorCellValue()), ErrorEval.getText(actual.getErrorValue()));
break; 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); throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg);
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actual.getCellType()); assertEquals(msg, CellType.NUMERIC, actual.getCellType());
TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
assertEquals(msg, Cell.CELL_TYPE_STRING, actual.getCellType()); assertEquals(msg, CellType.STRING, actual.getCellType());
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue()); assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break; 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 int result = Result.NO_EVALUATIONS_FOUND; // so far
Cell c = formulasRow.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE); 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; 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"); System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); 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"); System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_TEST_NAME + ", can't figure out test name");
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == Cell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
} }

View File

@ -36,6 +36,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference; import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -122,7 +123,7 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
currentGroupComment = newMarkerValue; currentGroupComment = newMarkerValue;
} }
HSSFCell evalCell = r.getCell(SS.COLUMN_INDEX_EVALUATION); 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; continue;
} }
String rowComment = getCellTextValue(r, SS.COLUMN_ROW_COMMENT, "row comment"); 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 + " - Bad setup data expected value is null", expectedCell);
assertNotNull(msg + " - actual value was null", actualValue); assertNotNull(msg + " - actual value was null", actualValue);
if (expectedCell.getCellType() == HSSFCell.CELL_TYPE_ERROR) { if (expectedCell.getCellType() == CellType.ERROR) {
int expectedErrorCode = expectedCell.getErrorCellValue(); 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, ErrorEval.getText(expectedErrorCode), actualValue.formatAsString());
assertEquals(msg, expectedErrorCode, actualValue.getErrorValue()); assertEquals(msg, expectedErrorCode, actualValue.getErrorValue());
assertEquals(msg, ErrorEval.getText(expectedErrorCode), ErrorEval.getText(actualValue.getErrorValue())); assertEquals(msg, ErrorEval.getText(expectedErrorCode), ErrorEval.getText(actualValue.getErrorValue()));
@ -162,24 +163,27 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
} }
// unexpected error // unexpected error
assertNotEquals(msg, HSSFCell.CELL_TYPE_ERROR, actualValue.getCellType()); assertNotEquals(msg, CellType.ERROR, actualValue.getCellType());
assertNotEquals(msg, formatValue(expectedCell), ErrorEval.getText(actualValue.getErrorValue())); assertNotEquals(msg, formatValue(expectedCell), ErrorEval.getText(actualValue.getErrorValue()));
// wrong type error // wrong type error
assertEquals(msg, expectedCell.getCellType(), actualValue.getCellType()); assertEquals(msg, expectedCell.getCellType(), actualValue.getCellType());
switch (expectedCell.getCellType()) { final CellType expectedCellType = expectedCell.getCellType();
case HSSFCell.CELL_TYPE_BOOLEAN: switch (expectedCellType) {
case BOOLEAN:
assertEquals(msg, expectedCell.getBooleanCellValue(), actualValue.getBooleanValue()); assertEquals(msg, expectedCell.getBooleanCellValue(), actualValue.getBooleanValue());
break; 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); fail("Cannot expect formula as result of formula evaluation: " + msg);
case HSSFCell.CELL_TYPE_NUMERIC: case NUMERIC:
assertEquals(expectedCell.getNumericCellValue(), actualValue.getNumberValue(), 0.0); assertEquals(expectedCell.getNumericCellValue(), actualValue.getNumberValue(), 0.0);
break; break;
case HSSFCell.CELL_TYPE_STRING: case STRING:
assertEquals(msg, expectedCell.getRichStringCellValue().getString(), actualValue.getStringValue()); assertEquals(msg, expectedCell.getRichStringCellValue().getString(), actualValue.getStringValue());
break; break;
default:
fail("Unexpected cell type: " + expectedCellType);
} }
} }
@ -208,10 +212,10 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
if(cell == null) { if(cell == null) {
return null; return null;
} }
if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { if(cell.getCellType() == CellType.BLANK) {
return null; return null;
} }
if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { if(cell.getCellType() == CellType.STRING) {
return cell.getRichStringCellValue().getString(); return cell.getRichStringCellValue().getString();
} }
@ -222,12 +226,12 @@ public abstract class BaseTestFunctionsFromSpreadsheet {
private static String formatValue(HSSFCell expecedCell) { private static String formatValue(HSSFCell expecedCell) {
switch (expecedCell.getCellType()) { switch (expecedCell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK: return "<blank>"; case BLANK: return "<blank>";
case HSSFCell.CELL_TYPE_BOOLEAN: return Boolean.toString(expecedCell.getBooleanCellValue()); case BOOLEAN: return Boolean.toString(expecedCell.getBooleanCellValue());
case HSSFCell.CELL_TYPE_NUMERIC: return Double.toString(expecedCell.getNumericCellValue()); case NUMERIC: return Double.toString(expecedCell.getNumericCellValue());
case HSSFCell.CELL_TYPE_STRING: return expecedCell.getRichStringCellValue().getString(); 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 ""; return "";
} }

View File

@ -527,7 +527,7 @@ public abstract class BaseTestBugzillaIssues {
for(int rn=0; rn<= topRow; rn++) { for(int rn=0; rn<= topRow; rn++) {
Row r = s.createRow(rn); Row r = s.createRow(rn);
for(int cn=0; cn<leftmostColumn; cn++) { 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); c.setCellValue(100*rn + cn);
} }
} }
@ -654,16 +654,16 @@ public abstract class BaseTestBugzillaIssues {
Row r1 = s.createRow(0); Row r1 = s.createRow(0);
for (int i=0; i<3; i++) { 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++) { 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++) { 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++) { 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) { for (int i=0; i<12; i+=3) {
r1.getCell(i).setCellStyle(iPercent); r1.getCell(i).setCellStyle(iPercent);
@ -714,11 +714,11 @@ public abstract class BaseTestBugzillaIssues {
// C1 is a string, with different text // C1 is a string, with different text
r1.createCell(2).setCellValue("This some other text"); r1.createCell(2).setCellValue("This some other text");
// D1 is a blank cell // D1 is a blank cell
r1.createCell(3, Cell.CELL_TYPE_BLANK); r1.createCell(3, CellType.BLANK);
// E1 is null // E1 is null
// A2 will hold our test formulas // 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 // First up, check that TRUE and ISLOGICAL both behave
@ -872,10 +872,10 @@ public abstract class BaseTestBugzillaIssues {
// Create the references // Create the references
Cell c1 = r1.createCell(0, Cell.CELL_TYPE_FORMULA); Cell c1 = r1.createCell(0, CellType.FORMULA);
c1.setCellFormula(refLocal); c1.setCellFormula(refLocal);
Cell c2 = r1.createCell(1, Cell.CELL_TYPE_FORMULA); Cell c2 = r1.createCell(1, CellType.FORMULA);
c2.setCellFormula(refHttp); c2.setCellFormula(refHttp);
@ -952,7 +952,7 @@ public abstract class BaseTestBugzillaIssues {
assertEquals(1, cArray.length);*/ assertEquals(1, cArray.length);*/
Cell cell = row.getCell(0); Cell cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
} }
{ // overwrite the row { // overwrite the row
@ -1119,29 +1119,29 @@ public abstract class BaseTestBugzillaIssues {
Row r = s.createRow(0); Row r = s.createRow(0);
// Setup // Setup
Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC); Cell cn = r.createCell(0, CellType.NUMERIC);
cn.setCellValue(1.2); cn.setCellValue(1.2);
Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING); Cell cs = r.createCell(1, CellType.STRING);
cs.setCellValue("Testing"); cs.setCellValue("Testing");
Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA); Cell cfn = r.createCell(2, CellType.FORMULA);
cfn.setCellFormula("A1"); cfn.setCellFormula("A1");
Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA); Cell cfs = r.createCell(3, CellType.FORMULA);
cfs.setCellFormula("B1"); cfs.setCellFormula("B1");
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType()); assertEquals(CellType.NUMERIC, fe.evaluate(cfn).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType()); assertEquals(CellType.STRING, fe.evaluate(cfs).getCellType());
fe.evaluateFormulaCell(cfn); fe.evaluateFormulaCell(cfn);
fe.evaluateFormulaCell(cfs); fe.evaluateFormulaCell(cfs);
// Now test // Now test
assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType()); assertEquals(CellType.NUMERIC, cn.getCellType());
assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType()); assertEquals(CellType.STRING, cs.getCellType());
assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType()); assertEquals(CellType.FORMULA, cfn.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, cfn.getCachedFormulaResultType());
assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType()); assertEquals(CellType.FORMULA, cfs.getCellType());
assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType()); assertEquals(CellType.STRING, cfs.getCachedFormulaResultType());
// Different ways of retrieving // Different ways of retrieving
assertEquals(1.2, cn.getNumericCellValue(), 0); assertEquals(1.2, cn.getNumericCellValue(), 0);
@ -1195,7 +1195,7 @@ public abstract class BaseTestBugzillaIssues {
cell = row.createCell(1); cell = row.createCell(1);
// also verify that setting formulas to null works // also verify that setting formulas to null works
cell.setCellType(Cell.CELL_TYPE_FORMULA); cell.setCellType(CellType.FORMULA);
cell.setCellValue((String)null); cell.setCellValue((String)null);
wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
@ -1205,7 +1205,7 @@ public abstract class BaseTestBugzillaIssues {
value == null || value.length() == 0); value == null || value.length() == 0);
// set some value // set some value
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
cell.setCellValue("somevalue"); cell.setCellValue("somevalue");
value = cell.getStringCellValue(); value = cell.getStringCellValue();
@ -1383,7 +1383,7 @@ public abstract class BaseTestBugzillaIssues {
Sheet s = wb.createSheet(); Sheet s = wb.createSheet();
Cell cell = s.createRow(0).createCell(0); Cell cell = s.createRow(0).createCell(0);
cell.setCellValue((String)null); cell.setCellValue((String)null);
assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); assertEquals(CellType.BLANK, cell.getCellType());
_testDataProvider.trackAllColumnsForAutosizing(s); _testDataProvider.trackAllColumnsForAutosizing(s);
@ -1554,8 +1554,8 @@ public abstract class BaseTestBugzillaIssues {
Cell cell = row.getCell(cellId); Cell cell = row.getCell(cellId);
System.out.println("Formula:" + cell.getCellFormula()); System.out.println("Formula:" + cell.getCellFormula());
if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { if (CellType.FORMULA == cell.getCellType()) {
int formulaResultType = cell.getCachedFormulaResultType(); CellType formulaResultType = cell.getCachedFormulaResultType();
System.out.println("Formula Result Type:" + formulaResultType); System.out.println("Formula Result Type:" + formulaResultType);
} }
@ -1569,8 +1569,8 @@ public abstract class BaseTestBugzillaIssues {
cell = row.getCell(cellId); cell = row.getCell(cellId);
System.out.println("Formula:" + cell.getCellFormula()); System.out.println("Formula:" + cell.getCellFormula());
if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { if (CellType.FORMULA == cell.getCellType()) {
int formulaResultType = cell.getCachedFormulaResultType(); CellType formulaResultType = cell.getCachedFormulaResultType();
System.out.println("Formula Result Type:" + formulaResultType); System.out.println("Formula Result Type:" + formulaResultType);
} }

View File

@ -60,76 +60,78 @@ public abstract class BaseTestCell {
cell.setCellValue(1.2); cell.setCellValue(1.2);
assertEquals(1.2, cell.getNumericCellValue(), 0.0001); assertEquals(1.2, cell.getNumericCellValue(), 0.0001);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
cell.setCellValue(false); cell.setCellValue(false);
assertEquals(false, cell.getBooleanCellValue()); assertEquals(false, cell.getBooleanCellValue());
assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); assertEquals(CellType.BOOLEAN, cell.getCellType());
cell.setCellValue(true); cell.setCellValue(true);
assertEquals(true, cell.getBooleanCellValue()); assertEquals(true, cell.getBooleanCellValue());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
cell.setCellValue(factory.createRichTextString("Foo")); cell.setCellValue(factory.createRichTextString("Foo"));
assertEquals("Foo", cell.getRichStringCellValue().getString()); assertEquals("Foo", cell.getRichStringCellValue().getString());
assertEquals("Foo", cell.getStringCellValue()); assertEquals("Foo", cell.getStringCellValue());
assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(CellType.STRING, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
cell.setCellValue("345"); cell.setCellValue("345");
assertEquals("345", cell.getRichStringCellValue().getString()); assertEquals("345", cell.getRichStringCellValue().getString());
assertEquals("345", cell.getStringCellValue()); assertEquals("345", cell.getStringCellValue());
assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(CellType.STRING, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
Calendar c = LocaleUtil.getLocaleCalendar(); Calendar c = LocaleUtil.getLocaleCalendar();
c.setTimeInMillis(123456789); c.setTimeInMillis(123456789);
cell.setCellValue(c.getTime()); cell.setCellValue(c.getTime());
assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
cell.setCellValue(c); cell.setCellValue(c);
assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); CellType.FORMULA, CellType.ERROR);
cell.setCellErrorValue(FormulaError.NA.getCode()); cell.setCellErrorValue(FormulaError.NA.getCode());
assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue()); assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue());
assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType()); assertEquals(CellType.ERROR, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING); CellType.FORMULA, CellType.STRING);
book.close(); book.close();
} }
private static void assertProhibitedValueAccess(Cell cell, int ... types){ private static void assertProhibitedValueAccess(Cell cell, CellType ... types) {
for(int type : types){ for(CellType type : types){
try { try {
switch (type) { switch (type) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
cell.getNumericCellValue(); cell.getNumericCellValue();
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
cell.getStringCellValue(); cell.getStringCellValue();
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
cell.getBooleanCellValue(); cell.getBooleanCellValue();
break; break;
case Cell.CELL_TYPE_FORMULA: case FORMULA:
cell.getCellFormula(); cell.getCellFormula();
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
cell.getErrorCellValue(); cell.getErrorCellValue();
break; break;
default:
fail("Should get exception when reading cell type (" + type + ").");
} }
fail("Should get exception when reading cell type (" + type + ").");
} catch (IllegalStateException e){ } catch (IllegalStateException e){
// expected during successful test // expected during successful test
assertTrue(e.getMessage().startsWith("Cannot get a")); assertTrue(e.getMessage().startsWith("Cannot get a"));
@ -174,13 +176,13 @@ public abstract class BaseTestCell {
c = r.getCell(1); c = r.getCell(1);
assertEquals(0, c.getRowIndex()); assertEquals(0, c.getRowIndex());
assertEquals(1, c.getColumnIndex()); assertEquals(1, c.getColumnIndex());
assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType()); assertEquals(CellType.BOOLEAN, c.getCellType());
assertEquals("B1 value", true, c.getBooleanCellValue()); assertEquals("B1 value", true, c.getBooleanCellValue());
c = r.getCell(2); c = r.getCell(2);
assertEquals(0, c.getRowIndex()); assertEquals(0, c.getRowIndex());
assertEquals(2, c.getColumnIndex()); assertEquals(2, c.getColumnIndex());
assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType()); assertEquals(CellType.BOOLEAN, c.getCellType());
assertEquals("C1 value", false, c.getBooleanCellValue()); assertEquals("C1 value", false, c.getBooleanCellValue());
wb2.close(); wb2.close();
@ -224,13 +226,13 @@ public abstract class BaseTestCell {
c = r.getCell(1); c = r.getCell(1);
assertEquals(0, c.getRowIndex()); assertEquals(0, c.getRowIndex());
assertEquals(1, c.getColumnIndex()); 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()); assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
c = r.getCell(2); c = r.getCell(2);
assertEquals(0, c.getRowIndex()); assertEquals(0, c.getRowIndex());
assertEquals(2, c.getColumnIndex()); 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()); assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
wb2.close(); wb2.close();
@ -270,7 +272,7 @@ public abstract class BaseTestCell {
r = s.getRow(0); r = s.getRow(0);
c = r.getCell(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(); cs = c.getCellStyle();
assertNotNull("Formula Cell Style", cs); assertNotNull("Formula Cell Style", cs);
@ -345,25 +347,25 @@ public abstract class BaseTestCell {
Cell c1 = r.createCell(0); Cell c1 = r.createCell(0);
c1.setCellFormula("NA()"); c1.setCellFormula("NA()");
assertEquals(0.0, c1.getNumericCellValue(), 0.0); assertEquals(0.0, c1.getNumericCellValue(), 0.0);
assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType());
c1.setCellValue(10); c1.setCellValue(10);
assertEquals(10.0, c1.getNumericCellValue(), 0.0); assertEquals(10.0, c1.getNumericCellValue(), 0.0);
assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType()); assertEquals(CellType.FORMULA, c1.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType());
Cell c2 = r.createCell(1); Cell c2 = r.createCell(1);
c2.setCellFormula("NA()"); c2.setCellFormula("NA()");
assertEquals(0.0, c2.getNumericCellValue(), 0.0); assertEquals(0.0, c2.getNumericCellValue(), 0.0);
assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType()); assertEquals(CellType.NUMERIC, c2.getCachedFormulaResultType());
c2.setCellValue("I changed!"); c2.setCellValue("I changed!");
assertEquals("I changed!", c2.getStringCellValue()); assertEquals("I changed!", c2.getStringCellValue());
assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType()); assertEquals(CellType.FORMULA, c2.getCellType());
assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType()); assertEquals(CellType.STRING, c2.getCachedFormulaResultType());
//calglin Cell.setCellFormula(null) for a non-formula cell //calglin Cell.setCellFormula(null) for a non-formula cell
Cell c3 = r.createCell(2); Cell c3 = r.createCell(2);
c3.setCellFormula(null); c3.setCellFormula(null);
assertEquals(Cell.CELL_TYPE_BLANK, c3.getCellType()); assertEquals(CellType.BLANK, c3.getCellType());
wb.close(); wb.close();
} }
@ -418,21 +420,21 @@ public abstract class BaseTestCell {
Cell cell = createACell(wb); Cell cell = createACell(wb);
cell.setCellValue("TRUE"); cell.setCellValue("TRUE");
assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(CellType.STRING, cell.getCellType());
// test conversion of cell from text to boolean // 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()); assertEquals(true, cell.getBooleanCellValue());
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
assertEquals("TRUE", cell.getRichStringCellValue().getString()); assertEquals("TRUE", cell.getRichStringCellValue().getString());
// 'false' text to bool and back // 'false' text to bool and back
cell.setCellValue("FALSE"); cell.setCellValue("FALSE");
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); assertEquals(CellType.BOOLEAN, cell.getCellType());
assertEquals(false, cell.getBooleanCellValue()); assertEquals(false, cell.getBooleanCellValue());
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
assertEquals("FALSE", cell.getRichStringCellValue().getString()); assertEquals("FALSE", cell.getRichStringCellValue().getString());
wb.close(); wb.close();
@ -446,7 +448,7 @@ public abstract class BaseTestCell {
cell.setCellValue(true); cell.setCellValue(true);
// test conversion of cell from boolean to text // test conversion of cell from boolean to text
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
assertEquals("TRUE", cell.getRichStringCellValue().getString()); assertEquals("TRUE", cell.getRichStringCellValue().getString());
wb.close(); wb.close();
@ -523,7 +525,7 @@ public abstract class BaseTestCell {
fe.clearAllCachedResultValues(); fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cellA1); fe.evaluateFormulaCell(cellA1);
assertEquals("DEF", cellA1.getStringCellValue()); assertEquals("DEF", cellA1.getStringCellValue());
cellA1.setCellType(Cell.CELL_TYPE_STRING); cellA1.setCellType(CellType.STRING);
assertEquals("DEF", cellA1.getStringCellValue()); assertEquals("DEF", cellA1.getStringCellValue());
cellA1.setCellFormula("25.061"); cellA1.setCellFormula("25.061");
@ -531,7 +533,7 @@ public abstract class BaseTestCell {
fe.evaluateFormulaCell(cellA1); fe.evaluateFormulaCell(cellA1);
confirmCannotReadString(cellA1); confirmCannotReadString(cellA1);
assertEquals(25.061, cellA1.getNumericCellValue(), 0.0); assertEquals(25.061, cellA1.getNumericCellValue(), 0.0);
cellA1.setCellType(Cell.CELL_TYPE_STRING); cellA1.setCellType(CellType.STRING);
assertEquals("25.061", cellA1.getStringCellValue()); assertEquals("25.061", cellA1.getStringCellValue());
cellA1.setCellFormula("TRUE"); cellA1.setCellFormula("TRUE");
@ -539,7 +541,7 @@ public abstract class BaseTestCell {
fe.evaluateFormulaCell(cellA1); fe.evaluateFormulaCell(cellA1);
confirmCannotReadString(cellA1); confirmCannotReadString(cellA1);
assertEquals(true, cellA1.getBooleanCellValue()); assertEquals(true, cellA1.getBooleanCellValue());
cellA1.setCellType(Cell.CELL_TYPE_STRING); cellA1.setCellType(CellType.STRING);
assertEquals("TRUE", cellA1.getStringCellValue()); assertEquals("TRUE", cellA1.getStringCellValue());
cellA1.setCellFormula("#NAME?"); cellA1.setCellFormula("#NAME?");
@ -547,14 +549,14 @@ public abstract class BaseTestCell {
fe.evaluateFormulaCell(cellA1); fe.evaluateFormulaCell(cellA1);
confirmCannotReadString(cellA1); confirmCannotReadString(cellA1);
assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue())); assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue()));
cellA1.setCellType(Cell.CELL_TYPE_STRING); cellA1.setCellType(CellType.STRING);
assertEquals("#NAME?", cellA1.getStringCellValue()); assertEquals("#NAME?", cellA1.getStringCellValue());
wb.close(); wb.close();
} }
private static void confirmCannotReadString(Cell cell) { 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 cell = createACell(wb);
cell.setCellFormula("1=1"); cell.setCellFormula("1=1");
cell.setCellValue(true); cell.setCellValue(true);
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(CellType.BOOLEAN);
assertTrue("Identified bug 46479d", cell.getBooleanCellValue()); assertTrue("Identified bug 46479d", cell.getBooleanCellValue());
assertEquals(true, cell.getBooleanCellValue()); assertEquals(true, cell.getBooleanCellValue());
@ -585,19 +587,19 @@ public abstract class BaseTestCell {
Cell cell; Cell cell;
Row row = workSheet.createRow(0); Row row = workSheet.createRow(0);
cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC); cell = row.createCell(0, CellType.NUMERIC);
cell.setCellValue(1.0); cell.setCellValue(1.0);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(1.0, cell.getNumericCellValue(), 0.0); 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); cell.setCellValue(2.0);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(2.0, cell.getNumericCellValue(), 0.0); 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)"); cell.setCellFormula("SUM(A1:B1)");
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("SUM(A1:B1)", cell.getCellFormula()); assertEquals("SUM(A1:B1)", cell.getCellFormula());
//serialize and check again //serialize and check again
@ -605,15 +607,15 @@ public abstract class BaseTestCell {
wb1.close(); wb1.close();
row = wb2.getSheetAt(0).getRow(0); row = wb2.getSheetAt(0).getRow(0);
cell = row.getCell(0); cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(1.0, cell.getNumericCellValue(), 0.0); assertEquals(1.0, cell.getNumericCellValue(), 0.0);
cell = row.getCell(1); cell = row.getCell(1);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(2.0, cell.getNumericCellValue(), 0.0); assertEquals(2.0, cell.getNumericCellValue(), 0.0);
cell = row.getCell(2); cell = row.getCell(2);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("SUM(A1:B1)", cell.getCellFormula()); assertEquals("SUM(A1:B1)", cell.getCellFormula());
wb2.close(); 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 @Test
public void testSetBlank_bug47028() throws Exception { public void testSetBlank_bug47028() throws Exception {
@ -637,7 +639,7 @@ public abstract class BaseTestCell {
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellStyle(style); cell.setCellStyle(style);
int i1 = cell.getCellStyle().getIndex(); int i1 = cell.getCellStyle().getIndex();
cell.setCellType(Cell.CELL_TYPE_BLANK); cell.setCellType(CellType.BLANK);
int i2 = cell.getCellStyle().getIndex(); int i2 = cell.getCellStyle().getIndex();
assertEquals(i1, i2); assertEquals(i1, i2);
wb.close(); wb.close();
@ -670,17 +672,17 @@ public abstract class BaseTestCell {
Cell cell0 = row.createCell(0); Cell cell0 = row.createCell(0);
cell0.setCellValue(Double.NaN); 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())); assertEquals("Double.NaN should change cell value to #NUM!", FormulaError.NUM, forInt(cell0.getErrorCellValue()));
Cell cell1 = row.createCell(1); Cell cell1 = row.createCell(1);
cell1.setCellValue(Double.POSITIVE_INFINITY); 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())); assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
Cell cell2 = row.createCell(2); Cell cell2 = row.createCell(2);
cell2.setCellValue(Double.NEGATIVE_INFINITY); 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())); assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
@ -688,15 +690,15 @@ public abstract class BaseTestCell {
row = wb2.getSheetAt(0).getRow(0); row = wb2.getSheetAt(0).getRow(0);
cell0 = row.getCell(0); cell0 = row.getCell(0);
assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType()); assertEquals(CellType.ERROR, cell0.getCellType());
assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue())); assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue()));
cell1 = row.getCell(1); cell1 = row.getCell(1);
assertEquals(Cell.CELL_TYPE_ERROR, cell1.getCellType()); assertEquals(CellType.ERROR, cell1.getCellType());
assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue())); assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
cell2 = row.getCell(2); cell2 = row.getCell(2);
assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType()); assertEquals(CellType.ERROR, cell2.getCellType());
assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue())); assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
wb2.close(); wb2.close();
} }
@ -897,21 +899,21 @@ public abstract class BaseTestCell {
RichTextString nullStr = null; RichTextString nullStr = null;
cell.setCellValue(nullStr); cell.setCellValue(nullStr);
assertEquals("", cell.getStringCellValue()); assertEquals("", cell.getStringCellValue());
assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); assertEquals(CellType.BLANK, cell.getCellType());
cell = sheet.createRow(0).createCell(1); cell = sheet.createRow(0).createCell(1);
cell.setCellValue(1.2d); cell.setCellValue(1.2d);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(CellType.NUMERIC, cell.getCellType());
cell.setCellValue(nullStr); cell.setCellValue(nullStr);
assertEquals("", cell.getStringCellValue()); assertEquals("", cell.getStringCellValue());
assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); assertEquals(CellType.BLANK, cell.getCellType());
cell = sheet.createRow(0).createCell(1); cell = sheet.createRow(0).createCell(1);
cell.setCellValue(wb.getCreationHelper().createRichTextString("Test")); cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(CellType.STRING, cell.getCellType());
cell.setCellValue(nullStr); cell.setCellValue(nullStr);
assertEquals("", cell.getStringCellValue()); assertEquals("", cell.getStringCellValue());
assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType()); assertEquals(CellType.BLANK, cell.getCellType());
wb.close(); wb.close();
} }

View File

@ -216,7 +216,7 @@ public abstract class BaseTestFormulaEvaluator {
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
Sheet sheet = wb.createSheet("Sheet1"); Sheet sheet = wb.createSheet("Sheet1");
Row r = sheet.createRow(0); 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 // Create a value and check it
c.setCellFormula("Date(2011,10,6)"); c.setCellFormula("Date(2011,10,6)");