You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2014/07/30 17:44:05 UTC

svn commit: r1614691 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java

Author: nick
Date: Wed Jul 30 15:44:05 2014
New Revision: 1614691

URL: http://svn.apache.org/r1614691
Log:
Add row label getter for Pivot Tables, and use this in unit tests

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java?rev=1614691&r1=1614690&r2=1614691&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java Wed Jul 30 15:44:05 2014
@@ -19,6 +19,8 @@ package org.apache.poi.xssf.usermodel;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import javax.xml.namespace.QName;
@@ -39,6 +41,7 @@ import org.openxmlformats.schemas.spread
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColFields;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataField;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTField;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItems;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLocation;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageField;
@@ -216,14 +219,19 @@ public class XSSFPivotTable extends POIX
         style.setShowRowHeaders(true);
     }
 
+    protected AreaReference getPivotArea() {
+        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
+                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        return pivotArea;
+    }
+
     /**
      * Add a row label using data from the given column.
      * @param columnIndex, the index of the column to be used as row label.
      */
     @Beta
     public void addRowLabel(int columnIndex) {
-        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
-                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        AreaReference pivotArea = getPivotArea();
         int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
         int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
 
@@ -256,6 +264,20 @@ public class XSSFPivotTable extends POIX
         rowFields.addNewField().setX(columnIndex);
         rowFields.setCount(rowFields.getFieldList().size());
     }
+    
+    @Beta
+    @SuppressWarnings("deprecation")
+    public List<Integer> getRowLabelColumns() {
+        if (pivotTableDefinition.getRowFields() != null) {
+            List<Integer> columnIndexes = new ArrayList<Integer>();
+            for (CTField f : pivotTableDefinition.getRowFields().getFieldArray()) {
+                columnIndexes.add(f.getX());
+            }
+            return columnIndexes;
+        } else {
+            return Collections.emptyList();
+        }
+    }
 
     /**
      * Add a column label using data from the given column and specified function
@@ -266,8 +288,7 @@ public class XSSFPivotTable extends POIX
      */
     @Beta
     public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
-        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
-                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        AreaReference pivotArea = getPivotArea();
         int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
 
         if(columnIndex > lastColIndex && columnIndex < 0) {
@@ -299,8 +320,7 @@ public class XSSFPivotTable extends POIX
      */
     @Beta
     private void addDataField(DataConsolidateFunction function, int columnIndex) {
-        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
-                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        AreaReference pivotArea = getPivotArea();
         int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
 
         if(columnIndex > lastColIndex && columnIndex < 0) {
@@ -328,8 +348,7 @@ public class XSSFPivotTable extends POIX
      */
     @Beta
     public void addDataColumn(int columnIndex, boolean isDataField) {
-        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
-                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        AreaReference pivotArea = getPivotArea();
         int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
         if(columnIndex > lastColIndex && columnIndex < 0) {
             throw new IndexOutOfBoundsException();
@@ -350,8 +369,7 @@ public class XSSFPivotTable extends POIX
      */
     @Beta
     public void addReportFilter(int columnIndex) {
-        AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
-                getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
+        AreaReference pivotArea = getPivotArea();
         int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
         int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
 
@@ -432,9 +450,7 @@ public class XSSFPivotTable extends POIX
         } else {
             pivotFields = pivotTableDefinition.addNewPivotFields();
         }
-        String source = pivotCacheDefinition.getCTPivotCacheDefinition().
-                getCacheSource().getWorksheetSource().getRef();
-        AreaReference sourceArea = new AreaReference(source);
+        AreaReference sourceArea = getPivotArea();
         int firstColumn = sourceArea.getFirstCell().getCol();
         int lastColumn = sourceArea.getLastCell().getCol();
         CTPivotField pivotField;
@@ -445,4 +461,4 @@ public class XSSFPivotTable extends POIX
         }
         pivotFields.setCount(pivotFields.getPivotFieldList().size());
     }
-}
\ No newline at end of file
+}

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java?rev=1614691&r1=1614690&r2=1614691&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java Wed Jul 30 15:44:05 2014
@@ -67,18 +67,28 @@ public class TestXSSFPivotTable extends 
         pivotTable = sheet.createPivotTable(source, new CellReference("H5"));
     }
 
-    /*
+    /**
      * Verify that when creating a row label it's  created on the correct row
      * and the count is increased by one.
      */
     public void testAddRowLabelToPivotTable() {
         int columnIndex = 0;
 
+        assertEquals(0, pivotTable.getRowLabelColumns().size());
+        
         pivotTable.addRowLabel(columnIndex);
         CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
 
         assertEquals(defintion.getRowFields().getFieldArray(0).getX(), columnIndex);
         assertEquals(defintion.getRowFields().getCount(), 1);
+        assertEquals(1, pivotTable.getRowLabelColumns().size());
+        
+        columnIndex = 1;
+        pivotTable.addRowLabel(columnIndex);
+        assertEquals(2, pivotTable.getRowLabelColumns().size());
+        
+        assertEquals(0, (int)pivotTable.getRowLabelColumns().get(0));
+        assertEquals(1, (int)pivotTable.getRowLabelColumns().get(1));
     }
     /**
      * Verify that it's not possible to create a row label outside of the referenced area.
@@ -94,7 +104,7 @@ public class TestXSSFPivotTable extends 
         fail();
     }
 
-     /*
+    /**
      * Verify that when creating one column label, no col fields are being created.
      */
     public void testAddOneColumnLabelToPivotTableDoesNotCreateColField() {
@@ -106,7 +116,7 @@ public class TestXSSFPivotTable extends 
         assertEquals(defintion.getColFields(), null);
     }
 
-     /*
+    /**
      * Verify that when creating two column labels, a col field is being created and X is set to -2.
      */
     public void testAddTwoColumnLabelsToPivotTable() {
@@ -120,7 +130,7 @@ public class TestXSSFPivotTable extends 
         assertEquals(defintion.getColFields().getFieldArray(0).getX(), -2);
     }
 
-     /*
+    /**
      * Verify that a data field is created when creating a data column
      */
     public void testColumnLabelCreatesDataField() {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java?rev=1614691&r1=1614690&r2=1614691&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java Wed Jul 30 15:44:05 2014
@@ -1388,8 +1388,7 @@ public final class TestXSSFSheet extends
 
         XSSFPivotTable pivotTable = sheet2.createPivotTable
                 (new AreaReference("A1:B2"), new CellReference("H5"), sheet1);
-
-        // TODO Test the pivot table was setup correctly
+        assertEquals(0, pivotTable.getRowLabelColumns().size());
         
         assertEquals(1, wb.getPivotTables().size());
         assertEquals(1, sheet1.getPivotTables().size());
@@ -1403,6 +1402,7 @@ public final class TestXSSFSheet extends
 
         XSSFPivotTable pivotTable = sheet2.createPivotTable
                 (new AreaReference(sheet.getSheetName()+"!A$1:B$2"), new CellReference("H5"));
+        assertEquals(0, pivotTable.getRowLabelColumns().size());
     }
 
     public void testCreatePivotTableWithConflictingDataSheets(){



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