Implemented row bounds. Will raise an IndexOutOfBoundsException if the excel limits are not observerd.

Interestingly column bounds were previouly implemented, but row bounds were not.

Anyhow this fixes bug 15102


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353608 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason Height 2004-10-14 03:38:20 +00:00
parent 6ab6d0a295
commit dd6a35b1bd
4 changed files with 40 additions and 8 deletions

View File

@ -35,6 +35,12 @@ public class RowRecord
implements Comparable
{
public final static short sid = 0x208;
/** The maximum row number that excel can handle (zero bazed) ie 65536 rows is
* max number of rows.
*/
public final static int MAX_ROW_NUMBER = 65535;
//private short field_1_row_number;
private int field_1_row_number;
private short field_2_first_col;

View File

@ -84,7 +84,6 @@ public class HSSFRow
//protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
protected HSSFRow(Workbook book, Sheet sheet, int rowNum)
{
this.rowNum = rowNum;
cells = new HashMap(10); // new ArrayList(INITIAL_CAPACITY);
this.book = book;
this.sheet = sheet;
@ -94,7 +93,6 @@ public class HSSFRow
row.setLastCol((short) -1);
row.setFirstCol((short) -1);
// row.setRowNumber(rowNum);
setRowNum(rowNum);
}
@ -110,17 +108,12 @@ public class HSSFRow
protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)
{
//this.rowNum = rowNum;
cells = new HashMap(); // ArrayList(INITIAL_CAPACITY);
this.book = book;
this.sheet = sheet;
row = record;
// row.setHeight(record.getHeight());
// row.setRowNumber(rowNum);
setRowNum(record.getRowNumber());
// addColumns(book, sheet, record);
}
/**
@ -206,11 +199,14 @@ public class HSSFRow
/**
* set the row number of this row.
* @param rowNum the row number (0-based)
* @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
*/
//public void setRowNum(short rowNum)
public void setRowNum(int rowNum)
{
if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER))
throw new IndexOutOfBoundsException("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">");
this.rowNum = rowNum;
if (row != null)
{

View File

@ -242,10 +242,12 @@ public class HSSFSheet
int rownum = lastrow - 1;
HSSFRow r = getRow(rownum);
while (r == null && rownum >= 0)
while (r == null && rownum > 0)
{
r = getRow(--rownum);
}
if (r == null)
return -1;
return rownum;
}

View File

@ -99,4 +99,32 @@ public class TestHSSFRow
}
public void testRowBounds()
throws Exception
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
//Test low row bound
HSSFRow row = sheet.createRow( (short) 0);
//Test low row bound exception
boolean caughtException = false;
try {
row = sheet.createRow(-1);
} catch (IndexOutOfBoundsException ex) {
caughtException = true;
}
assertTrue(caughtException);
//Test high row bound
row = sheet.createRow(65535);
//Test high row bound exception
caughtException = false;
try {
row = sheet.createRow(65536);
} catch (IndexOutOfBoundsException ex) {
caughtException = true;
}
assertTrue(caughtException);
}
}