View | Details | Raw Unified | Return to bug 57063
Collapse All | Expand All

(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java (-5 / +19 lines)
Lines 275-290 public class XSSFPivotTable extends POIXMLDocumentPart { Link Here
275
            return Collections.emptyList();
275
            return Collections.emptyList();
276
        }
276
        }
277
    }
277
    }
278
278
    
279
    /**
279
    /**
280
     * Add a column label using data from the given column and specified function
280
     * Add a column label using data from the given column and specified function
281
     * @param columnIndex the index of the column to be used as column label.
281
     * @param columnIndex the index of the column to be used as column label.
282
     * @param function the function to be used on the data
282
     * @param function the function to be used on the data
283
     * The following functions exists:
283
     * The following functions exists:
284
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
284
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
285
     * @param valueFieldName the name of pivot table value field
285
     */
286
     */
286
    @Beta
287
    @Beta
287
    public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
288
    public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
288
        AreaReference pivotArea = getPivotArea();
289
        AreaReference pivotArea = getPivotArea();
289
        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
290
        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
290
291
Lines 293-299 public class XSSFPivotTable extends POIXMLDocumentPart { Link Here
293
        }
294
        }
294
295
295
        addDataColumn(columnIndex, true);
296
        addDataColumn(columnIndex, true);
296
        addDataField(function, columnIndex);
297
        addDataField(function, columnIndex, valueFieldName);
297
298
298
        //colfield should be added for the second one.
299
        //colfield should be added for the second one.
299
        if (pivotTableDefinition.getDataFields().getCount() == 2) {
300
        if (pivotTableDefinition.getDataFields().getCount() == 2) {
Lines 309-322 public class XSSFPivotTable extends POIXMLDocumentPart { Link Here
309
    }
310
    }
310
311
311
    /**
312
    /**
313
     * Add a column label using data from the given column and specified function
314
     * @param columnIndex the index of the column to be used as column label.
315
     * @param function the function to be used on the data
316
     * The following functions exists:
317
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
318
     */
319
    @Beta
320
    public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
321
        addColumnLabel(function, columnIndex, function.getName());
322
    }
323
324
    /**
312
     * Add data field with data from the given column and specified function.
325
     * Add data field with data from the given column and specified function.
313
     * @param function the function to be used on the data
326
     * @param function the function to be used on the data
314
     * @param index the index of the column to be used as column label.
327
     * @param index the index of the column to be used as column label.
315
     * The following functions exists:
328
     * The following functions exists:
316
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
329
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
330
     * @param valueFieldName the name of pivot table value field
317
     */
331
     */
318
    @Beta
332
    @Beta
319
    private void addDataField(DataConsolidateFunction function, int columnIndex) {
333
    private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
320
        AreaReference pivotArea = getPivotArea();
334
        AreaReference pivotArea = getPivotArea();
321
        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
335
        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
322
336
Lines 333-339 public class XSSFPivotTable extends POIXMLDocumentPart { Link Here
333
        dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue()));
347
        dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue()));
334
        Cell cell = getDataSheet().getRow(pivotArea.getFirstCell().getRow()).getCell(columnIndex);
348
        Cell cell = getDataSheet().getRow(pivotArea.getFirstCell().getRow()).getCell(columnIndex);
335
        cell.setCellType(Cell.CELL_TYPE_STRING);
349
        cell.setCellType(Cell.CELL_TYPE_STRING);
336
        dataField.setName(function.getName());
350
        dataField.setName(valueFieldName);
337
        dataField.setFld(columnIndex);
351
        dataField.setFld(columnIndex);
338
        dataFields.setCount(dataFields.sizeOfDataFieldArray());
352
        dataFields.setCount(dataFields.sizeOfDataFieldArray());
339
    }
353
    }
(-)a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java (-1 / +16 lines)
Lines 144-149 public class TestXSSFPivotTable extends TestCase { Link Here
144
        assertEquals(defintion.getDataFields().getDataFieldArray(0).getSubtotal(),
144
        assertEquals(defintion.getDataFields().getDataFieldArray(0).getSubtotal(),
145
                STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()));
145
                STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()));
146
    }
146
    }
147
    
148
    /**
149
     * Verify that it's possible to set a custom name when creating a data column
150
     */
151
    public void testColumnLabelSetCustomName() {
152
        int columnIndex = 0;
153
154
        String customName = "Custom Name";
155
        
156
        pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex, customName);
157
158
        CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
159
160
        assertEquals(defintion.getDataFields().getDataFieldArray(0).getFld(), columnIndex);
161
        assertEquals(defintion.getDataFields().getDataFieldArray(0).getName(), customName);
162
    }
147
163
148
    /**
164
    /**
149
     * Verify that it's not possible to create a column label outside of the referenced area.
165
     * Verify that it's not possible to create a column label outside of the referenced area.
150
- 

Return to bug 57063