fixed autoSizeColumn() to use cached formula values when processing formula cells, see bug #46736

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@745937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-02-19 17:29:55 +00:00
parent cafbc76eb7
commit 6fa67727ea
6 changed files with 47 additions and 9 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.5-beta6" date="2008-??-??"> <release version="3.5-beta6" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">fixed Sheet.autoSizeColumn() to use cached formula values when processing formula cells </action>
<action dev="POI-DEVELOPERS" type="fix">Fixed formula parser to handle names with backslashes</action> <action dev="POI-DEVELOPERS" type="fix">Fixed formula parser to handle names with backslashes</action>
<action dev="POI-DEVELOPERS" type="add">46660 - added Workbook getHidden() and setHidden(boolean)</action> <action dev="POI-DEVELOPERS" type="add">46660 - added Workbook getHidden() and setHidden(boolean)</action>
<action dev="POI-DEVELOPERS" type="fix">46693 - Fixed bugs serialization bugs in records: CHARTFORMAT, SHTPROPS, SXVD and SXVDEX</action> <action dev="POI-DEVELOPERS" type="fix">46693 - Fixed bugs serialization bugs in records: CHARTFORMAT, SHTPROPS, SXVD and SXVDEX</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.5-beta6" date="2008-??-??"> <release version="3.5-beta6" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">fixed Sheet.autoSizeColumn() to use cached formula values when processing formula cells </action>
<action dev="POI-DEVELOPERS" type="fix">Fixed formula parser to handle names with backslashes</action> <action dev="POI-DEVELOPERS" type="fix">Fixed formula parser to handle names with backslashes</action>
<action dev="POI-DEVELOPERS" type="add">46660 - added Workbook getHidden() and setHidden(boolean)</action> <action dev="POI-DEVELOPERS" type="add">46660 - added Workbook getHidden() and setHidden(boolean)</action>
<action dev="POI-DEVELOPERS" type="fix">46693 - Fixed bugs serialization bugs in records: CHARTFORMAT, SHTPROPS, SXVD and SXVDEX</action> <action dev="POI-DEVELOPERS" type="fix">46693 - Fixed bugs serialization bugs in records: CHARTFORMAT, SHTPROPS, SXVD and SXVDEX</action>

View File

@ -1699,9 +1699,12 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} }
HSSFCellStyle style = cell.getCellStyle(); HSSFCellStyle style = cell.getCellStyle();
int cellType = cell.getCellType();
if(cellType == HSSFCell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
HSSFFont font = wb.getFontAt(style.getFontIndex()); HSSFFont font = wb.getFontAt(style.getFontIndex());
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { if (cellType == HSSFCell.CELL_TYPE_STRING) {
HSSFRichTextString rt = cell.getRichStringCellValue(); HSSFRichTextString 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++) {
@ -1739,8 +1742,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} }
} else { } else {
String sval = null; String sval = null;
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
String format = style.getDataFormatString().replaceAll("\"", ""); String dfmt = style.getDataFormatString();
String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
double value = cell.getNumericCellValue(); double value = cell.getNumericCellValue();
try { try {
NumberFormat fmt; NumberFormat fmt;
@ -1754,7 +1758,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} catch (Exception e) { } catch (Exception e) {
sval = "" + value; sval = "" + value;
} }
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue()); sval = String.valueOf(cell.getBooleanCellValue());
} }
if(sval != null) { if(sval != null) {

View File

@ -131,6 +131,14 @@ public interface Cell {
*/ */
int getCellType(); int getCellType();
/**
* Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
* on the cached value of the formula
*/
int getCachedFormulaResultType();
/** /**
* Set a numeric value for the cell * Set a numeric value for the cell
* *

View File

@ -466,7 +466,28 @@ public final class XSSFCell implements Cell {
return CELL_TYPE_FORMULA; return CELL_TYPE_FORMULA;
} }
switch (this.cell.getT().intValue()) { return getBaseCellType();
}
/**
* Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
* on the cached value of the formula
*/
public int getCachedFormulaResultType() {
if (cell.getF() == null) {
throw new IllegalStateException("Only formula cells have cached results");
}
return getBaseCellType();
}
/**
* Detect cell type based on the "t" attribute of the CTCell bean
*/
private int getBaseCellType() {
switch (cell.getT().intValue()) {
case STCellType.INT_B: case STCellType.INT_B:
return CELL_TYPE_BOOLEAN; return CELL_TYPE_BOOLEAN;
case STCellType.INT_N: case STCellType.INT_N:

View File

@ -346,9 +346,11 @@ public class ColumnHelper {
} }
XSSFCellStyle style = cell.getCellStyle(); XSSFCellStyle style = cell.getCellStyle();
int cellType = cell.getCellType();
if(cellType == XSSFCell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
XSSFFont font = wb.getFontAt(style.getFontIndex()); XSSFFont font = wb.getFontAt(style.getFontIndex());
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { if (cellType == XSSFCell.CELL_TYPE_STRING) {
XSSFRichTextString rt = cell.getRichStringCellValue(); XSSFRichTextString 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++) {
@ -388,8 +390,9 @@ public class ColumnHelper {
} }
} else { } else {
String sval = null; String sval = null;
if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {
String format = style.getDataFormatString().replaceAll("\"", ""); String dfmt = style.getDataFormatString();
String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
double value = cell.getNumericCellValue(); double value = cell.getNumericCellValue();
try { try {
NumberFormat fmt; NumberFormat fmt;
@ -403,7 +406,7 @@ public class ColumnHelper {
} catch (Exception e) { } catch (Exception e) {
sval = "" + value; sval = "" + value;
} }
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { } else if (cellType == XSSFCell.CELL_TYPE_BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue()); sval = String.valueOf(cell.getBooleanCellValue());
} }
if(sval != null) { if(sval != null) {