From 5d9a1169cd605af2a0ff2017eea3d7fbf194f7d7 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Fri, 4 Dec 2009 11:24:02 +0000 Subject: [PATCH] fixed XSSFSheet autoSizeColumn() to tolerate empty RichTextString, see Bugzilla 48332 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@887160 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../xssf/usermodel/helpers/ColumnHelper.java | 6 +++-- .../poi/xssf/usermodel/TestXSSFSheet.java | 22 ++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8229f2a3bc..c949c66e96 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 48332 - fixed XSSFSheet autoSizeColumn() to tolerate empty RichTextString 48332 - fixed ColumnInfoRecord to tolerate missing reserved field 47701 - fixed RecordFormatException when reading list subrecords (LbsDataSubRecord) memory usage optimization in XSSF - avoid creating parentless xml beans diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java index 8359ab818d..cad16aeeba 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -366,8 +366,10 @@ public class ColumnHelper { XSSFFont fnt = rt.getFontOfFormattingRun(j); if (fnt != null) { int len = rt.getLengthOfFormattingRun(j); - copyAttributes(fnt, str, pos, pos + len); - pos += len; + if(len > 0) { //ignore degenerate zero-length runs + copyAttributes(fnt, str, pos, pos + len); + pos += len; + } } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index ff7764c82e..b66d85e2a8 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -17,9 +17,7 @@ package org.apache.poi.xssf.usermodel; -import org.apache.poi.ss.usermodel.BaseTestSheet; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -158,6 +156,24 @@ public class TestXSSFSheet extends BaseTestSheet { assertTrue(col.getBestFit()); } + /** + * XSSFSheet autoSizeColumn() on empty RichTextString fails + */ + public void test48325() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet("Test"); + CreationHelper factory = wb.getCreationHelper(); + + XSSFRow row = sheet.createRow(0); + XSSFCell cell = row.createCell(0); + + XSSFFont font = wb.createFont(); + RichTextString rts = factory.createRichTextString(""); + rts.applyFont(font); + cell.setCellValue(rts); + + sheet.autoSizeColumn(0); + } public void testGetCellComment() { XSSFWorkbook workbook = new XSSFWorkbook();