From: Javen O'Neal Date: Mon, 4 Jul 2016 19:09:10 +0000 (+0000) Subject: bug 59796: XSSFTable#getRowCount off-by-one error X-Git-Tag: REL_3_15_BETA3~192 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=152ca83a2ec63ffd39b31be01192ea92c978529e;p=poi.git bug 59796: XSSFTable#getRowCount off-by-one error git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751373 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java index acf9ef69d4..d832f5bea2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java @@ -324,8 +324,9 @@ public class XSSFTable extends POIXMLDocumentPart implements Table { /** - * @return the total number of rows in the selection. (Note: in this version autofiltering is ignored) - * + * @return the total number of rows in the selection. (Note: in this version autofiltering is ignored) + * Returns 0 if the start or end cell references are not set. + * * Does not track updates to underlying changes to CTTable * To synchronize with changes to the underlying CTTable, * call {@link #updateReferences()}. @@ -334,10 +335,9 @@ public class XSSFTable extends POIXMLDocumentPart implements Table { CellReference from = getStartCellReference(); CellReference to = getEndCellReference(); - int rowCount = -1; + int rowCount = 0; if (from!=null && to!=null) { - // FIXME: shouldn't this be to-from+1? - rowCount = to.getRow()-from.getRow(); + rowCount = to.getRow() - from.getRow() + 1; } return rowCount; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java index 38edcbf6a5..69b412a51f 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java @@ -267,4 +267,24 @@ public final class TestXSSFTable { assertEquals(new CellReference("M3"), table.getEndCellReference()); } + + @Test + public void getRowCount() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sh = wb.createSheet(); + XSSFTable table = sh.createTable(); + CTTable ctTable = table.getCTTable(); + + assertEquals(0, table.getRowCount()); + + ctTable.setRef("B2:B2"); + // update cell references to clear the cache + table.updateReferences(); + assertEquals(1, table.getRowCount()); + + ctTable.setRef("B2:B12"); + // update cell references to clear the cache + table.updateReferences(); + assertEquals(11, table.getRowCount()); + } }