From f3f4773f288fee1fe36e93d04d9b1ae92ad683ae Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 16 Mar 2008 15:29:34 +0000 Subject: [PATCH] Patch from Josh from bug #44606 - Support calculated string values for evaluated formulas git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@637595 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/usermodel/HSSFCell.java | 50 +++++++++---------- .../poi/hssf/usermodel/TestHSSFCell.java | 23 +++++---- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index e6f5251d88..3b245dc836 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 44606 - Support calculated string values for evaluated formulas Add accessors to horizontal and vertical alignment in HSSFTextbox 44593 - Improved handling of short DVRecords 28627 / 44580 - Fix Range.delete() in HWPF diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 36aee6a5d6..e174c790dd 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 44606 - Support calculated string values for evaluated formulas Add accessors to horizontal and vertical alignment in HSSFTextbox 44593 - Improved handling of short DVRecords 28627 / 44580 - Fix Range.delete() in HWPF diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 17bae63ca7..cb2a76848a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -589,38 +589,36 @@ public class HSSFCell * 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; } - 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_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; + } + + 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) { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 6c604d1b4d..38d3b89295 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -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.FileInputStream; 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 @@ import org.apache.poi.util.TempFile; * @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. @@ -388,6 +380,17 @@ extends TestCase { 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) { System.out .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell"); -- 2.39.5