From 942f8044d96bad05c54bf4d593a7298c3cdbca92 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 21 Dec 2014 06:12:20 +0000 Subject: [PATCH] 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 --- .../poi/xssf/usermodel/XSSFPivotTable.java | 24 +++++++++++++++---- .../xssf/usermodel/TestXSSFPivotTable.java | 16 +++++++++++++ 2 files changed, 35 insertions(+), 5 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 278e66e3d..465d1feec 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java @@ -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()); } 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 9eb164eaa..b7aabfc8c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java @@ -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.