如何隐藏选定的范围并对显示的结果进行排序(Aspose Cells)?

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

我可以使用以下代码按所选值对显示的结果进行排序(降序):

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

这是我看到的(显示的总购买量确实是从多到少):

或者,我只能使用此代码显示 "Percentage of Total" 值至少为 1% 的描述范围:

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;
    }
}

...就像这样:

...但我无法让它们同时工作。因此,我可以隐藏少于 1% 百分比的描述,或者我可以按总购买量降序排序,但我无法同时完成这两项操作。我尝试完成两者的代码如下:

. . .
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);

调用 HideItemsWithFewerThan1PercentOfSales() 时好像没有遵守排序顺序 - 方法 "sees" 的行号不是根据已建立的排序的行号。

我怎样才能让排序和隐藏都起作用?

注意: 调用 HideItemsWithFewerThan1PercentOfSales();在排序代码不起作用之前 - 它仍然 shows/hides 一些错误的东西。

请查看 Aspose.Cells 论坛中此 thread 中的回复。

注意:我在 Aspose 担任开发人员布道师