我如何为所有相关的行和列着色,而不仅仅是其中的一个子集(Aspose Cells)?

How can I color all the related rows and columns, rather than just a subset of them (Aspose Cells)?

我有一个相关问题 ,那里的答案(几乎)对我有用是使用:

pt.ShowInCompactForm();

在我生成的电子表格中,项目块跨越 5 行 - 第一行是描述,下面的四行是 "subrows",包含该项目的详细数据(即,"Total Packages"、"Total Purchases"、"Sum of Average Price" 和 "Percentage of Total").

在某些情况下,我需要为该区域的单元格着色并且我能够在大多数情况下使用以下代码执行此操作:

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

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

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

    // 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.BackgroundColor = CONTRACT_ITEM_COLOR;
            style.Pattern = BackgroundType.Solid;

            CellArea columnRange = pt.ColumnRange;
            // Using StartColumn-1 instead of StartColumn-1 gives me the "Percentage of Total" data field subrow (but not the others - "Total Packages", "Total Purchases", and "Sum of Average Price")
            for (int c = columnRange.StartColumn-1; c <= columnRange.EndColumn; c++)
            {
                //pt.Format(currentRowBeingExamined-1, c, style); <= Instead of adding the "Description" row, this colors up some unrelated final ("Percentage of Total") data rows
                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;
    }
}

但它只有效 "for the most part," 因为我无法着色 "Description" 行(例如,"AVOCADOS, HASS 70 CT #2")或除第 0/A 列的最后一行以外的任何行,- "Percentage of Total" 子行,如下所示:

可能 Description 行向左 untainted/unpainted 更好,但我认为下面的子行彩色化会更好,但我不明白为什么不是这样。

我可以使用这个来防止所有这些子行被着色:

for (int c = columnRange.StartColumn; c <= columnRange.EndColumn; c++)

(也就是说,用 "StartColumn" 而不是 "StartColumn-1" 开始循环可以防止第 0/A 列中的任何内容被着色),但对我来说似乎很奇怪,只有最后一个子行当我从一列向后(0/A)开始时变色。

我们将在您创建的 Aspose.Cells Forum Thread 中调查您的问题。

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