You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/07/04 18:25:00 UTC

svn commit: r1751368 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFTable.java testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java

Author: onealj
Date: Mon Jul  4 18:25:00 2016
New Revision: 1751368

URL: http://svn.apache.org/viewvc?rev=1751368&view=rev
Log:
bug 59795: add method to synchronize start and stop cell references with CTTable

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java?rev=1751368&r1=1751367&r2=1751368&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java Mon Jul  4 18:25:00 2016
@@ -34,6 +34,7 @@ import org.apache.poi.openxml4j.opc.Pack
 import org.apache.poi.ss.usermodel.Table;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
+import org.apache.poi.util.Internal;
 import org.apache.poi.util.StringUtil;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
@@ -113,6 +114,10 @@ public class XSSFTable extends POIXMLDoc
         out.close();
     }
     
+    /**
+      * get the underlying CTTable XML bean
+      */
+    @Internal
     public CTTable getCTTable() {
         return ctTable;
     }
@@ -262,15 +267,12 @@ public class XSSFTable extends POIXMLDoc
      * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref) 
      *
      * Does not track updates to underlying changes to CTTable
+     * To synchronize with changes to the underlying CTTable,
+     * call {@link #updateReferences()}.
      */
     public CellReference getStartCellReference() {
         if (startCellReference==null) {
-            String ref = ctTable.getRef();
-            if (ref != null) {
-                String[] boundaries = ref.split(":");
-                String from = boundaries[0];
-                startCellReference = new CellReference(from);
-            }
+             setCellReferences();
         }
         return startCellReference;
     }
@@ -280,22 +282,53 @@ public class XSSFTable extends POIXMLDoc
      * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
      *
      * Does not track updates to underlying changes to CTTable
+     * To synchronize with changes to the underlying CTTable,
+     * call {@link #updateReferences()}.
      */
     public CellReference getEndCellReference() {
         if (endCellReference==null) {
-            String ref = ctTable.getRef();
-            String[] boundaries = ref.split(":");
-            String from = boundaries[1];
-            endCellReference = new CellReference(from);
+             setCellReferences();
         }
         return endCellReference;
     }
+
+    /**
+      * @since POI 3.15 beta 3
+      */
+    private void setCellReferences() {
+        String ref = ctTable.getRef();
+        if (ref != null) {
+            String[] boundaries = ref.split(":", 2);
+            String from = boundaries[0];
+            String to = boundaries[1];
+            startCellReference = new CellReference(from);
+            endCellReference = new CellReference(to);
+        }
+    }
+
     
+    /**
+     * Clears the cached values set by {@link #getStartCellReference()}
+     * and {@link #getEndCellReference()}.
+     * The next call to {@link #getStartCellReference()} and
+     * {@link #getEndCellReference()} will synchronize the
+     * cell references with the underlying <code>CTTable</code>.
+     * Thus, {@link #updateReferences()} is inexpensive.
+     *
+     * @since POI 3.15 beta 3
+     */
+    public void updateReferences() {
+        startCellReference = null;
+        endCellReference = null;
+    }
+
     
     /**
      *  @return the total number of rows in the selection. (Note: in this version autofiltering is ignored)
      *
      * Does not track updates to underlying changes to CTTable
+     * To synchronize with changes to the underlying CTTable,
+     * call {@link #updateReferences()}.
      */
     public int getRowCount() {
         CellReference from = getStartCellReference();
@@ -303,6 +336,7 @@ public class XSSFTable extends POIXMLDoc
         
         int rowCount = -1;
         if (from!=null && to!=null) {
+            // FIXME: shouldn't this be to-from+1?
             rowCount = to.getRow()-from.getRow();
         }
         return rowCount;
@@ -312,6 +346,10 @@ public class XSSFTable extends POIXMLDoc
      * Synchronize table headers with cell values in the parent sheet.
      * Headers <em>must</em> be in sync, otherwise Excel will display a
      * "Found unreadable content" message on startup.
+     * 
+     * If calling both {@link #updateReferences()} and
+     * {@link #updateHeaders()}, {@link #updateReferences()}
+     * should be called first.
      */
     public void updateHeaders() {
         XSSFSheet sheet = (XSSFSheet)getParent();

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java?rev=1751368&r1=1751367&r2=1751368&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java Mon Jul  4 18:25:00 2016
@@ -238,4 +238,33 @@ public final class TestXSSFTable {
 
         wb.close();
     }
+
+    @Test
+    public void getCellReferences() {
+        // make sure that cached start and end cell references
+        // can be synchronized with the underlying CTTable
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFSheet sh = wb.createSheet();
+        XSSFTable table = sh.createTable();
+        CTTable ctTable = table.getCTTable();
+        ctTable.setRef("B2:E8");
+
+        assertEquals(new CellReference("B2"), table.getStartCellReference());
+        assertEquals(new CellReference("E8"), table.getEndCellReference());
+
+        // At this point start and end cell reference are cached
+        // and may not follow changes to the underlying CTTable
+        ctTable.setRef("C1:M3");
+
+        assertEquals(new CellReference("B2"), table.getStartCellReference());
+        assertEquals(new CellReference("E8"), table.getEndCellReference());
+
+        // Force a synchronization between CTTable and XSSFTable
+        // start and end cell references
+        table.updateReferences();
+
+        assertEquals(new CellReference("C1"), table.getStartCellReference());
+        assertEquals(new CellReference("M3"), table.getEndCellReference());
+
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org