Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#
Tip/Trick

How to Hide a Specified Number of Contiguous Rows on Spreadsheets with Aspose Cells

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Nov 2016CPOL 8.2K   2  
Based on some condition, hide a range (actually a specified number of contiguous rows) on a spreadsheet with Aspose Cells

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Hidden in Plain Sight

To hide any number of contiguous rows based on some condition when using the Aspose Cells library, you can use code like this:

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

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

    // Loop through PivotTable data, hiding where percentage < 0.01 (1%)
    while (currentRowBeingExamined < rowsUsed)
    {
        Cell percentageCell = pivotTableSheet.Cells[currentRowBeingExamined, _grandTotalsColumnPivotTable];
        String prcntgStr = percentageCell.Value.ToString();
        Decimal prcntg = Convert.ToDecimal(prcntgStr);
        if (prcntg < 0.01M)
        {
            pivotTableSheet.Cells.HideRows(currentRowBeingExamined-3, ROWS_BETWEEN_PERCENTAGES);
        }
        currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_PERCENTAGES;
    }
}

The logic, of course, will differ based on your data and situation (how many rows at a time you want to hide - maybe just one! - and what condition you want to use as a gauge for what should be visible and what not, or whatnot), but these are the parts that you will need in any case:

C#
var pivot = pivotTableSheet.PivotTables[0]; // change sheet named from "pivotTableSheet" to yours
var dataBodyRange = pivot.DataBodyRange;
int rowsUsed = dataBodyRange.EndRow;
nt currentRowBeingExamined = someStartingRow; // change "someStartingRow" to what you need

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

// Loop through PivotTable data, hiding where a particular condition is met
while (currentRowBeingExamined < rowsUsed)
{
    Cell someCell = pivotTableSheet.Cells[currentRowBeingExamined, _grandTotalsColumnPivotTable];
    if (yourCondition) // replace "yourCondition" with the logic you need
    {
    	// replace first arg with the first row you want to hide; replace the second arg with how many rows from there you 

want to hide - perhaps just 1
        pivotTableSheet.Cells.HideRows(currentRowBeingExamined, someNumberOfRows);
    }
    // If you're examining each row, and only hiding one when the condition is found, change to inc by 1
    currentRowBeingExamined = currentRowBeingExamined + someNumberOfRows;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --