Skip to content

Commit 017576b

Browse files
committed
no message
2 parents f5ced00 + e653814 commit 017576b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

FileFormat.Cells/Workbook.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,53 @@ public void SetSheetVisibility(string sheetName, SheetVisibility visibility)
415415
throw new ArgumentException($"Sheet '{sheetName}' not found in the workbook.");
416416
}
417417
}
418+
419+
/// <summary>
420+
/// Retrieves a list of hidden or very hidden sheets from the workbook.
421+
/// </summary>
422+
/// <returns>A list of tuples where each tuple contains the sheet ID and sheet name for hidden sheets.</returns>
423+
/// <exception cref="InvalidOperationException">Thrown when the workbook part is not initialized.</exception>
424+
/// <exception cref="ArgumentNullException">Thrown when a sheet's ID or Name is null.</exception>
425+
public List<Tuple<string, string>> GetHiddenSheets()
426+
{
427+
if (workbookpart is null)
428+
throw new InvalidOperationException("WorkbookPart is not initialized.");
429+
430+
List<Tuple<string, string>> returnVal = new List<Tuple<string, string>>();
431+
432+
// Retrieves all sheets in the workbook.
433+
// Reference: DocumentFormat.OpenXml.Spreadsheet.Sheet
434+
var sheets = workbookpart.Workbook.Descendants<Sheet>();
435+
436+
// Look for sheets where there is a State attribute defined,
437+
// where the State has a value,
438+
// and where the value is either Hidden or VeryHidden.
439+
// Reference: DocumentFormat.OpenXml.Spreadsheet.SheetStateValues
440+
var hiddenSheets = sheets.Where(item => item.State is not null &&
441+
item.State.HasValue &&
442+
(item.State.Value == SheetStateValues.Hidden ||
443+
item.State.Value == SheetStateValues.VeryHidden));
444+
445+
// Populate the return list with the sheet ID and name as tuples.
446+
foreach (var sheet in hiddenSheets)
447+
{
448+
// Check if sheet ID or Name is null
449+
if (sheet.Id is null)
450+
throw new ArgumentNullException(nameof(sheet.Id), "Sheet ID cannot be null.");
451+
452+
if (sheet.Name is null)
453+
throw new ArgumentNullException(nameof(sheet.Name), "Sheet Name cannot be null.");
454+
455+
returnVal.Add(new Tuple<string, string>(
456+
sheet.Id, // The ID of the sheet, typically used to reference the sheet in the workbook.
457+
sheet.Name // The name of the sheet as displayed in the workbook.
458+
));
459+
}
460+
461+
return returnVal;
462+
}
463+
464+
418465

419466
/// <summary>
420467
/// Retrieves a list of hidden or very hidden sheets from the workbook.

0 commit comments

Comments
 (0)