Sync to REL_2_BR: HSSFCell.getStringValue now returns the formula result if it is a string.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353327 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Avik Sengupta 2003-08-31 06:19:41 +00:00
parent 3a17ddd5d4
commit 6b8378de60
3 changed files with 25 additions and 0 deletions

View File

@ -267,4 +267,9 @@ public class FormulaRecordAggregate
return true; return true;
} }
public String getStringValue() {
if(stringRecord==null) return null;
return stringRecord.getString();
}
} }

View File

@ -338,6 +338,7 @@ public class HSSFCell
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue();
stringValue=((FormulaRecordAggregate) cval).getStringValue();
break; break;
case CELL_TYPE_BOOLEAN : case CELL_TYPE_BOOLEAN :
@ -820,6 +821,7 @@ public class HSSFCell
/** /**
* get the value of the cell as a string - for numeric cells we throw an exception. * 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 blank cells we return an empty string.
* For formulaCells that are not string Formulas, we return empty String
*/ */
public String getStringCellValue() public String getStringCellValue()
@ -843,6 +845,10 @@ public class HSSFCell
throw new NumberFormatException( throw new NumberFormatException(
"You cannot get a string value from an error cell"); "You cannot get a string value from an error cell");
} }
if (cellType == CELL_TYPE_FORMULA)
{
if (stringValue==null) return "";
}
return stringValue; return stringValue;
} }

View File

@ -1106,6 +1106,20 @@ extends TestCase {
assertEquals(4d, d2.getNumericCellValue(), 1e-9); 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) { public static void main(String [] args) {
System.out System.out
.println("Testing org.apache.poi.hssf.usermodel.TestFormulas"); .println("Testing org.apache.poi.hssf.usermodel.TestFormulas");