@@ -287,8 +287,11 @@ public bool RemoveSheet(string sheetName)
287
287
/// Renames an existing sheet within the workbook.
288
288
/// </summary>
289
289
/// <param name="existingSheetName">The current name of the sheet to be renamed.</param>
290
- /// <param name="newSheetName">The new name to be assigned to the sheet.</param>
291
- public void RenameSheet ( string existingSheetName , string newSheetName )
290
+ /// <param name="newSheetName">The new name for the sheet.</param>
291
+ /// <returns>
292
+ /// Returns <c>true</c> if the sheet is successfully renamed; otherwise, <c>false</c>.
293
+ /// </returns>
294
+ public bool RenameSheet ( string existingSheetName , string newSheetName )
292
295
{
293
296
// Find the sheet by its existing name
294
297
var sheet = workbookpart . Workbook . Descendants < Sheet > ( ) . FirstOrDefault ( s => s . Name == existingSheetName ) ;
@@ -301,15 +304,20 @@ public void RenameSheet(string existingSheetName, string newSheetName)
301
304
302
305
// Synchronize the Worksheets property with the Sheets of the workbook
303
306
SyncWorksheets ( ) ;
307
+ return true ; // Sheet renamed successfully
304
308
}
309
+ return false ; // Sheet not found, rename failed
305
310
}
306
311
307
312
/// <summary>
308
313
/// Copies an existing sheet within the workbook to a new sheet.
309
314
/// </summary>
310
315
/// <param name="sourceSheetName">The name of the sheet to be copied.</param>
311
316
/// <param name="newSheetName">The name of the new sheet to be created.</param>
312
- public void CopySheet ( string sourceSheetName , string newSheetName )
317
+ /// <returns>
318
+ /// Returns <c>true</c> if the sheet is successfully copied; otherwise, <c>false</c>.
319
+ /// </returns>
320
+ public bool CopySheet ( string sourceSheetName , string newSheetName )
313
321
{
314
322
// Find the source sheet by its name
315
323
Sheet sourceSheet = workbookpart . Workbook . Descendants < Sheet > ( ) . FirstOrDefault ( s => s . Name == sourceSheetName ) ;
@@ -335,7 +343,10 @@ public void CopySheet(string sourceSheetName, string newSheetName)
335
343
// Append the new sheet to the workbook
336
344
workbookpart . Workbook . Sheets . Append ( newSheet ) ;
337
345
workbookpart . Workbook . Save ( ) ; // Save changes to the workbook
346
+
347
+ return true ; // Sheet copied successfully
338
348
}
349
+ return false ; // Source sheet not found, copy failed
339
350
}
340
351
341
352
@@ -452,6 +463,50 @@ public List<Tuple<string, string>> GetHiddenSheets()
452
463
453
464
454
465
466
+ /// <summary>
467
+ /// Retrieves a list of hidden or very hidden sheets from the workbook.
468
+ /// </summary>
469
+ /// <returns>A list of tuples where each tuple contains the sheet ID and sheet name for hidden sheets.</returns>
470
+ /// <exception cref="InvalidOperationException">Thrown when the workbook part is not initialized.</exception>
471
+ /// <exception cref="ArgumentNullException">Thrown when a sheet's ID or Name is null.</exception>
472
+ public List < Tuple < string , string > > GetHiddenSheets ( )
473
+ {
474
+ if ( workbookpart is null )
475
+ throw new InvalidOperationException ( "WorkbookPart is not initialized." ) ;
476
+
477
+ List < Tuple < string , string > > returnVal = new List < Tuple < string , string > > ( ) ;
478
+
479
+ // Retrieves all sheets in the workbook.
480
+ // Reference: DocumentFormat.OpenXml.Spreadsheet.Sheet
481
+ var sheets = workbookpart . Workbook . Descendants < Sheet > ( ) ;
482
+
483
+ // Look for sheets where there is a State attribute defined,
484
+ // where the State has a value,
485
+ // and where the value is either Hidden or VeryHidden.
486
+ // Reference: DocumentFormat.OpenXml.Spreadsheet.SheetStateValues
487
+ var hiddenSheets = sheets . Where ( item => item . State is not null &&
488
+ item . State . HasValue &&
489
+ ( item . State . Value == SheetStateValues . Hidden ||
490
+ item . State . Value == SheetStateValues . VeryHidden ) ) ;
491
+
492
+ // Populate the return list with the sheet ID and name as tuples.
493
+ foreach ( var sheet in hiddenSheets )
494
+ {
495
+ // Check if sheet ID or Name is null
496
+ if ( sheet . Id is null )
497
+ throw new ArgumentNullException ( nameof ( sheet . Id ) , "Sheet ID cannot be null." ) ;
498
+
499
+ if ( sheet . Name is null )
500
+ throw new ArgumentNullException ( nameof ( sheet . Name ) , "Sheet Name cannot be null." ) ;
501
+
502
+ returnVal . Add ( new Tuple < string , string > (
503
+ sheet . Id , // The ID of the sheet, typically used to reference the sheet in the workbook.
504
+ sheet . Name // The name of the sheet as displayed in the workbook.
505
+ ) ) ;
506
+ }
507
+
508
+ return returnVal ;
509
+ }
455
510
456
511
/// <summary>
457
512
/// Synchronize the Worksheets property with the actual sheets present in the workbook.
0 commit comments