From 8eb7e4a7f43336bc916393b65b800b53594e43b5 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 30 Jul 2014 15:44:05 +0000 Subject: [PATCH] Add row label getter for Pivot Tables, and use this in unit tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614691 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/XSSFPivotTable.java | 44 +++++++++++++------ .../xssf/usermodel/TestXSSFPivotTable.java | 18 ++++++-- .../poi/xssf/usermodel/TestXSSFSheet.java | 4 +- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java index 94d73fbef0..db44f3fc0f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java @@ -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.spreadsheetml.x2006.main.CTCacheSource; 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 POIXMLDocumentPart { 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 POIXMLDocumentPart { rowFields.addNewField().setX(columnIndex); rowFields.setCount(rowFields.getFieldList().size()); } + + @Beta + @SuppressWarnings("deprecation") + public List getRowLabelColumns() { + if (pivotTableDefinition.getRowFields() != null) { + List columnIndexes = new ArrayList(); + 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 POIXMLDocumentPart { */ @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 POIXMLDocumentPart { */ @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 POIXMLDocumentPart { */ @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 POIXMLDocumentPart { */ @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 POIXMLDocumentPart { } 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 POIXMLDocumentPart { } pivotFields.setCount(pivotFields.getPivotFieldList().size()); } -} \ No newline at end of file +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java index 4f7c13d18c..9eb164eaa9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java @@ -67,18 +67,28 @@ public class TestXSSFPivotTable extends TestCase { 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 TestCase { 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 TestCase { 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 TestCase { assertEquals(defintion.getColFields().getFieldArray(0).getX(), -2); } - /* + /** * Verify that a data field is created when creating a data column */ public void testColumnLabelCreatesDataField() { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index e260e9e732..44342781a8 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -1388,8 +1388,7 @@ public final class TestXSSFSheet extends BaseTestSheet { 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 BaseTestSheet { XSSFPivotTable pivotTable = sheet2.createPivotTable (new AreaReference(sheet.getSheetName()+"!A$1:B$2"), new CellReference("H5")); + assertEquals(0, pivotTable.getRowLabelColumns().size()); } public void testCreatePivotTableWithConflictingDataSheets(){