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

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

View File

@ -145,6 +145,22 @@ public class TestXSSFPivotTable extends TestCase {
STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue())); 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. * Verify that it's not possible to create a column label outside of the referenced area.
*/ */