mirror of https://github.com/apache/poi.git
Bug 63749; Make getLastRowNum() and getFirstRow() return -1 instead of 0 on empty Sheets
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871589 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
57e5e87e58
commit
2748844549
|
@ -114,8 +114,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
|
|||
protected final InternalWorkbook _book;
|
||||
protected final HSSFWorkbook _workbook;
|
||||
private HSSFPatriarch _patriarch;
|
||||
private int _firstrow;
|
||||
private int _lastrow;
|
||||
private int _firstrow = -1;
|
||||
private int _lastrow = -1;
|
||||
|
||||
/**
|
||||
* Creates new HSSFSheet - called by HSSFWorkbook to create a sheet from
|
||||
|
@ -316,6 +316,12 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
|
|||
_firstrow = findFirstRow(_firstrow);
|
||||
}
|
||||
_sheet.removeRow(hrow.getRowRecord());
|
||||
|
||||
// if there are no more rows, then reset first/last
|
||||
if(_rows.size() == 0) {
|
||||
_firstrow = -1;
|
||||
_lastrow = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -99,6 +99,7 @@ public interface Sheet extends Iterable<Row> {
|
|||
* than expected!
|
||||
*
|
||||
* @return the number of the first logical row on the sheet (0-based)
|
||||
* or -1 if no row exists
|
||||
*/
|
||||
int getFirstRowNum();
|
||||
|
||||
|
@ -110,7 +111,7 @@ public interface Sheet extends Iterable<Row> {
|
|||
* method will include such rows and thus the returned value might be higher
|
||||
* than expected!
|
||||
*
|
||||
* @return last row contained on this sheet (0-based)
|
||||
* @return last row contained on this sheet (0-based) or -1 if no row exists
|
||||
*/
|
||||
int getLastRowNum();
|
||||
|
||||
|
|
|
@ -206,7 +206,7 @@ public class SXSSFSheet implements Sheet
|
|||
if(_writer.getNumberOfFlushedRows() > 0) {
|
||||
return _writer.getLowestIndexOfFlushedRows();
|
||||
}
|
||||
return _rows.size() == 0 ? 0 : _rows.firstKey();
|
||||
return _rows.size() == 0 ? -1 : _rows.firstKey();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -217,7 +217,7 @@ public class SXSSFSheet implements Sheet
|
|||
@Override
|
||||
public int getLastRowNum()
|
||||
{
|
||||
return _rows.size() == 0 ? 0 : _rows.lastKey();
|
||||
return _rows.size() == 0 ? -1 : _rows.lastKey();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1098,7 +1098,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
|||
*/
|
||||
@Override
|
||||
public int getFirstRowNum() {
|
||||
return _rows.isEmpty() ? 0 : _rows.firstKey();
|
||||
return _rows.isEmpty() ? -1 : _rows.firstKey();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1220,7 +1220,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
|||
// A test with 1_000_000 rows shows that querying getLastRowNum with lastKey() implementation takes ~40 ms,
|
||||
// and ~1.2 ms with cached implementation. 40 ms is negligible compared to the time of evaluation a million
|
||||
// cells, and the lastKey implementation is much more elegant and less error prone than caching.
|
||||
return _rows.isEmpty() ? 0 : _rows.lastKey();
|
||||
return _rows.isEmpty() ? -1 : _rows.lastKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -113,8 +113,8 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
|
|||
assertEquals(1, wb1.getSheetAt(0).getLastRowNum());
|
||||
assertEquals(0, wb1.getSheetAt(1).getFirstRowNum());
|
||||
assertEquals(0, wb1.getSheetAt(1).getLastRowNum());
|
||||
assertEquals(0, wb1.getSheetAt(2).getFirstRowNum());
|
||||
assertEquals(0, wb1.getSheetAt(2).getLastRowNum());
|
||||
assertEquals(-1, wb1.getSheetAt(2).getFirstRowNum());
|
||||
assertEquals(-1, wb1.getSheetAt(2).getLastRowNum());
|
||||
|
||||
File file = TempFile.createTempFile("poi-", ".xlsx");
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
|
@ -151,8 +151,8 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
|
|||
assertEquals(1, wb2.getSheetAt(0).getLastRowNum());
|
||||
assertEquals(0, wb2.getSheetAt(1).getFirstRowNum());
|
||||
assertEquals(0, wb2.getSheetAt(1).getLastRowNum());
|
||||
assertEquals(0, wb2.getSheetAt(2).getFirstRowNum());
|
||||
assertEquals(0, wb2.getSheetAt(2).getLastRowNum());
|
||||
assertEquals(-1, wb2.getSheetAt(2).getFirstRowNum());
|
||||
assertEquals(-1, wb2.getSheetAt(2).getLastRowNum());
|
||||
|
||||
sheet1 = wb2.getSheetAt(0);
|
||||
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
|
||||
|
|
|
@ -1230,9 +1230,9 @@ public final class TestBugs extends BaseTestBugzillaIssues {
|
|||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
HSSFSheet s = wb.createSheet();
|
||||
|
||||
// No rows, everything is 0
|
||||
assertEquals(0, s.getFirstRowNum());
|
||||
assertEquals(0, s.getLastRowNum());
|
||||
// No rows, first/last return -1
|
||||
assertEquals(-1, s.getFirstRowNum());
|
||||
assertEquals(-1, s.getLastRowNum());
|
||||
assertEquals(0, s.getPhysicalNumberOfRows());
|
||||
|
||||
// One row, most things are 0, physical is 1
|
||||
|
|
|
@ -532,4 +532,32 @@ public abstract class BaseTestRow {
|
|||
assertNull(f1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastRowEmptySheet() {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet("sheet1");
|
||||
|
||||
assertEquals("Sheet without rows should return -1 as lastRowNum",
|
||||
-1, sheet.getLastRowNum());
|
||||
Row row = sheet.createRow(0);
|
||||
assertNotNull(row);
|
||||
|
||||
assertEquals("Sheet with one row should return 0 as lastRowNum",
|
||||
0, sheet.getLastRowNum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstRowEmptySheet() {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet("sheet1");
|
||||
|
||||
assertEquals("Sheet without rows should return -1 as firstRowNum",
|
||||
-1, sheet.getFirstRowNum());
|
||||
Row row = sheet.createRow(0);
|
||||
assertNotNull(row);
|
||||
|
||||
assertEquals("Sheet with one row should return 0 as firstRowNum",
|
||||
0, sheet.getFirstRowNum());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -130,8 +130,8 @@ public abstract class BaseTestSheet {
|
|||
Workbook workbook = _testDataProvider.createWorkbook();
|
||||
Sheet sheet1 = workbook.createSheet();
|
||||
assertEquals(0, sheet1.getPhysicalNumberOfRows());
|
||||
assertEquals(0, sheet1.getFirstRowNum());
|
||||
assertEquals(0, sheet1.getLastRowNum());
|
||||
assertEquals(-1, sheet1.getFirstRowNum());
|
||||
assertEquals(-1, sheet1.getLastRowNum());
|
||||
|
||||
Row row0 = sheet1.createRow(0);
|
||||
assertEquals(1, sheet1.getPhysicalNumberOfRows());
|
||||
|
@ -139,8 +139,8 @@ public abstract class BaseTestSheet {
|
|||
assertEquals(0, sheet1.getLastRowNum());
|
||||
sheet1.removeRow(row0);
|
||||
assertEquals(0, sheet1.getPhysicalNumberOfRows());
|
||||
assertEquals(0, sheet1.getFirstRowNum());
|
||||
assertEquals(0, sheet1.getLastRowNum());
|
||||
assertEquals(-1, sheet1.getFirstRowNum());
|
||||
assertEquals(-1, sheet1.getLastRowNum());
|
||||
|
||||
sheet1.createRow(1);
|
||||
Row row2 = sheet1.createRow(2);
|
||||
|
|
|
@ -36,7 +36,7 @@ public abstract class BaseTestXEvaluationSheet {
|
|||
Sheet underlyingSheet = sheetPair.getKey();
|
||||
EvaluationSheet instance = sheetPair.getValue();
|
||||
|
||||
assertEquals(0, instance.getLastRowNum());
|
||||
assertEquals(-1, instance.getLastRowNum());
|
||||
|
||||
underlyingSheet.createRow(0);
|
||||
underlyingSheet.createRow(1);
|
||||
|
|
Loading…
Reference in New Issue