-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract code snippets from how-to-retrieve-a-list-of-the-hidden-works…
…heets-in-a-spreadsheet.md and add missing project (#293)
- Loading branch information
1 parent
021771f
commit 80dda58
Showing
5 changed files
with
63 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletions
17
samples/spreadsheet/retrieve_a_list_of_the_hidden_worksheets/cs/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,46 @@ | ||
// <Snippet0> | ||
using DocumentFormat.OpenXml.Packaging; | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
GetHiddenSheets(args[0]); | ||
|
||
static List<Sheet> GetHiddenSheets(string fileName) | ||
{ | ||
List<Sheet> returnVal = new List<Sheet>(); | ||
|
||
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false)) | ||
{ | ||
// <Snippet1> | ||
WorkbookPart? wbPart = document.WorkbookPart; | ||
|
||
if (wbPart is not null) | ||
{ | ||
var sheets = wbPart.Workbook.Descendants<Sheet>(); | ||
// </Snippet1> | ||
|
||
// Look for sheets where there is a State attribute defined, | ||
// where the State has a value, | ||
// and where the value is either Hidden or VeryHidden. | ||
|
||
// <Snippet2> | ||
var hiddenSheets = sheets.Where((item) => item.State is not null && | ||
item.State.HasValue && | ||
(item.State.Value == SheetStateValues.Hidden || | ||
item.State.Value == SheetStateValues.VeryHidden)); | ||
// </Snippet2> | ||
|
||
returnVal = hiddenSheets.ToList(); | ||
} | ||
} | ||
|
||
return returnVal; | ||
} | ||
// </Snippet0> | ||
|
||
var sheets = GetHiddenSheets(args[0]); | ||
|
||
foreach (var sheet in sheets) | ||
{ | ||
Console.WriteLine(sheet.Name); | ||
} |
27 changes: 20 additions & 7 deletions
27
samples/spreadsheet/retrieve_a_list_of_the_hidden_worksheets/vb/Program.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
' <Snippet0> | ||
Imports DocumentFormat.OpenXml.Spreadsheet | ||
Imports DocumentFormat.OpenXml.Packaging | ||
|
||
Module Program ` | ||
Sub Main(args As String())` | ||
End Sub` | ||
Module Program | ||
Sub Main(args As String()) | ||
Dim fileName As String = args(0) | ||
Dim hiddenSheets As List(Of Sheet) = GetHiddenSheets(fileName) | ||
|
||
For Each sheet As Sheet In hiddenSheets | ||
Console.WriteLine("Sheet ID: {0} Name: {1}", sheet.Id, sheet.Name) | ||
Next | ||
End Sub | ||
|
||
|
||
|
||
|
||
|
||
Public Function GetHiddenSheets(ByVal fileName As String) As List(Of Sheet) | ||
Dim returnVal As New List(Of Sheet) | ||
|
||
Using document As SpreadsheetDocument = | ||
SpreadsheetDocument.Open(fileName, False) | ||
|
||
' <Snippet1> | ||
Dim wbPart As WorkbookPart = document.WorkbookPart | ||
Dim sheets = wbPart.Workbook.Descendants(Of Sheet)() | ||
' </Snippet1> | ||
|
||
' Look for sheets where there is a State attribute defined, | ||
' where the State has a value, | ||
' and where the value is either Hidden or VeryHidden: | ||
|
||
' <Snippet2> | ||
Dim hiddenSheets = sheets.Where(Function(item) item.State IsNot | ||
Nothing AndAlso item.State.HasValue _ | ||
AndAlso (item.State.Value = SheetStateValues.Hidden Or _ | ||
AndAlso (item.State.Value = SheetStateValues.Hidden Or | ||
item.State.Value = SheetStateValues.VeryHidden)) | ||
' </Snippet2> | ||
|
||
returnVal = hiddenSheets.ToList() | ||
End Using | ||
Return returnVal | ||
End Function | ||
End Module | ||
End Module | ||
' </Snippet0> |