From: Yegor Kozlov Date: Fri, 6 Feb 2009 18:59:24 +0000 (+0000) Subject: Improved error handling for problems described in bugzilla 46569 - Changed Sheet... X-Git-Tag: REL_3_5_BETA5~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5831ea5750a9bd6c13574a5971bab245bfc6139c;p=poi.git Improved error handling for problems described in bugzilla 46569 - Changed Sheet.setColumnWidth to throw IllegalArgumentException if the column width argument is greater than 255 characters (the maximum column width in Excel) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@741678 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 5e528f659c..80adc84579 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -1091,6 +1091,8 @@ public final class Sheet implements Model { * (in units of 1/256th of a character width) */ public void setColumnWidth(int column, int width) { + if(width > 255*256) throw new IllegalArgumentException("The maximum column width for an individual cell is 255 characters."); + setColumn(column, null, new Integer(width), null, null, null); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index af51ce654e..5e7609db0a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -429,9 +429,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { } /** - * set the width (in units of 1/256th of a character width) + * Set the width (in units of 1/256th of a character width) + *

+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *

+ * * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width + * @throws IllegalArgumentException if width > 65536 (the maximum column width in Excel) */ public void setColumnWidth(int columnIndex, int width) { sheet.setColumnWidth(columnIndex, width); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index a6a1662c53..ad3e50b09e 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -118,6 +118,11 @@ public interface Sheet extends Iterable { /** * Set the width (in units of 1/256th of a character width) + *

+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *

* * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character 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 aa35007ff4..b1ed009358 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -1266,11 +1266,19 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Set the width (in units of 1/256th of a character width) + *

+ * The maximum column width for an individual cell is 255 characters. + * This value represents the number of characters that can be displayed + * in a cell that is formatted with the standard font. + *

* * @param columnIndex - the column to set (0-based) * @param width - the width in units of 1/256th of a character width + * @throws IllegalArgumentException if width > 65536 (the maximum column width in Excel) */ public void setColumnWidth(int columnIndex, int width) { + if(width > 255*256) throw new IllegalArgumentException("The maximum column width for an individual cell is 255 characters."); + columnHelper.setColWidth(columnIndex, (double)width/256); } 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 dd0446c9dd..61a0b5e94d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -184,6 +184,18 @@ public class TestXSSFSheet extends TestCase { assertEquals(1, sheet.getRowBreaks().length); } + public void testMaxColumnWidth() { + XSSFWorkbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + sheet.setColumnWidth(0, 255*256); //the limit + try { + sheet.setColumnWidth(0, 256*256); //the limit + fail("expected exception"); + } catch (Exception e){ + ; + } + } + public void testGetSetColumnBreaks() { XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java index 477908dc72..9afae28b13 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java +++ b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java @@ -54,4 +54,15 @@ public final class TestSheetAdditional extends TestCase { assertEquals((short)100,sheet.getColumnWidth((short)9)); assertEquals((short)100,sheet.getColumnWidth((short)10)); } + + public void testMaxColumnWidth() { + Sheet sheet = Sheet.createSheet(); + sheet.setColumnWidth(0, 255*256); //the limit + try { + sheet.setColumnWidth(0, 256*256); //the limit + fail("expected exception"); + } catch (Exception e){ + ; + } + } }