Fixed HSSFSheet#getFirstRowNum and HSSFSheet#getLastRowNum to return correct values after removal of all rows

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@780245 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-05-30 11:31:14 +00:00
parent 15a46deae0
commit e7d06ec34c
3 changed files with 18 additions and 5 deletions

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.5-beta6" date="2009-??-??"> <release version="3.5-beta6" date="2009-??-??">
<action dev="POI-DEVELOPERS" type="fix">47069 - Fixed HSSFSheet#getFirstRowNum and HSSFSheet#getLastRowNum to return correct values after removal of all rows</action>
<action dev="POI-DEVELOPERS" type="fix">47278 - Fixed XSSFCell to avoid generating xsi:nil entries in shared string table</action> <action dev="POI-DEVELOPERS" type="fix">47278 - Fixed XSSFCell to avoid generating xsi:nil entries in shared string table</action>
<action dev="POI-DEVELOPERS" type="fix">47206 - Fixed XSSFCell to properly read inline strings</action> <action dev="POI-DEVELOPERS" type="fix">47206 - Fixed XSSFCell to properly read inline strings</action>
<action dev="POI-DEVELOPERS" type="add">47250 - Fixed FontRecord to expect unicode flags even when name length is zero</action> <action dev="POI-DEVELOPERS" type="add">47250 - Fixed FontRecord to expect unicode flags even when name length is zero</action>

View File

@ -253,7 +253,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*/ */
private int findLastRow(int lastrow) { private int findLastRow(int lastrow) {
if (lastrow < 1) { if (lastrow < 1) {
return -1; return 0;
} }
int rownum = lastrow - 1; int rownum = lastrow - 1;
HSSFRow r = getRow(rownum); HSSFRow r = getRow(rownum);
@ -262,7 +262,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
r = getRow(--rownum); r = getRow(--rownum);
} }
if (r == null) { if (r == null) {
return -1; return 0;
} }
return rownum; return rownum;
} }
@ -282,7 +282,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} }
if (rownum > getLastRowNum()) if (rownum > getLastRowNum())
return -1; return 0;
return rownum; return rownum;
} }
@ -300,11 +300,12 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
{ {
_sheet.addRow(row.getRowRecord()); _sheet.addRow(row.getRowRecord());
} }
if (row.getRowNum() > getLastRowNum()) boolean firstRow = _rows.size() == 1;
if (row.getRowNum() > getLastRowNum() || firstRow)
{ {
_lastrow = row.getRowNum(); _lastrow = row.getRowNum();
} }
if (row.getRowNum() < getFirstRowNum()) if (row.getRowNum() < getFirstRowNum() || firstRow)
{ {
_firstrow = row.getRowNum(); _firstrow = row.getRowNum();
} }

View File

@ -79,21 +79,32 @@ public abstract class BaseTestSheet extends TestCase {
Workbook workbook = getTestDataProvider().createWorkbook(); Workbook workbook = getTestDataProvider().createWorkbook();
Sheet sheet1 = workbook.createSheet(); Sheet sheet1 = workbook.createSheet();
assertEquals(0, sheet1.getPhysicalNumberOfRows()); assertEquals(0, sheet1.getPhysicalNumberOfRows());
assertEquals(0, sheet1.getFirstRowNum());
assertEquals(0, sheet1.getLastRowNum());
Row row0 = sheet1.createRow(0); Row row0 = sheet1.createRow(0);
assertEquals(1, sheet1.getPhysicalNumberOfRows()); assertEquals(1, sheet1.getPhysicalNumberOfRows());
assertEquals(0, sheet1.getFirstRowNum());
assertEquals(0, sheet1.getLastRowNum());
sheet1.removeRow(row0); sheet1.removeRow(row0);
assertEquals(0, sheet1.getPhysicalNumberOfRows()); assertEquals(0, sheet1.getPhysicalNumberOfRows());
assertEquals(0, sheet1.getFirstRowNum());
assertEquals(0, sheet1.getLastRowNum());
Row row1 = sheet1.createRow(1); Row row1 = sheet1.createRow(1);
Row row2 = sheet1.createRow(2); Row row2 = sheet1.createRow(2);
assertEquals(2, sheet1.getPhysicalNumberOfRows()); assertEquals(2, sheet1.getPhysicalNumberOfRows());
assertEquals(1, sheet1.getFirstRowNum());
assertEquals(2, sheet1.getLastRowNum());
assertNotNull(sheet1.getRow(1)); assertNotNull(sheet1.getRow(1));
assertNotNull(sheet1.getRow(2)); assertNotNull(sheet1.getRow(2));
sheet1.removeRow(row2); sheet1.removeRow(row2);
assertNotNull(sheet1.getRow(1)); assertNotNull(sheet1.getRow(1));
assertNull(sheet1.getRow(2)); assertNull(sheet1.getRow(2));
assertEquals(1, sheet1.getPhysicalNumberOfRows());
assertEquals(1, sheet1.getFirstRowNum());
assertEquals(1, sheet1.getLastRowNum());
Row row3 = sheet1.createRow(3); Row row3 = sheet1.createRow(3);
Sheet sheet2 = workbook.createSheet(); Sheet sheet2 = workbook.createSheet();