Search code examples
sortingpivot-tableasposeaspose-cellssortdirection

How can I hide selected ranges AND sort the displayed results (Aspose Cells)?


I can sort (descending) my displayed results by a selected value using this code:

PivotField field = pivotTable.RowFields[0];
field.IsAutoSort = true;
field.IsAscendSort = false;
field.AutoSortField = 1;

This is what I see (Total Purchases displayed are indeed shown from most to least):

enter image description here

Or, I can only display Description ranges whose "Percentage of Total" value is at least 1% with this code:

private void HideItemsWithFewerThan1PercentOfSales()
{
    int FIRST_TOTAL_PRICE_ROW = 8;
    int ROWS_BETWEEN_PERCENTAGES = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_TOTAL_PRICE_ROW;
    int rowsUsed = dataBodyRange.EndRow;

    pivot.RefreshData();
    pivot.CalculateData();

    // Get grand total of purchases for all items and months, and calculate what 1% of that is
    Cell totalTotalPurchasesCell = pivotTableSheet.Cells[rowsUsed - 2, _grandTotalsColumnPivotTable + 1];
    double totalTotalPurchases = Convert.ToDouble(totalTotalPurchasesCell.Value);
    var onePercentOfTotalPurchases = totalTotalPurchases / 100;

    // Loop through PivotTable data, hiding where percentage < 0.01 (1%)
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell priceCell = pivotTableSheet.Cells[currentRowBeingExamined, _grandTotalsColumnPivotTable + 1];
        String priceStr = priceCell.Value.ToString();
        Double price = Convert.ToDouble(priceStr);
        if (price < onePercentOfTotalPurchases)
        {
            pivotTableSheet.Cells.HideRows(currentRowBeingExamined - 1, ROWS_BETWEEN_PERCENTAGES);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_PERCENTAGES;
    }
}

...like so:

enter image description here

...but I can't get them both to work at the same time. So I can either hide the Descriptions with less than 1% of the percntage OR I can sort by Total Purchases descending, but I'm not able to accomplish both at the same time. My code to try to accomplish both is as follows:

. . .
pivotTable.AddFieldToArea(PivotFieldType.Row, DESCRIPTION_COLUMN);
pivotTable.RowHeaderCaption = "Description";

// Dragging the second field to the column area.
pivotTable.AddFieldToArea(PivotFieldType.Column, MONTHYR_COLUMN);
pivotTable.ColumnHeaderCaption = "Months";

// Dragging the third field to the data area.
pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALQTY_COLUMN);
pivotTable.DataFields[0].DisplayName = "Total Packages";

pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALPRICE_COLUMN);
pivotTable.DataFields[1].DisplayName = "Total Purchases";
. . .

// Sort by "Total Purchases" descending
PivotField field = pivotTable.RowFields[0];
field.IsAutoSort = true;
field.IsAscendSort = false;
field.AutoSortField = 1; // This is the "Total Purchases" field

pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;

pivotTable.RefreshDataFlag = true;
pivotTable.RefreshData();
pivotTable.CalculateData();
pivotTable.RefreshDataFlag = false;

List<String> contractItemDescs = GetContractItemDescriptions();
ColorizeContractItemBlocks(contractItemDescs);
HideItemsWithFewerThan1PercentOfSales();
FreezePanePivotTable(HEADER_ROW, 2); 
FormatPivotTableNumbers();
ConfigureForPrinting(pivotTableSheet.Cells.Rows.Count);

It's as if the sorting order is not being respected when HideItemsWithFewerThan1PercentOfSales() is called - the row numbers that method "sees" is not the row numbers according to the sorting that has been established.

How can I get both the sorting AND the hiding to work?

NOTE: Calling HideItemsWithFewerThan1PercentOfSales(); prior to the sorting code does NOT work - it still shows/hides some of the wrong things.


Solution

  • Please check the reply in this thread in Aspose.Cells forum.

    Note: I am working as Developer Evangelist at Aspose