为什么尝试为数据透视表中的范围着色无效(Aspose 单元格)?

Why does attempt to color ranges in a PivotTable have no effect (Aspose Cells)?

我正在尝试像这样有条件地为数据透视表中的范围着色:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            // args are firstRow, firstColumn, totalRows, totalColumns
            Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
                currentRowBeingExamined, 0,
                ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 2);
            Style style = workBook.Styles[workBook.Styles.Add()];
            style.BackgroundColor = CONTRACT_ITEM_COLOR;
            StyleFlag styleFlag = new StyleFlag();
            styleFlag.All = true;
            rangeToColorize.ApplyStyle(style, styleFlag);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

...但是,尽管多次到达 if 块(例如 "rangeToColorize" 跨越 A28:E31 的地方)它没有 "take";我的样式或样式标记或应用有什么问题?

更新

即使我将其更改为:

// Declare a style object.
Style style;

// Create/add the style object.
style = workBook.CreateStyle();

// To Set the fill color of the range, you may use ForegroundColor with
// Solid Pattern setting.
style.BackgroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;

// Create a StyleFlag object.
StyleFlag styleFlag = new StyleFlag();
// Make the corresponding attributes ON.
styleFlag.Font = true;
styleFlag.CellShading = true;

// Apply the style to the range.
rangeToColorize.ApplyStyle(style, styleFlag);

...基于the offical docs,没有区别。

更新 2

即使我将关于一个特定单元格的一些非常明确的代码更改为:

cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = workBook.CreateStyle(); // cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
//style.BackgroundColor = CONTRACT_ITEM_COLOR; 
style.ForegroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);

...IOW,将“style = cell.GetStyle()”更改为“style = workBook.CreateStyle()”和“BackgroundColor”到“ForegroundColor”,它什么都不做;有问题的单元格没有颜色。

更新 3

好吧,(或一件)奇怪的事情是,我能够为任何东西着色的唯一方法是在手动生成的 "Grand Total" 列中:

如您所见,某些行已根据条件进行了着色。但仅在该列中,而不是在整行中,因为它应该(理论上,至少)是:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            // args are firstRow, firstColumn, totalRows, totalColumns
            Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
                currentRowBeingExamined, 0,
                ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 2);

            // Declare a style object.
            Style style;

            // Create/add the style object.
            style = workBook.CreateStyle();
            style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
            style.Pattern = BackgroundType.Solid;

            // Create a StyleFlag object.
            StyleFlag styleFlag = new StyleFlag();
            // Make the corresponding attributes ON.
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            // Apply the style to the range.
            rangeToColorize.ApplyStyle(style, styleFlag);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

所以我认为数据透视表不能着色。然而,即使我尝试像这样给 "plain old" 单元格着色:

cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = workBook.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.ForegroundColor = CONTRACT_ITEM_COLOR;

// Create a StyleFlag object.
StyleFlag styleFlag = new StyleFlag();
// Make the corresponding attributes ON.
styleFlag.Font = true;
styleFlag.CellShading = true;

// Apply the style to the cell
pivotTableSheet.Cells[4, 0].SetStyle(style, styleFlag);

...它不起作用 - 未添加颜色。为什么着色只在一种情况下有效,而在其他情况下无效?

更新 4

有了这个:

PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
PivotTable pivotTable = pivotTables[0];
pivotTable.Format();

...我明白了,“方法 'Format' 没有重载需要 0 个参数。

...还有这个:

PivotTable.Format();

(大写 "P",未分配 "pivotTable"),我收到相同的错误消息。

更新 5

即使有以下内容,根据另一个 Aspose 支持人员的建议,它也没有任何作用:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();
        if (contractItemDescs.Contains(desc))
        {
            Style style;
            style = workBook.CreateStyle();
            style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;
            style.Pattern = BackgroundType.Solid;

            StyleFlag styleFlag = new StyleFlag();
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            PivotTable pt = pivotTableSheet.PivotTables[0];
            pt.Format(currentRowBeingExamined, 0, style); // test - does not work, either
            pt.Format(currentRowBeingExamined, 1, style); // " "
            //CellArea columnRange = pt.ColumnRange;
            //for (int c = columnRange.StartColumn; c < columnRange.EndColumn; c++)
            //{
            //    //    pt.Format(columnRange.StartRow + 1, c, style);
            //    pt.Format(currentRowBeingExamined, c, style);
            //}
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

更新 6

现在,我已经使用以下代码完成了它的大部分工作:

private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
    int FIRST_DESCRIPTION_ROW = 7;
    int DESCRIPTION_COL = 0;
    int ROWS_BETWEEN_DESCRIPTIONS = 4;
    var pivot = pivotTableSheet.PivotTables[0];
    var dataBodyRange = pivot.DataBodyRange;
    int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
    int rowsUsed = dataBodyRange.EndRow;

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

    // Loop through PivotTable data, colorizing contract items
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
        String desc = descriptionCell.Value.ToString();

        if (contractItemDescs.Contains(desc))
        {
            Style style;
            style = workBook.CreateStyle();
            style.BackgroundColor = CONTRACT_ITEM_COLOR;
            style.Pattern = BackgroundType.Solid;

            StyleFlag styleFlag = new StyleFlag();
            styleFlag.Font = true;
            styleFlag.CellShading = true;

            PivotTable pt = pivotTableSheet.PivotTables[0];
            pt.Format(currentRowBeingExamined, 0, style); 
            pt.Format(currentRowBeingExamined, 1, style);
            CellArea columnRange = pt.ColumnRange;
            for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
            {
                pt.Format(currentRowBeingExamined, c, style);
                pt.Format(currentRowBeingExamined+1, c, style);
                pt.Format(currentRowBeingExamined+2, c, style);
                pt.Format(currentRowBeingExamined+3, c, style);
            }
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
    }
}

...但是数据列的第二行、第三行和第四行没有着色:

为什么不呢?我该如何解决?

更新 7

我试图让 B 列的元素变色,并尝试更改它:

pt.Format(currentRowBeingExamined, 0, style); 
pt.Format(currentRowBeingExamined, 1, style);

...为此:

pt.Format(currentRowBeingExamined, 0, style); 
pt.Format(currentRowBeingExamined, 1, style);
pt.Format(currentRowBeingExamined, 2, style); // <= made no difference
pt.Format(currentRowBeingExamined, 3, style); // " "

...但没有任何区别。

然后我想知道上面的第一个两行代码片段是否必要,但是注释掉这些行导致列 A/0 和 B/1 根本没有颜色:

这是上下文中的当前代码:

PivotTable pt = pivotTableSheet.PivotTables[0];
var style = workBook.CreateStyle();

while (currentRowBeingExamined < rowsUsed)
{
    Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
    String desc = descriptionCell.Value.ToString();

    if (contractItemDescs.Contains(desc))
    {
        style.BackgroundColor = CONTRACT_ITEM_COLOR;
        style.Pattern = BackgroundType.Solid;

        pt.Format(currentRowBeingExamined, 0, style); 
        pt.Format(currentRowBeingExamined, 1, style);
        //pt.Format(currentRowBeingExamined, 2, style); <= made no difference
        //pt.Format(currentRowBeingExamined, 3, style);
        CellArea columnRange = pt.ColumnRange;
        for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)
        {
            pt.Format(currentRowBeingExamined, c, style);
            pt.Format(currentRowBeingExamined+1, c, style);
            pt.Format(currentRowBeingExamined+2, c, style);
            pt.Format(currentRowBeingExamined+3, c, style);
        }
    }
    currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
}

那么我怎样才能让着色跨越 所有 列,包括 "Total Purchases"、"Sum of Average Price" 和 "Percentage of Total" 单元格?

请更改以下两行

Style style = workBook.CreateStyle();
style.BackgroundColor = CONTRACT_ITEM_COLOR;

进入

Style style = workBook.CreateStyle();
style.ForegroundColor= CONTRACT_ITEM_COLOR;

它应该可以解决您的问题。让我们知道您的反馈。

更新 2

请尝试以下两种专门针对数据透视表的方法之一

PivotTable.Format()
PivotTable.FormatAll()

更新 5

对于 PivotTable.Format(),您应该使用 Style.BackgroundColor 属性 而不是 Style.ForegroundColor属性。所以换行

style.ForegroundColor = CONTRACT_ITEM_COLOR; //Color.Red;

style.BackgroundColor = CONTRACT_ITEM_COLOR; //Color.Red;

它应该可以解决您的问题。谢谢。

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