Patch from Kamil Linek from bug #57063 - XSSF custom column label names for pivot tables

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647087 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-12-21 06:12:20 +00:00
parent 68bb54e06a
commit 942f8044d9
2 changed files with 35 additions and 5 deletions

View File

@ -275,16 +275,17 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
return Collections.emptyList();
}
}
/**
* Add a column label using data from the given column and specified function
* @param columnIndex the index of the column to be used as column label.
* @param function the function to be used on the data
* The following functions exists:
* Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
* @param valueFieldName the name of pivot table value field
*/
@Beta
public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
@ -293,7 +294,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
}
addDataColumn(columnIndex, true);
addDataField(function, columnIndex);
addDataField(function, columnIndex, valueFieldName);
//Only add colfield if there is already one.
if (pivotTableDefinition.getDataFields().getCount() > 1) {
@ -308,15 +309,28 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
}
}
/**
* Add a column label using data from the given column and specified function
* @param columnIndex the index of the column to be used as column label.
* @param function the function to be used on the data
* The following functions exists:
* Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
*/
@Beta
public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
addColumnLabel(function, columnIndex, function.getName());
}
/**
* Add data field with data from the given column and specified function.
* @param function the function to be used on the data
* @param index the index of the column to be used as column label.
* The following functions exists:
* Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
* @param valueFieldName the name of pivot table value field
*/
@Beta
private void addDataField(DataConsolidateFunction function, int columnIndex) {
private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
@ -333,7 +347,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue()));
Cell cell = getDataSheet().getRow(pivotArea.getFirstCell().getRow()).getCell(columnIndex);
cell.setCellType(Cell.CELL_TYPE_STRING);
dataField.setName(function.getName());
dataField.setName(valueFieldName);
dataField.setFld(columnIndex);
dataFields.setCount(dataFields.sizeOfDataFieldArray());
}

View File

@ -144,6 +144,22 @@ public class TestXSSFPivotTable extends TestCase {
assertEquals(defintion.getDataFields().getDataFieldArray(0).getSubtotal(),
STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()));
}
/**
* Verify that it's possible to set a custom name when creating a data column
*/
public void testColumnLabelSetCustomName() {
int columnIndex = 0;
String customName = "Custom Name";
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex, customName);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getDataFields().getDataFieldArray(0).getFld(), columnIndex);
assertEquals(defintion.getDataFields().getDataFieldArray(0).getName(), customName);
}
/**
* Verify that it's not possible to create a column label outside of the referenced area.