Bug 59705

Summary: Wrong implementation of XSSFPivotTable.addColumnLabel() methods
Product: POI Reporter: Chenna Kesavarao <chennak.s>
Component: XSSFAssignee: POI Developers List <dev>
Status: NEEDINFO ---    
Severity: blocker CC: datasurfer
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: pivot table

Description Chenna Kesavarao 2016-06-15 11:45:29 UTC
Created attachment 33950 [details]
pivot table

Implementation for XSSFPivotTable.addColumnLabel() method was wrong. 

Implementation provided for XSSFPivotTable.addColumnLabel() methods is actually the implementation for ValueLabel() methods.

If we are creating pivot tables using XSSFPivotTable.addColumnLabel(), the field is adding to the Values position in the excel file.
Comment 1 Chenna Kesavarao 2016-06-15 11:54:36 UTC
i have updated code accordingly to fix this issue.

i have created a new method XSSFPivotTable.addColumnLabel(int columnIndex) and changed the names of existing XSSFPivotTable.addColumnLabel() methods to XSSFPivotTable.addValueLabel()

--------------------------------------------
New method, which i have added
---------------------------------------
  /**
     * Add a column label using data from the given column.
     * @param columnIndex the index of the column to be used as column label.
     */
    @Beta
    public void addColumnLabel(int columnIndex) {
        AreaReference pivotArea = getPivotArea();
        int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

        if(columnIndex > lastColIndex) {
            throw new IndexOutOfBoundsException();
        }
        CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();

        CTPivotField pivotField = CTPivotField.Factory.newInstance();
        CTItems items = pivotField.addNewItems();

        pivotField.setAxis(STAxis.AXIS_COL);
        pivotField.setShowAll(false);
        for(int i = 0; i <= lastRowIndex; i++) {
            items.addNewItem().setT(STItemType.DEFAULT);
        }
        items.setCount(items.sizeOfItemArray());
        pivotFields.setPivotFieldArray(columnIndex, pivotField);

        CTColFields colFields;
        if(pivotTableDefinition.getColFields() != null) {
            colFields = pivotTableDefinition.getColFields();
        } else {
            colFields = pivotTableDefinition.addNewColFields();
        }

        colFields.addNewField().setX(columnIndex);
        colFields.setCount(colFields.sizeOfFieldArray());
    }



--------------------------------------------------
Change of previous existing XSSFPivotTable.addColumnLabel methods to XSSFPivotTable.addValueLabel
-----------------------------------------------------
Comment 2 Chenna Kesavarao 2016-06-15 11:55:48 UTC
@Apache POI developers

Please verify it and resolve the ticket
Comment 3 Chenna Kesavarao 2016-06-17 05:06:57 UTC
Also please provide the provision to user to apply sorting on row label(s) and/or column label(s)
Comment 4 Javen O'Neal 2016-06-17 06:17:04 UTC
Thanks for your contribution!

Any chance you could also write a unit test for this so that behavior can be verified in our automated builds?
Probably the best place to add the unit test would be here: https://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java?view=log
Comment 5 Javen O'Neal 2016-06-17 10:50:52 UTC
*** Bug 59368 has been marked as a duplicate of this bug. ***