]> source.dussan.org Git - poi.git/commitdiff
Implemented row bounds. Will raise an IndexOutOfBoundsException if the excel limits...
authorJason Height <jheight@apache.org>
Thu, 14 Oct 2004 03:38:20 +0000 (03:38 +0000)
committerJason Height <jheight@apache.org>
Thu, 14 Oct 2004 03:38:20 +0000 (03:38 +0000)
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

src/java/org/apache/poi/hssf/record/RowRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java

index 6fd67c4faf368df7f1af03eabb5881c427ce4fe0..486a57e2ef6674887154cf721c6f3521ef9c01df 100644 (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;
index c84df9032e1a640fc2214e19d2269a5b828f0220..f8a33427fb785e44a9f8ec2a915161f9553bf90e 100644 (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)
         {
index c381e1f1a35794f4916123aeb0247a18dffc3d31..8c3eb00d538e4eb4e475425d37080e16c47712c3 100644 (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;
     }
 
index 8258b8b868bc923c8b3f2b34a2ddcae95d5fe2a7..c82956db614676ea75bd7c2d16a709527401ec09 100644 (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);
+    }
+    
 }