View | Details | Raw Unified | Return to bug 44606
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (-25 / +23 lines)
Lines 589-626 Link Here
589
     * change the cell to a string cell and set its value.
589
     * change the cell to a string cell and set its value.
590
     * If value is null then we will change the cell to a Blank cell.
590
     * If value is null then we will change the cell to a Blank cell.
591
     */
591
     */
592
592
    public void setCellValue(HSSFRichTextString value) {
593
    public void setCellValue(HSSFRichTextString value)
594
    {
595
        int row=record.getRow();
593
        int row=record.getRow();
596
        short col=record.getColumn();
594
        short col=record.getColumn();
597
        short styleIndex=record.getXFIndex();
595
        short styleIndex=record.getXFIndex();
598
        if (value == null)
596
        if (value == null) {
599
        {
600
            setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
597
            setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
598
            return;
601
        }
599
        }
602
        else
600
        if (cellType == CELL_TYPE_FORMULA) {
603
        {
601
            // Set the 'pre-evaluated result' for the formula 
604
            if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA))
602
            // note - formulas do not preserve text formatting.
605
            {
603
            FormulaRecordAggregate fr = (FormulaRecordAggregate) record;
606
                setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
604
            // must make new sr because fr.getStringRecord() may be null
607
            }
605
            StringRecord sr = new StringRecord(); 
608
            int index = 0;
606
            sr.setString(value.getString()); // looses format
607
            fr.setStringRecord(sr);
608
            return;
609
        }
609
610
610
            UnicodeString str = value.getUnicodeString();            
611
        if (cellType != CELL_TYPE_STRING) {
611
//          jmh            if (encoding == ENCODING_COMPRESSED_UNICODE)
612
            setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
612
//          jmh            {
613
//          jmh                str.setCompressedUnicode();
614
//          jmh            } else if (encoding == ENCODING_UTF_16)
615
//          jmh            {
616
//          jmh                str.setUncompressedUnicode();
617
//          jmh            }
618
            index = book.addSSTString(str);            
619
            (( LabelSSTRecord ) record).setSSTIndex(index);
620
            stringValue = value;
621
            stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record));
622
            stringValue.setUnicodeString(book.getSSTString(index));            
623
        }
613
        }
614
        int index = 0;
615
616
        UnicodeString str = value.getUnicodeString();
617
        index = book.addSSTString(str);
618
        (( LabelSSTRecord ) record).setSSTIndex(index);
619
        stringValue = value;
620
        stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record));
621
        stringValue.setUnicodeString(book.getSSTString(index));
624
    }
622
    }
625
623
626
    public void setCellFormula(String formula) {
624
    public void setCellFormula(String formula) {
(-)src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (-10 / +13 lines)
Lines 1-4 Link Here
1
2
/* ====================================================================
1
/* ====================================================================
3
   Licensed to the Apache Software Foundation (ASF) under one or more
2
   Licensed to the Apache Software Foundation (ASF) under one or more
4
   contributor license agreements.  See the NOTICE file distributed with
3
   contributor license agreements.  See the NOTICE file distributed with
Lines 15-21 Link Here
15
   See the License for the specific language governing permissions and
14
   See the License for the specific language governing permissions and
16
   limitations under the License.
15
   limitations under the License.
17
==================================================================== */
16
==================================================================== */
18
        
19
17
20
package org.apache.poi.hssf.usermodel;
18
package org.apache.poi.hssf.usermodel;
21
19
Lines 24-35 Link Here
24
import java.io.FileOutputStream;
22
import java.io.FileOutputStream;
25
import java.util.Date;
23
import java.util.Date;
26
import java.util.GregorianCalendar;
24
import java.util.GregorianCalendar;
27
import java.util.List;
28
25
26
import junit.framework.AssertionFailedError;
29
import junit.framework.TestCase;
27
import junit.framework.TestCase;
30
28
31
import org.apache.poi.hssf.model.Sheet;
29
import org.apache.poi.hssf.model.Sheet;
32
import org.apache.poi.hssf.record.HyperlinkRecord;
33
import org.apache.poi.hssf.util.HSSFColor;
30
import org.apache.poi.hssf.util.HSSFColor;
34
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
31
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
35
import org.apache.poi.util.TempFile;
32
import org.apache.poi.util.TempFile;
Lines 41-53 Link Here
41
 * @author  Dan Sherman (dsherman at isisph.com)
38
 * @author  Dan Sherman (dsherman at isisph.com)
42
 * @author Alex Jacoby (ajacoby at gmail.com)
39
 * @author Alex Jacoby (ajacoby at gmail.com)
43
 */
40
 */
41
public final class TestHSSFCell extends TestCase {
44
42
45
public class TestHSSFCell
46
extends TestCase {
47
    public TestHSSFCell(String s) {
48
        super(s);
49
    }
50
51
    /**
43
    /**
52
     * test that Boolean and Error types (BoolErrRecord) are supported properly.
44
     * test that Boolean and Error types (BoolErrRecord) are supported properly.
53
     */
45
     */
Lines 388-393 Link Here
388
    	assertEquals("Formula", "A1+B1", c.toString());
380
    	assertEquals("Formula", "A1+B1", c.toString());
389
    }
381
    }
390
    
382
    
383
    public void testSetStringInFormulaCell_bug44606() {
384
        HSSFWorkbook wb = new HSSFWorkbook();
385
        HSSFCell cell = wb.createSheet("Sheet1").createRow(0).createCell((short)0);
386
        cell.setCellFormula("B1&C1");
387
        try {
388
            cell.setCellValue(new HSSFRichTextString("hello"));
389
        } catch (ClassCastException e) {
390
            throw new AssertionFailedError("Identified bug 44606");
391
        }
392
    }
393
    
391
    public static void main(String [] args) {
394
    public static void main(String [] args) {
392
        System.out
395
        System.out
393
        .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");
396
        .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");

Return to bug 44606