Bug 40520

Summary: HSSFFont.applyFont() formats wrong parts of HSSFRichTextString
Product: POI Reporter: Martin Jost <Martin.Jost>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.0-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: Testcase for the problerm

Description Martin Jost 2006-09-15 13:22:42 UTC
I'm using poi-bin-3.0-alpha2-20060616 because I need support for (RichText) 
formatting within a cell. The resulting sheet is read by Excel 2003 SP2.

I found a number of cases where I think, HSSFFont.applyFont() formats wrong 
parts of HSSFRichTextString. (see testcase below)

This seems to happen in (at least) the following cases:
    // overlapped range => will format whole String
    str.applyFont(0,7,font);
    str.applyFont(5,9,font);
    
    
    // formated twice => will format whole String
    str2.applyFont(0,2,font);
    str2.applyFont(0,2,font);

    // wrong order and formated twice => will format 0-6
    str3.applyFont(0,2,font);
    str3.applyFont(5,7,font);
    str3.applyFont(0,2,font);

My "font" just uses a foreground color "font.setColor((short) 2);".

Here is my complete testcase. (I will try to attach it also, but haven't seen 
an option until now to attach a file)

------------------ snip, snip ----
package poi_hssf;

import java.io.*;

import org.apache.poi.hssf.usermodel.*;


public class HSSFRichText
{
  private void newRow()
  {
    this.actRow = this.sheet.createRow(this.nextLineNr);
    this.nextLineNr++;
    this.actCol = 0;
  }


  private HSSFCell appendCell()
  {
    this.actCell = this.actRow.createCell(this.actCol++);
    return (this.actCell);
  }

  
  private void test()
  {
    File file = new File("RichPOI.xls");
    FileOutputStream outStream = null;
    try
    {
      outStream = new FileOutputStream(file);
    }
    catch (FileNotFoundException e)
    {
      System.err.println(e.getStackTrace());
    }
    
    this.wb = new HSSFWorkbook();
    this.sheet = wb.createSheet();

    HSSFFont font;
    font = this.wb.createFont();
    font.setColor((short) 2); // Rot

    HSSFCell cell;
    
    this.newRow();
    HSSFRichTextString str = new HSSFRichTextString
("f0_123456789012345678901234567890123456789012345678901234567890");    
    
    // overlapped range => will format whole String
    str.applyFont(0,7,font);
    str.applyFont(5,9,font);
    cell = this.appendCell();
    cell.setCellValue(str);
    
    
    this.newRow();
    HSSFRichTextString str2 = new HSSFRichTextString
("f1_123456789012345678901234567890123456789012345678901234567890");    
    // formated twice => will format whole String
    str2.applyFont(0,2,font);
    str2.applyFont(0,2,font);
    cell = this.appendCell();
    cell.setCellValue(str2);
    

    this.newRow();
    HSSFRichTextString str3 = new HSSFRichTextString
("f2_123456789012345678901234567890123456789012345678901234567890");    
    // wrong order => will format 0-6
    str3.applyFont(0,2,font);
    str3.applyFont(5,7,font);
    str3.applyFont(0,2,font);
    cell = this.appendCell();
    cell.setCellValue(str3);

    try
    {
      this.wb.write(outStream);
      outStream.close();
    }
    catch (IOException e)
    {
       System.err.println(e.getStackTrace());
    }    
  }
  
  
  public static void main(String[] args)
  {
      HSSFRichText testit = new HSSFRichText();
      testit.test();
  }
  
  
  private int nextLineNr;
  private short actCol;
  private HSSFWorkbook wb;
  private HSSFSheet sheet;
  private HSSFRow actRow;
  private HSSFCell actCell;
}
Comment 1 Martin Jost 2006-09-15 13:24:05 UTC
Created attachment 18869 [details]
Testcase for the problerm
Comment 2 Yegor Kozlov 2009-01-29 08:06:31 UTC
fixed  in r738908.

Yegor