From b0473f7518a09da7f582692407dbdab517f5e88c Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Sat, 30 Aug 2003 13:41:56 +0000 Subject: [PATCH] access the result of string formulas getStringValue on HSSFCell now returns the formula result if it's a string formula git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353325 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/aggregates/FormulaRecordAggregate.java | 5 +++++ .../org/apache/poi/hssf/usermodel/HSSFCell.java | 6 ++++++ .../apache/poi/hssf/usermodel/TestFormulas.java | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java index e3eb40e201..761d821dcf 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java @@ -266,5 +266,10 @@ public class FormulaRecordAggregate return true; } + + public String getStringValue() { + if(stringRecord==null) return null; + return stringRecord.getString(); + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index c9a31854dc..7ebd7e38d3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -326,6 +326,7 @@ public class HSSFCell case CELL_TYPE_FORMULA : cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); + stringValue=((FormulaRecordAggregate) cval).getStringValue(); break; case CELL_TYPE_BOOLEAN : @@ -797,6 +798,7 @@ public class HSSFCell /** * get the value of the cell as a string - for numeric cells we throw an exception. * For blank cells we return an empty string. + * For formulaCells that are not string Formulas, we return empty String */ public String getStringCellValue() @@ -820,6 +822,10 @@ public class HSSFCell throw new NumberFormatException( "You cannot get a string value from an error cell"); } + if (cellType == CELL_TYPE_FORMULA) + { + if (stringValue==null) return ""; + } return stringValue; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java index 0baf1a3b67..5b75303e12 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java @@ -1106,6 +1106,20 @@ extends TestCase { assertEquals(4d, d2.getNumericCellValue(), 1e-9); } + public void testStringFormulaRead() throws IOException { + File dir = new File(System.getProperty("HSSF.testdata.path")); + File xls = new File(dir, "StringFormulas.xls"); + FileInputStream in = new FileInputStream(xls); + HSSFWorkbook w; + try { + w = new HSSFWorkbook(in); + } finally { + in.close(); + } + HSSFCell c = w.getSheetAt(0).getRow(0).getCell((short)0); + assertEquals("String Cell value","XYZ",c.getStringCellValue()); + } + public static void main(String [] args) { System.out .println("Testing org.apache.poi.hssf.usermodel.TestFormulas"); -- 2.39.5