https://issues.apache.org/bugzilla/show_bug.cgi?id=48338

           Summary: XSSFSheet autoSizeColumn() on empty RichTextString
                    fails
           Product: POI
           Version: 3.5-FINAL
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
        AssignedTo: [email protected]
        ReportedBy: [email protected]


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.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to