Browse Source

update getCellType to return CellType enum instead of int

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808703 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_0_0_FINAL
PJ Fanning 6 years ago
parent
commit
e3ddb77bc1

+ 2
- 2
src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java View File

} }


private static CellType ultimateCellType(Cell c) { private static CellType ultimateCellType(Cell c) {
CellType type = c.getCellTypeEnum();
CellType type = c.getCellType();
if (type == CellType.FORMULA) { if (type == CellType.FORMULA) {
type = c.getCachedFormulaResultTypeEnum();
type = c.getCachedFormulaResultType();
} }
return type; return type;
} }

+ 2
- 2
src/java/org/apache/poi/ss/formula/DataValidationEvaluator.java View File

* @return true if the cell or cached cell formula result type match the given type * @return true if the cell or cached cell formula result type match the given type
*/ */
public static boolean isType(Cell cell, CellType type) { public static boolean isType(Cell cell, CellType type) {
final CellType cellType = cell.getCellTypeEnum();
final CellType cellType = cell.getCellType();
return cellType == type return cellType == type
|| (cellType == CellType.FORMULA || (cellType == CellType.FORMULA
&& cell.getCachedFormulaResultTypeEnum() == type
&& cell.getCachedFormulaResultType() == type
); );
} }

+ 4
- 4
src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java View File

// 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.getCellTypeEnum() != CellType.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);
} catch (RuntimeException re) { } catch (RuntimeException re) {
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.getCachedFormulaResultTypeEnum()) {
switch(srcCell.getCachedFormulaResultType()) {
case NUMERIC: case NUMERIC:
result = new NumberEval(srcCell.getNumericCellValue()); result = new NumberEval(srcCell.getNumericCellValue());
break; break;
break; break;
case FORMULA: case FORMULA:
default: default:
throw new RuntimeException("Unexpected cell type '" + srcCell.getCellTypeEnum()+"' found!");
throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
} }
} else { } else {
throw re; throw re;
if (cell == null) { if (cell == null) {
return BlankEval.instance; return BlankEval.instance;
} }
CellType cellType = cell.getCellTypeEnum();
CellType cellType = cell.getCellType();
switch (cellType) { switch (cellType) {
case NUMERIC: case NUMERIC:
return new NumberEval(cell.getNumericCellValue()); return new NumberEval(cell.getNumericCellValue());

+ 2
- 2
src/java/org/apache/poi/ss/util/SheetUtil.java View File

} }


CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
CellType cellType = cell.getCellTypeEnum();
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 == CellType.FORMULA) if (cellType == CellType.FORMULA)
cellType = cell.getCachedFormulaResultTypeEnum();
cellType = cell.getCachedFormulaResultType();


Font font = wb.getFontAt(style.getFontIndex()); Font font = wb.getFontAt(style.getFontIndex());



+ 2
- 2
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java View File

private void mapCellOnNode(XSSFCell cell, Node node) { private void mapCellOnNode(XSSFCell cell, Node node) {


String value =""; String value ="";
switch (cell.getCellTypeEnum()) {
switch (cell.getCellType()) {


case STRING: value = cell.getStringCellValue(); break; case STRING: value = cell.getStringCellValue(); break;
case BOOLEAN: value += cell.getBooleanCellValue(); break; case BOOLEAN: value += cell.getBooleanCellValue(); break;
case ERROR: value = cell.getErrorCellString(); break; case ERROR: value = cell.getErrorCellString(); break;
case FORMULA: case FORMULA:
if (cell.getCachedFormulaResultTypeEnum() == CellType.STRING) {
if (cell.getCachedFormulaResultType() == CellType.STRING) {
value = cell.getStringCellValue(); value = cell.getStringCellValue();
} else { } else {
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {

+ 1
- 1
src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java View File

_out.write("><f>"); _out.write("><f>");
outputQuotedString(cell.getCellFormula()); outputQuotedString(cell.getCellFormula());
_out.write("</f>"); _out.write("</f>");
switch (cell.getCachedFormulaResultTypeEnum()) {
switch (cell.getCachedFormulaResultType()) {
case NUMERIC: case NUMERIC:
double nval = cell.getNumericCellValue(); double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) { if (!Double.isNaN(nval)) {

+ 17
- 17
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java View File

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.getCellTypeEnum() == CellType.FORMULA) {
if (c.getCellType() == CellType.FORMULA) {
CellValue cv = eval.evaluate(c); CellValue cv = eval.evaluate(c);


if (cv.getCellTypeEnum() == CellType.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();


cell = sheet.getRow(0).getCell(0); cell = sheet.getRow(0).getCell(0);
assertEquals("#REF!*#REF!", cell.getCellFormula()); assertEquals("#REF!*#REF!", cell.getCellFormula());
assertEquals(CellType.ERROR, evaluator.evaluateInCell(cell).getCellTypeEnum());
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");


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(CellType.ERROR, evaluator.evaluateInCell(cell).getCellTypeEnum());
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();
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.getCellTypeEnum() == CellType.FORMULA) {
if (cell.getCellType() == CellType.FORMULA) {
formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
} }
} }


// 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(CellType.NUMERIC, cell.getCellTypeEnum());
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);


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(CellType.FORMULA, cell.getCellTypeEnum());
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(CellType.ERROR, value.getCellTypeEnum());
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());
if (cell == null) { if (cell == null) {
cell = row.createCell(cellnum); cell = row.createCell(cellnum);
} else { } else {
if (cell.getCellTypeEnum() == CellType.FORMULA) {
if (cell.getCellType() == CellType.FORMULA) {
cell.setCellFormula(null); cell.setCellFormula(null);
cell.getCellStyle().setDataFormat((short) 0); cell.getCellStyle().setDataFormat((short) 0);
} }
} }


private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) { private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
assertEquals(CellType.FORMULA, intF.getCellTypeEnum());
assertEquals(CellType.FORMULA, intF.getCellType());
if (null == expectedResultOrNull) { if (null == expectedResultOrNull) {
assertEquals(CellType.ERROR, intF.getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, intF.getCachedFormulaResultType());
expectedResultOrNull = "#VALUE!"; expectedResultOrNull = "#VALUE!";
} else { } else {
assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultTypeEnum());
assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType());
} }


assertEquals(expectedFormula, intF.getCellFormula()); assertEquals(expectedFormula, intF.getCellFormula());
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.getCellTypeEnum() == CellType.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);
row = worksheet.getRow(2); row = worksheet.getRow(2);
cell = row.getCell(1); cell = row.getCell(1);


assertEquals(CellType.BLANK, cell.getCellTypeEnum());
assertEquals(CellType.BLANK, cell.getCellType());
assertEquals(CellType._NONE, evaluator.evaluateFormulaCellEnum(cell)); assertEquals(CellType._NONE, evaluator.evaluateFormulaCellEnum(cell));


// A3 // A3
row = worksheet.getRow(2); row = worksheet.getRow(2);
cell = row.getCell(0); cell = row.getCell(0);


assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("IF(ISBLANK(B3),\"\",B3)", cell.getCellFormula()); assertEquals("IF(ISBLANK(B3),\"\",B3)", cell.getCellFormula());
assertEquals(CellType.STRING, evaluator.evaluateFormulaCellEnum(cell)); assertEquals(CellType.STRING, evaluator.evaluateFormulaCellEnum(cell));
CellValue value = evaluator.evaluate(cell); CellValue value = evaluator.evaluate(cell);
row = worksheet.getRow(4); row = worksheet.getRow(4);
cell = row.getCell(0); cell = row.getCell(0);


assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula()); assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula());
assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCellEnum(cell)); assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCellEnum(cell));
value = evaluator.evaluate(cell); value = evaluator.evaluate(cell);


Row r = s.getRow(3); Row r = s.getRow(3);
Cell c = r.getCell(0); Cell c = r.getCell(0);
assertEquals(CellType.FORMULA, c.getCellTypeEnum());
assertEquals(CellType.FORMULA, c.getCellType());
System.out.println(c.getCellFormula()); System.out.println(c.getCellFormula());
eval.setDebugEvaluationOutputForNextEval(true); eval.setDebugEvaluationOutputForNextEval(true);
CellValue cv = eval.evaluate(c); CellValue cv = eval.evaluate(c);

+ 5
- 5
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java View File



wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); wb.getCreationHelper().createFormulaEvaluator().evaluateAll();


assertEquals(CellType.ERROR, getCell(sheet, 0,0).getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, getCell(sheet, 0,0).getCachedFormulaResultType());
assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,0).getErrorCellValue()); assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,0).getErrorCellValue());
assertEquals(CellType.ERROR, getCell(sheet, 0,1).getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, getCell(sheet, 0,1).getCachedFormulaResultType());
assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,1).getErrorCellValue()); assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,1).getErrorCellValue());
wb.close(); wb.close();


wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); wb.getCreationHelper().createFormulaEvaluator().evaluateAll();


assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultType());
assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue()); assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
assertEquals(CellType.ERROR, getCell(sheet, 1, 0).getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, getCell(sheet, 1, 0).getCachedFormulaResultType());
assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue()); assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue());
assertEquals(CellType.ERROR, getCell(sheet, 0, 3).getCachedFormulaResultTypeEnum());
assertEquals(CellType.ERROR, getCell(sheet, 0, 3).getCachedFormulaResultType());
assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue()); assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue());
wb.close(); wb.close();

+ 1
- 1
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java View File

assertEquals(wb.getWorkbook(), cell.getBoundWorkbook()); assertEquals(wb.getWorkbook(), cell.getBoundWorkbook());


try { try {
cell.getCachedFormulaResultTypeEnum();
cell.getCachedFormulaResultType();
fail("Should catch exception"); fail("Should catch exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// expected here // expected here

Loading…
Cancel
Save