poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1797919 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFTable.java testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
Date Wed, 07 Jun 2017 13:06:39 GMT
Author: nick
Date: Wed Jun  7 13:06:38 2017
New Revision: 1797919

URL: http://svn.apache.org/viewvc?rev=1797919&view=rev
Log:
Make it possible to create simple XSSF sheet tables without needing CT classes directly

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=1797919&r1=1797918&r2=1797919&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 Wed Jun  7 13:06:38
2017
@@ -42,6 +42,7 @@ import org.apache.poi.xssf.usermodel.hel
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument;
 
 /**
@@ -215,7 +216,6 @@ public class XSSFTable extends POIXMLDoc
      * @return List of XSSFXmlColumnPr
      */
     public List<XSSFXmlColumnPr> getXmlColumnPrs() {
-        
         if (xmlColumnPr==null) {
             xmlColumnPr = new ArrayList<XSSFXmlColumnPr>();
             for (CTTableColumn column: getTableColumns()) {
@@ -229,6 +229,29 @@ public class XSSFTable extends POIXMLDoc
     }
     
     /**
+     * Adds another column to the table.
+     * 
+     * Warning - Return type likely to change!
+     */
+    @Internal("Return type likely to change")
+    public void addColumn() {
+        // Ensure we have Table Columns
+        CTTableColumns columns = ctTable.getTableColumns();
+        if (columns == null) {
+            columns = ctTable.addNewTableColumns();
+        }
+        
+        // Add another Column, and give it a sensible ID
+        CTTableColumn column = columns.addNewTableColumn();
+        int num = columns.sizeOfTableColumnArray();
+        columns.setCount(num);
+        column.setId(num);
+        
+        // Have the Headers updated if possible
+        updateHeaders();
+    }
+    
+    /**
      * @return the name of the Table, if set
      */
     public String getName() {
@@ -321,12 +344,34 @@ public class XSSFTable extends POIXMLDoc
      * To synchronize with changes to the underlying CTTable,
      * call {@link #updateReferences()}.
      */
-    public AreaReference getReferences() {
+    public AreaReference getCellReferences() {
         return new AreaReference(
                 getStartCellReference(),
                 getEndCellReference()
         );
     }
+    /**
+     * Updates the reference for the cells of the table
+     * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
+     * and synchronizes any changes
+     */
+    public void setCellReferences(AreaReference refs) {
+        // Strip the Sheet name
+        String ref = refs.formatAsString();
+        if (ref.indexOf('!') != -1) {
+            ref = ref.substring(ref.indexOf('!')+1);
+        }
+        
+        // Update
+        ctTable.setRef(ref);
+        if (ctTable.isSetAutoFilter()) {
+            ctTable.getAutoFilter().setRef(ref);
+        }
+        
+        // Have everything recomputed
+        updateReferences();
+        updateHeaders();
+    }
     
     /**
      * @return The reference for the cell in the top-left part of the table

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=1797919&r1=1797918&r2=1797919&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 Wed Jun
 7 13:06:38 2017
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.util.AreaReference;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -301,21 +302,21 @@ public final class TestXSSFTable {
         s = wb.getSheet("IntHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B2", t.getReferences().formatAsString());
+        assertEquals("A1:B2", t.getCellReferences().formatAsString());
         assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("34", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
         
         s = wb.getSheet("FloatHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B2", t.getReferences().formatAsString());
+        assertEquals("A1:B2", t.getCellReferences().formatAsString());
         assertEquals("12.34", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
         
         s = wb.getSheet("NoExplicitHeaders");
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
-        assertEquals("A1:B3", t.getReferences().formatAsString());
+        assertEquals("A1:B3", t.getCellReferences().formatAsString());
         assertEquals("Column1", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
         assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
     }
@@ -327,30 +328,31 @@ public final class TestXSSFTable {
     public void testNumericCellsInTable() throws IOException {
         XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet s = wb.createSheet();
-
-        //Setting up the CTTable
-        XSSFTable t = s.createTable();
-        CTTable ctt = t.getCTTable();
-        ctt.setId(1);
-        ctt.setName("CT Table Test");
-        ctt.setRef("A1:B2");
-        CTTableColumns cttcs = ctt.addNewTableColumns();
-        CTTableColumn cttc1 = cttcs.addNewTableColumn();
-        cttc1.setId(1);
-        CTTableColumn cttc2 = cttcs.addNewTableColumn();
-        cttc2.setId(2);
-
-        //Creating the cells
+        
+        // Create some cells, some numeric, some not
         Cell c1 = s.createRow(0).createCell(0);
-        XSSFCell c2 = s.getRow(0).createCell(1);
-        XSSFCell c3 = s.createRow(1).createCell(0);
-        XSSFCell c4 = s.getRow(1).createCell(1);
-
-        // Inserting values; some numeric strings, some alphabetical strings
+        Cell c2 = s.getRow(0).createCell(1);
+        Cell c3 = s.getRow(0).createCell(2);
+        Cell c4 = s.createRow(1).createCell(0);
+        Cell c5 = s.getRow(1).createCell(1);
+        Cell c6 = s.getRow(1).createCell(2);
         c1.setCellValue(12);
-        c2.setCellValue(34); 
-        c3.setCellValue("AB");
-        c4.setCellValue("CD");
+        c2.setCellValue(34.56);
+        c3.setCellValue("ABCD");
+        c4.setCellValue("AB");
+        c5.setCellValue("CD");
+        c6.setCellValue("EF");
+
+        // Setting up the CTTable
+        XSSFTable t = s.createTable();
+        t.setName("TableTest");
+        t.setDisplayName("CT Table Test");
+        t.addColumn();
+        t.addColumn();
+        t.addColumn();
+        t.setCellReferences(new AreaReference(
+                new CellReference(c1), new CellReference(c6)
+        ));
 
         // Save and re-load
         wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
@@ -360,7 +362,12 @@ public final class TestXSSFTable {
         assertEquals(1, s.getTables().size());
         t = s.getTables().get(0);
         assertEquals("A1", t.getStartCellReference().formatAsString());
-        assertEquals("B2", t.getEndCellReference().formatAsString());
+        assertEquals("C2", t.getEndCellReference().formatAsString());
+        
+        // TODO Nicer column fetching
+        assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
+        assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
+        assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
         
         // Done
         wb.close();



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


Mime
View raw message