Bug 48338 - XSSFSheet autoSizeColumn() on empty RichTextString fails
Summary: XSSFSheet autoSizeColumn() on empty RichTextString fails
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-FINAL
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2009-12-04 00:11 UTC by Martin Poelman
Modified: 2009-12-04 03:24 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
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".

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("");

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