Author: nick Date: Sun Mar 16 08:29:34 2008 New Revision: 637595 URL: http://svn.apache.org/viewvc?rev=637595&view=rev Log: Patch from Josh from bug #44606 - Support calculated string values for evaluated formulas
Modified: poi/trunk/src/documentation/content/xdocs/changes.xml poi/trunk/src/documentation/content/xdocs/status.xml poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Modified: poi/trunk/src/documentation/content/xdocs/changes.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=637595&r1=637594&r2=637595&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/changes.xml (original) +++ poi/trunk/src/documentation/content/xdocs/changes.xml Sun Mar 16 08:29:34 2008 @@ -36,6 +36,7 @@ <!-- Don't forget to update status.xml too! --> <release version="3.1-beta1" date="2008-??-??"> + <action dev="POI-DEVELOPERS" type="fix">44606 - Support calculated string values for evaluated formulas</action> <action dev="POI-DEVELOPERS" type="add">Add accessors to horizontal and vertical alignment in HSSFTextbox</action> <action dev="POI-DEVELOPERS" type="add">44593 - Improved handling of short DVRecords</action> <action dev="POI-DEVELOPERS" type="add">28627 / 44580 - Fix Range.delete() in HWPF</action> Modified: poi/trunk/src/documentation/content/xdocs/status.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=637595&r1=637594&r2=637595&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/status.xml (original) +++ poi/trunk/src/documentation/content/xdocs/status.xml Sun Mar 16 08:29:34 2008 @@ -33,6 +33,7 @@ <!-- Don't forget to update changes.xml too! --> <changes> <release version="3.1-beta1" date="2008-??-??"> + <action dev="POI-DEVELOPERS" type="fix">44606 - Support calculated string values for evaluated formulas</action> <action dev="POI-DEVELOPERS" type="add">Add accessors to horizontal and vertical alignment in HSSFTextbox</action> <action dev="POI-DEVELOPERS" type="add">44593 - Improved handling of short DVRecords</action> <action dev="POI-DEVELOPERS" type="add">28627 / 44580 - Fix Range.delete() in HWPF</action> Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=637595&r1=637594&r2=637595&view=diff ============================================================================== --- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original) +++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Sun Mar 16 08:29:34 2008 @@ -589,38 +589,36 @@ * change the cell to a string cell and set its value. * If value is null then we will change the cell to a Blank cell. */ - - public void setCellValue(HSSFRichTextString value) - { + public void setCellValue(HSSFRichTextString value) { int row=record.getRow(); short col=record.getColumn(); short styleIndex=record.getXFIndex(); - if (value == null) - { + if (value == null) { setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex); + return; + } + if (cellType == CELL_TYPE_FORMULA) { + // Set the 'pre-evaluated result' for the formula + // note - formulas do not preserve text formatting. + FormulaRecordAggregate fr = (FormulaRecordAggregate) record; + // must make new sr because fr.getStringRecord() may be null + StringRecord sr = new StringRecord(); + sr.setString(value.getString()); // looses format + fr.setStringRecord(sr); + return; } - else - { - if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA)) - { - setCellType(CELL_TYPE_STRING, false, row, col, styleIndex); - } - int index = 0; - UnicodeString str = value.getUnicodeString(); -// jmh if (encoding == ENCODING_COMPRESSED_UNICODE) -// jmh { -// jmh str.setCompressedUnicode(); -// jmh } else if (encoding == ENCODING_UTF_16) -// jmh { -// jmh str.setUncompressedUnicode(); -// jmh } - index = book.addSSTString(str); - (( LabelSSTRecord ) record).setSSTIndex(index); - stringValue = value; - stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record)); - stringValue.setUnicodeString(book.getSSTString(index)); + if (cellType != CELL_TYPE_STRING) { + setCellType(CELL_TYPE_STRING, false, row, col, styleIndex); } + int index = 0; + + UnicodeString str = value.getUnicodeString(); + index = book.addSSTString(str); + (( LabelSSTRecord ) record).setSSTIndex(index); + stringValue = value; + stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record)); + stringValue.setUnicodeString(book.getSSTString(index)); } public void setCellFormula(String formula) { Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java?rev=637595&r1=637594&r2=637595&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Sun Mar 16 08:29:34 2008 @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.usermodel; @@ -24,12 +22,11 @@ import java.io.FileOutputStream; import java.util.Date; import java.util.GregorianCalendar; -import java.util.List; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.model.Sheet; -import org.apache.poi.hssf.record.HyperlinkRecord; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.TempFile; @@ -41,12 +38,7 @@ * @author Dan Sherman (dsherman at isisph.com) * @author Alex Jacoby (ajacoby at gmail.com) */ - -public class TestHSSFCell -extends TestCase { - public TestHSSFCell(String s) { - super(s); - } +public final class TestHSSFCell extends TestCase { /** * test that Boolean and Error types (BoolErrRecord) are supported properly. @@ -386,6 +378,17 @@ assertEquals("Error", "#ERR7", c.toString()); c=r.getCell((short)4); assertEquals("Formula", "A1+B1", c.toString()); + } + + public void testSetStringInFormulaCell_bug44606() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFCell cell = wb.createSheet("Sheet1").createRow(0).createCell((short)0); + cell.setCellFormula("B1&C1"); + try { + cell.setCellValue(new HSSFRichTextString("hello")); + } catch (ClassCastException e) { + throw new AssertionFailedError("Identified bug 44606"); + } } public static void main(String [] args) { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]