Bug 48338

Summary: XSSFSheet autoSizeColumn() on empty RichTextString fails
Product: POI Reporter: Martin Poelman <poelman>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.5-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description Martin Poelman 2009-12-04 00:11:23 UTC
After filling a sheet with data I try to invoke the sheets autoSizeColumn method on a certain column but get "IllegalArgumentException: Invalid substring range".

------------------------------------
StackTrace:
------------------------------------
java.lang.IllegalArgumentException: Invalid substring range
 at java.text.AttributedString.addAttribute(AttributedString.java:332)
 at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.copyAttributes(ColumnHelper.java:447)
	at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.getColumnWidth(ColumnHelper.java:369)
	at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:307)
	at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:290)
	at [object where sheet.autoSizeColumn([columnIndex]) is called]
------------------------------------

After testing different columns I came to the conclusion this only happens on that specific column, even unfilled columns can be autosized. I searched for the reason inside the cells of that column and discovered this happens only in a specific situation. If the cell is filled with a RichTextString created with an empty String and a Font (or XSSFFont) applied the column with this cell in it can't be autosized. Example:

********** Code **********
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = new wb.createSheet("Test");
CreationHelper ch = wb.getCreationHelper();

XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);

XSSFFont font = wb.createFont();
RichTextString rts = ch.createRichtTextString("");
rts.applyFont(font);
cell.setCellValue(rts);
**************************

The code above does not work unless I invoke ch.createRichtextString(x) where x is a non empty String. So my fix atm is: ch.createRichTextString(s.isEmpty()? "" : s); where s is the String.
Comment 1 Yegor Kozlov 2009-12-04 03:24:54 UTC
Fixed in r887160

Yegor