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;
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;
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();
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
*/
@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) {
*/
@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) {
*/
@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();
*/
@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();
} 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;
}
pivotFields.setCount(pivotFields.getPivotFieldList().size());
}
-}
\ No newline at end of file
+}
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.
fail();
}
- /*
+ /**
* Verify that when creating one column label, no col fields are being created.
*/
public void testAddOneColumnLabelToPivotTableDoesNotCreateColField() {
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() {
assertEquals(defintion.getColFields().getFieldArray(0).getX(), -2);
}
- /*
+ /**
* Verify that a data field is created when creating a data column
*/
public void testColumnLabelCreatesDataField() {