We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Hide Columns and Unhide them columns changing to very narrowed

I want to hide 3 columns from a worksheet and on other method that will be called when user press Close I want to unhide them (Reset them to the previous state). It works fine to hide the columns:
wb.Worksheets[i].Unprotect(“LineMap”);
if (lstBox.SelectedItems.Contains(wb.Worksheets[i].Name))
{
wb.Worksheets[i].IsVisible = true;
wb.Worksheets.ActiveSheetIndex = i;
wb.Worksheets[i].Cells.HideColumns(6, 3);
}

But when I unhide them the columns remain very narrowed (about 10 times narrower than before - see attached .xls file on Remus worksheet (The columns should remain as on the Base sheet)). The snippet of code to Unhide them is:
for (int j = 0; j < wb.Worksheets.Count; j++)
{
wb.Worksheets[j].Unprotect(“LineMap”);
wb.Worksheets[j].IsVisible = true;
wb.Worksheets.ActiveSheetIndex = j;
wb.Worksheets[j].Cells.UnhideColumns(6, 3, 7.86);
wb.Worksheets[j].Protect(ProtectionType.All, “LineMap”, “”);
}

I just want to come back to the previous state and if possible not to specify the width as I don’t change any style or width on the worksheets.
And also if possible all tabs to remain side-by-side (visible) when I open the .xls file

@Remus87,
It seems that you forgot to attach the sample Excel file. Please share your runnable solution and template file after zipping it. If size is more than the allowed limit, you may upload the data to some public file sharing server and share the download link here for our testing.

Good morning ! Yes I forgot to add the .xls file. I have attached here. Thank you !LMRates.zip (26.7 KB)

@Remus87,
We have investigated it bit more. When we unhide the columns we got to specify the column width as the last parameter. So the width would be 7.86 only. You should extend this value or store the width in some variable before hiding the columns and when unhiding the columns you may specify the value from the variable to be on safer side.

I’ve stored the width into a double variable which I made it global (Just to be before hiding columns as you mentioned) and used the variable … No difference (still very narrowed)
double colWidth = 7.86;
public void ShowRates()
for (int i = 0; i < wb.Worksheets.Count; i++)
{
wb.Worksheets[i].Unprotect(“LineMap”);
if (lstBox.SelectedItems.Contains(wb.Worksheets[i].Name))
{
wb.Worksheets[i].IsVisible = true;
wb.Worksheets.ActiveSheetIndex = i;
wb.Worksheets[i].Cells.HideColumns(6, 3);
}
//…
public void ResetExcelRates(int openClose)
{
if (openClose == 0)
{
for (int i = 0; i < wb.Worksheets.Count; i++)
{
wb.Worksheets[i].Unprotect(“LineMap”);
wb.Worksheets[i].IsVisible = true;
wb.Worksheets[i].IsSelected = true;
wb.Worksheets[i].Cells.UnhideColumns(6, 3, colWidth);
wb.Worksheets[i].Protect(ProtectionType.All, “LineMap”, “”);
}
}

@Remus87,

I think you did not get our point. We suggested you store original columns’ width (via some method(s), e.g., Worksheet.Cells.GetColumnWidth, etc.) in some variable(s), then hide column(s). Now you may unhide columns with respect to your stored value (from the variable(s)) in code accordingly.

Hope, this helps a bit.

It may help but it gets complicated as it shouldn’t . I am using ShowRates() method where among other instructions I Hide the columns G,H and I . Now I am Unhiding the columns in ResetExcelRates() which is another method that is not dependable on the ShowRates(); .In each method I am looping an i number of times through the workbook to find the Worksheet selected by user (I am not having a defined number of sheets to know the precise sheet where to use the GetColumnWidth()) and therefore it complicates the architecture of the code and possibly the performance. I’ve tried to insert the whole numeric value as
#ahsaniqbalsidiqui suggested which is: 8.42857142857143 and still won’t change the width back to it’s state

@Remus87,

We understand your concerns. Aspose.Cells did implement the feature to previously assigned width of a column when unhiding a column. Please note, while making a hidden column visible, if you need to restore it to previously assigned width or to its original width, please unhide the column with a negative width. For example: worksheet.Cells.UnhideColumn(6, -1); --> it will unhide the 7th column (G column) to its original width. In short, you will put negative value for the width parameter.

Hope, this helps a bit.

It works fine the last solution Amjad. Thanks a lot !

@Remus87,

Good to know that the suggested solution works for your needs. In the event of further queries or issue, feel free to write us back.