From 67206e54506fc841f34a5bcbe8158006edd67105 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 16 Jun 2011 08:57:55 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/usermodel/HSSFSheet.java | 11 +++++++++- .../apache/poi/xssf/usermodel/XSSFSheet.java | 11 +++++++--- .../ss/usermodel/BaseTestBugzillaIssues.java | 20 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 889139d1d..c54e3d0db 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 679bfdd8d..e9bd4cf4d 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 244a9e063..204b4492f 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 5636f918e..a6ee29537 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 + } }