aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYegor Kozlov <yegor@apache.org>2009-02-19 17:29:55 +0000
committerYegor Kozlov <yegor@apache.org>2009-02-19 17:29:55 +0000
commit2ea123586aabe249b3a853ea2e7747b86cd28761 (patch)
tree3467fd57ee9257ad9e8714c61b9ae031affd5eb0
parent627105e288b7135c905d6724c0d9dd3ce77abc3b (diff)
downloadpoi-2ea123586aabe249b3a853ea2e7747b86cd28761.tar.gz
poi-2ea123586aabe249b3a853ea2e7747b86cd28761.zip
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
-rw-r--r--src/documentation/content/xdocs/changes.xml1
-rw-r--r--src/documentation/content/xdocs/status.xml1
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java12
-rw-r--r--src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java8
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java23
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java11
6 files changed, 47 insertions, 9 deletions
diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml
index c59adc2e72..9389059f55 100644
--- a/src/documentation/content/xdocs/changes.xml
+++ b/src/documentation/content/xdocs/changes.xml
@@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<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="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>
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index 386c5f998f..989c45d384 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<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="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>
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 86646286f0..86336034df 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -1699,9 +1699,12 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
HSSFCellStyle style = cell.getCellStyle();
+ int cellType = cell.getCellType();
+ if(cellType == HSSFCell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
+
HSSFFont font = wb.getFontAt(style.getFontIndex());
- if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
+ if (cellType == HSSFCell.CELL_TYPE_STRING) {
HSSFRichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (int i = 0; i < lines.length; i++) {
@@ -1739,8 +1742,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
} else {
String sval = null;
- if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
- String format = style.getDataFormatString().replaceAll("\"", "");
+ if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
+ String dfmt = style.getDataFormatString();
+ String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
double value = cell.getNumericCellValue();
try {
NumberFormat fmt;
@@ -1754,7 +1758,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} catch (Exception e) {
sval = "" + value;
}
- } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
+ } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue());
}
if(sval != null) {
diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
index 8816025a4f..aa1e8c3955 100644
--- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
+++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
@@ -132,6 +132,14 @@ public interface Cell {
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
*
* @param value the numeric value to set this cell to. For formulas we'll set the
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
index 26fb2dc8a5..433b2c650c 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
@@ -466,7 +466,28 @@ public final class XSSFCell implements Cell {
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:
return CELL_TYPE_BOOLEAN;
case STCellType.INT_N:
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
index c796776f47..30da5efd42 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
@@ -346,9 +346,11 @@ public class ColumnHelper {
}
XSSFCellStyle style = cell.getCellStyle();
+ int cellType = cell.getCellType();
+ if(cellType == XSSFCell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
XSSFFont font = wb.getFontAt(style.getFontIndex());
- if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
+ if (cellType == XSSFCell.CELL_TYPE_STRING) {
XSSFRichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (int i = 0; i < lines.length; i++) {
@@ -388,8 +390,9 @@ public class ColumnHelper {
}
} else {
String sval = null;
- if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
- String format = style.getDataFormatString().replaceAll("\"", "");
+ if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {
+ String dfmt = style.getDataFormatString();
+ String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
double value = cell.getNumericCellValue();
try {
NumberFormat fmt;
@@ -403,7 +406,7 @@ public class ColumnHelper {
} catch (Exception e) {
sval = "" + value;
}
- } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
+ } else if (cellType == XSSFCell.CELL_TYPE_BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue());
}
if(sval != null) {