Skip to content

Commit efc4081

Browse files
authored
Merge pull request #19 from fahadadeel/main
feat: Add boolean return status to CopySheet and RenameSheet methods
2 parents ca10604 + 017576b commit efc4081

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

FileFormat.Cells/Workbook.cs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,11 @@ public bool RemoveSheet(string sheetName)
287287
/// Renames an existing sheet within the workbook.
288288
/// </summary>
289289
/// <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)
292295
{
293296
// Find the sheet by its existing name
294297
var sheet = workbookpart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == existingSheetName);
@@ -301,15 +304,20 @@ public void RenameSheet(string existingSheetName, string newSheetName)
301304

302305
// Synchronize the Worksheets property with the Sheets of the workbook
303306
SyncWorksheets();
307+
return true; // Sheet renamed successfully
304308
}
309+
return false; // Sheet not found, rename failed
305310
}
306311

307312
/// <summary>
308313
/// Copies an existing sheet within the workbook to a new sheet.
309314
/// </summary>
310315
/// <param name="sourceSheetName">The name of the sheet to be copied.</param>
311316
/// <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)
313321
{
314322
// Find the source sheet by its name
315323
Sheet sourceSheet = workbookpart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sourceSheetName);
@@ -335,7 +343,10 @@ public void CopySheet(string sourceSheetName, string newSheetName)
335343
// Append the new sheet to the workbook
336344
workbookpart.Workbook.Sheets.Append(newSheet);
337345
workbookpart.Workbook.Save(); // Save changes to the workbook
346+
347+
return true; // Sheet copied successfully
338348
}
349+
return false; // Source sheet not found, copy failed
339350
}
340351

341352

@@ -452,6 +463,50 @@ public List<Tuple<string, string>> GetHiddenSheets()
452463

453464

454465

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+
}
455510

456511
/// <summary>
457512
/// Synchronize the Worksheets property with the actual sheets present in the workbook.

FileFormat.Cells/Worksheet.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ public void InsertColumns(string startColumn, int numberOfColumns)
15441544
/// <exception cref="InvalidOperationException">Thrown when the WorkbookPart is not found, the sheet is not found,
15451545
/// the column name is invalid, no header cell is found, or the SharedStringTablePart is missing.</exception>
15461546
/// <exception cref="IndexOutOfRangeException">Thrown when the shared string index is out of range.</exception>
1547-
public string? GetColumnHeadingNew(string cellName)
1547+
public string? GetColumnHeading(string cellName)
15481548
{
15491549
if (string.IsNullOrEmpty(cellName))
15501550
throw new ArgumentException("Cell name cannot be null or empty.", nameof(cellName));
@@ -1643,7 +1643,6 @@ private string GetColumnName(string? cellName)
16431643

16441644
return match.Value;
16451645
}
1646-
16471646
private static string IncrementColumnReference(string reference, int columnCount)
16481647
{
16491648
var regex = new System.Text.RegularExpressions.Regex("([A-Za-z]+)(\\d+)");

0 commit comments

Comments
 (0)