From: Yegor Kozlov Date: Thu, 16 Jun 2011 08:57:55 +0000 (+0000) Subject: Bug 50681 - Fixed autosizing columns beyond 255 character limit X-Git-Tag: REL_3_8_BETA4~381 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=64208474aa8497da8bbb77d722f1becd894324f5;p=poi.git Bug 50681 - Fixed autosizing columns beyond 255 character limit git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1136330 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 889139d1d7..c54e3d0db8 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 50681 - Fixed autosizing columns beyond 255 character limit 51374 - Fixed incorrect setting of lastPrinted OOXML core property 51351 - Word to XSL-FO converter 50458 - Fixed missing shapeId in XSSF drawings diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 679bfdd8db..e9bd4cf4d5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -1764,7 +1764,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { */ public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); - if(width != -1) setColumnWidth(column, (int) (256*width)); + + if (width != -1) { + width *= 256; + int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters + if (width > maxColumnWidth) { + width = maxColumnWidth; + } + setColumnWidth(column, (int)(width)); + } + } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 244a9e063f..204b4492fb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -381,10 +381,15 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); - if(width != -1){ + + if (width != -1) { + width *= 256; + int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters + if (width > maxColumnWidth) { + width = maxColumnWidth; + } + setColumnWidth(column, (int)(width)); columnHelper.setColBestFit(column, true); - columnHelper.setCustomWidth(column, true); - columnHelper.setColWidth(column, width); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java index 5636f918e3..a6ee295374 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -306,4 +306,24 @@ public abstract class BaseTestBugzillaIssues extends TestCase { fmla.append(")"); return fmla.toString(); } + + public final void testAutoSize_bug506819() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); + Row row = sheet.createRow(0); + Cell cell0 = row.createCell(0); + + String longValue = "www.hostname.com, www.hostname.com, " + + "www.hostname.com, www.hostname.com, www.hostname.com, " + + "www.hostname.com, www.hostname.com, www.hostname.com, " + + "www.hostname.com, www.hostname.com, www.hostname.com, " + + "www.hostname.com, www.hostname.com, www.hostname.com, " + + "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com"; + + cell0.setCellValue(longValue); + + sheet.autoSizeColumn(0); + assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters + sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point + } }