Skip to content

Commit

Permalink
Merge pull request #1446 from antony-liu/poi/bug60605
Browse files Browse the repository at this point in the history
Poi bug 60605: convert Workbook.SHEET_STATE_* to SheetVisibility enum
  • Loading branch information
tonyqus authored Nov 18, 2024
2 parents 04e052a + feb75f9 commit b57e765
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 286 deletions.
64 changes: 34 additions & 30 deletions main/HSSF/Model/InternalWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,31 @@ public bool IsSheetVeryHidden(int sheetnum)
{
return GetBoundSheetRec(sheetnum).IsVeryHidden;
}

/**
* Gets the hidden flag for a given sheet.
* Note that a sheet could instead be
* set to be very hidden, which is different
* ({@link #isSheetVeryHidden(int)})
*
* @param sheetnum the sheet number (0 based)
* @return True if sheet is hidden
* @since 3.16 beta 2
*/
public SheetVisibility GetSheetVisibility(int sheetnum)
{
BoundSheetRecord bsr = GetBoundSheetRec(sheetnum);
if(bsr.IsVeryHidden)
{
return SheetVisibility.VeryHidden;
}
if(bsr.IsHidden)
{
return SheetVisibility.Hidden;
}
return SheetVisibility.Visible;
}

/**
* Hide or Unhide a sheet
*
Expand All @@ -784,40 +809,19 @@ public bool IsSheetVeryHidden(int sheetnum)

public void SetSheetHidden(int sheetnum, bool hidden)
{
BoundSheetRecord bsr = boundsheets[sheetnum];
bsr.IsHidden=hidden;
SetSheetHidden(sheetnum, hidden ? SheetVisibility.Hidden : SheetVisibility.Visible);
}
/**
* Hide or unhide a sheet.
* 0 = not hidden
* 1 = hidden
* 2 = very hidden.
*
* @param sheetnum The sheet number
* @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden
*/
public void SetSheetHidden(int sheetnum, int hidden)
* Hide or unhide a sheet.
*
* @param sheetnum The sheet number
* @param visibility the sheet visibility to set (visible, hidden, very hidden)
*/
public void SetSheetHidden(int sheetnum, SheetVisibility visibility)
{
BoundSheetRecord bsr = GetBoundSheetRec(sheetnum);
bool h = false;
bool vh = false;
if (hidden == 0)
{
}
else if (hidden == 1)
{
h = true;
}
else if (hidden == 2)
{
vh = true;
}
else
{
throw new ArgumentException("Invalid hidden flag " + hidden + " given, must be 0, 1 or 2");
}
bsr.IsHidden = (h);
bsr.IsVeryHidden = (vh);
bsr.IsHidden = visibility == SheetVisibility.Hidden;
bsr.IsVeryHidden = visibility == SheetVisibility.VeryHidden;
}
/**
* Get the sheet's index
Expand Down
41 changes: 33 additions & 8 deletions main/HSSF/UserModel/HSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,32 +759,57 @@ public bool IsSheetVeryHidden(int sheetIx)
ValidateSheetIndex(sheetIx);
return workbook.IsSheetVeryHidden(sheetIx);
}

public SheetVisibility GetSheetVisibility(int sheetIx)
{
return workbook.GetSheetVisibility(sheetIx);
}

/// <summary>
/// Hide or Unhide a sheet
/// </summary>
/// <param name="sheetIx">The sheet index</param>
/// <param name="hidden">True to mark the sheet as hidden, false otherwise</param>
public void SetSheetHidden(int sheetIx, SheetState hidden)
/// <param name="hidden"></param>
[Obsolete]
public void SetSheetHidden(int sheetIx, SheetVisibility hidden)
{
ValidateSheetIndex(sheetIx);
WorkbookUtil.ValidateSheetState(hidden);
workbook.SetSheetHidden(sheetIx, (int)hidden);
SetSheetVisibility(sheetIx, hidden);
}
/// <summary>
/// Hide or unhide a sheet.
/// </summary>
/// <param name="sheetIx">The sheet number</param>
/// <param name="hidden">0 for not hidden, 1 for hidden, 2 for very hidden</param>
[Obsolete]
public void SetSheetHidden(int sheetIx, int hidden)
{
ValidateSheetIndex(sheetIx);
workbook.SetSheetHidden(sheetIx, hidden);
switch(hidden)
{
case 0:
SetSheetVisibility(sheetIx, SheetVisibility.Visible);
break;
case 1:
SetSheetVisibility(sheetIx, SheetVisibility.Hidden);
break;
case 2:
SetSheetVisibility(sheetIx, SheetVisibility.VeryHidden);
break;
default:
throw new ArgumentException("Invalid sheet state : " + hidden + "\n" +
"Sheet state must beone of the Workbook.SHEET_STATE_* constants");
}
}
public void SetSheetHidden(int sheetIx, bool hidden)
{
SetSheetVisibility(sheetIx, hidden ? SheetVisibility.Hidden : SheetVisibility.Visible);
}

public void SetSheetVisibility(int sheetIx, SheetVisibility visibility)
{
ValidateSheetIndex(sheetIx);
workbook.SetSheetHidden(sheetIx, hidden);
workbook.SetSheetHidden(sheetIx, visibility);
}

/// <summary>
/// Returns the index of the sheet by his name
/// </summary>
Expand Down
45 changes: 45 additions & 0 deletions main/SS/UserModel/SheetVisibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

namespace NPOI.SS.UserModel
{
/// <summary>
/// Specifies sheet visibility
/// </summary>
public enum SheetVisibility : int
{
/// <summary>
/// Indicates the sheet is visible.
/// </summary>
Visible = 0,

/// <summary>
/// Indicates the book window is hidden, but can be shown by the user via the user interface.
/// </summary>
Hidden = 1,

/// <summary>
/// Indicates the sheet is hidden and cannot be shown in the user interface (UI).
/// </summary>
/// <remarks>
/// In Excel this state is only available programmatically in VBA:
/// ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden
///
/// </remarks>
VeryHidden = 2
}
}
49 changes: 24 additions & 25 deletions main/SS/UserModel/Workbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@ namespace NPOI.SS.UserModel
using System.Collections.Generic;
using NPOI.Util;

public enum SheetState : int
{
/// <summary>
/// Indicates the sheet is visible.
/// </summary>
Visible = 0,

/// <summary>
/// Indicates the book window is hidden, but can be shown by the user via the user interface.
/// </summary>
Hidden = 1,

/// <summary>
/// Indicates the sheet is hidden and cannot be shown in the user interface (UI).
/// </summary>
/// <remarks>
/// In Excel this state is only available programmatically in VBA:
/// ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden
///
/// </remarks>
VeryHidden = 2
}

/// <summary>
/// High level interface of a Excel workbook. This is the first object most users
/// will construct whether they are reading or writing a workbook. It is also the
Expand Down Expand Up @@ -416,7 +393,7 @@ public interface IWorkbook : ICloseable, IDisposable
* @param sheetIx the sheet index (0-based)
* @param hidden True to mark the sheet as hidden, false otherwise
*/
void SetSheetHidden(int sheetIx, SheetState hidden);
void SetSheetHidden(int sheetIx, SheetVisibility hidden);

/**
* Hide or unhide a sheet.
Expand All @@ -428,8 +405,30 @@ public interface IWorkbook : ICloseable, IDisposable
* @param sheetIx The sheet number
* @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden
*/
[Obsolete]
void SetSheetHidden(int sheetIx, int hidden);

/**
* Get the visibility (visible, hidden, very hidden) of a sheet in this workbook
*
* @param sheetIx the index of the sheet
* @return the sheet visibility
* @since POI 3.16 beta 2
*/
SheetVisibility GetSheetVisibility(int sheetIx);

/**
* Hide or unhide a sheet.
*
* Please note that the sheet currently set as active sheet (sheet 0 in a newly
* created workbook or the one set via setActiveSheet()) cannot be hidden.
*
* @param sheetIx the sheet index (0-based)
* @param visibility the sheet visibility to set
* @since POI 3.16 beta 2
*/
void SetSheetVisibility(int sheetIx, SheetVisibility visibility);

/// <summary>
/// Register a new toolpack in this workbook.
/// </summary>
Expand All @@ -444,7 +443,7 @@ public interface IWorkbook : ICloseable, IDisposable
/// <returns>True if the date systems used in the workbook starts in 1904</returns>
bool IsDate1904();

void Close();
//void Close();

/// <summary>
/// Returns the spreadsheet version (EXCLE97) of this workbook
Expand Down
Loading

0 comments on commit b57e765

Please sign in to comment.