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
This commit is contained in:
Yegor Kozlov 2009-02-06 18:59:24 +00:00
parent 9866e626c7
commit 5831ea5750
6 changed files with 46 additions and 1 deletions

View File

@ -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);
}

View File

@ -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)
* <p>
* 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.
* </p>
*
* @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);

View File

@ -118,6 +118,11 @@ public interface Sheet extends Iterable<Row> {
/**
* Set the width (in units of 1/256th of a character width)
* <p>
* 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.
* </p>
*
* @param columnIndex - the column to set (0-based)
* @param width - the width in units of 1/256th of a character width

View File

@ -1266,11 +1266,19 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
/**
* Set the width (in units of 1/256th of a character width)
* <p>
* 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.
* </p>
*
* @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);
}

View File

@ -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");

View File

@ -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){
;
}
}
}