diff options
author | Dominik Stadler <centic@apache.org> | 2019-12-15 14:52:45 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2019-12-15 14:52:45 +0000 |
commit | 2748844549b50dc11e8ef19dcd506c820c1a1e19 (patch) | |
tree | aac3ba192034ff20e75a065a116e4424de0a5870 /src | |
parent | 57e5e87e58c8eabfc2fd1f3b14808814d668a83f (diff) | |
download | poi-2748844549b50dc11e8ef19dcd506c820c1a1e19.tar.gz poi-2748844549b50dc11e8ef19dcd506c820c1a1e19.zip |
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
Diffstat (limited to 'src')
9 files changed, 54 insertions, 19 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index c47cc1c6ac..f398c914cf 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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; + } } } diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java index 517cbec353..54b4f4303e 100644 --- a/src/java/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java @@ -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(); diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index 2139111f5d..f639dc12e7 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -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(); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index dcbe55cbd2..58fe33134b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -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 diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index b0a5474040..67da55b3a5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -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); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 46056ff82e..b0aa06cb4e 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -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 diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java index 6e663c37ee..1634330138 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java @@ -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()); + } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java index 657442026f..02930a9959 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -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); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestXEvaluationSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestXEvaluationSheet.java index cd60b3f6ff..f21842b4ec 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestXEvaluationSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestXEvaluationSheet.java @@ -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); |