Browse Source

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
tags/REL_3_5_BETA5
Yegor Kozlov 15 years ago
parent
commit
5831ea5750

+ 2
- 0
src/java/org/apache/poi/hssf/model/Sheet.java 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);
}


+ 8
- 1
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java 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);

+ 5
- 0
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java 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

+ 8
- 0
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java 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);
}


+ 12
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java 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");

+ 11
- 0
src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java 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){
;
}
}
}

Loading…
Cancel
Save