Skip to content

Commit

Permalink
closes OfficeDev#279
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Dec 5, 2023
1 parent a3c2510 commit 06dd059
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,112 +23,25 @@ and ranges of all defined names in an Microsoft Excel 2010 or Microsoft
Excel 2013 workbook. It contains an example **GetDefinedNames** method
to illustrate this task.



## GetDefinedNames Method

The **GetDefinedNames** procedure accepts a
The **GetDefinedNames** method accepts a
single parameter that indicates the name of the document from which to
retrieve the defined names. The procedure returns an
retrieve the defined names. The method returns an
[Dictionary](https://msdn.microsoft.com/library/xfhwa508.aspx)
instance that contains information about the defined names within the
specified workbook, which may be empty if there are no defined names.

### [C#](#tab/cs-0)
```csharp
public static Dictionary<String, String>
GetDefinedNames(String fileName)
```

### [Visual Basic](#tab/vb-0)
```vb
Public Function GetDefinedNames(
ByVal fileName As String) As Dictionary(Of String, String)
```
***


The method examines the workbook that you specify, looking for the part
that contains defined names. If it exists, the code iterates through all
the contents of the part, adding the name and value for each defined
name to the returned dictionary.

## Calling the Sample Method

To call the sample method, pass a string that contains the name of the
file from which to retrieve the defined names. The following code
example passes a string that contains the name of the file from which to
retrieve the defined names and iterates through the returned dictionary,
and displays the key and value from each item.

### [C#](#tab/cs-1)
```csharp
var result =
GetDefinedNames(@"C:\Users\Public\Documents\definednames.xlsx");
foreach (var dn in result)
Console.WriteLine("{0} {1}", dn.Key, dn.Value);
```

### [Visual Basic](#tab/vb-1)
```vb
Dim result =
GetDefinedNames("C:\Users\Public\Documents\definednames.xlsx")
For Each dn In result
Console.WriteLine("{0}: {1}", dn.Key, dn.Value)
```
***


## How the Code Works

The code starts by creating a variable named **returnValue** that the method will return before it exits.

### [C#](#tab/cs-2)
```csharp
// Given a workbook name, return a dictionary of defined names.
// The pairs include the range name and a string representing the range.
var returnValue = new Dictionary<String, String>();
// Code removed here…
return returnValue;
```

### [Visual Basic](#tab/vb-2)
```vb
' Given a workbook name, return a dictionary of defined names.
' The pairs include the range name and a string representing the range.
Dim returnValue As New Dictionary(Of String, String)
' Code removed here…
Return returnValue
```
***


The code continues by opening the spreadsheet document, using the **Open** method and indicating that the
document should be open for read-only access (the final false parameter). Given the open workbook, the code uses the **WorkbookPart** property to navigate to the main workbook part. The code stores this reference in a variable named **wbPart**.
The code opens the spreadsheet document, using the **Open** method, indicating that the
document should be open for read-only access with the final false parameter. Given the open workbook, the code uses the **WorkbookPart** property to navigate to the main workbook part. The code stores this reference in a variable named **wbPart**.

### [C#](#tab/cs-3)
```csharp
// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document =
SpreadsheetDocument.Open(fileName, false))
{
// Retrieve a reference to the workbook part.
var wbPart = document.WorkbookPart;
// Code removed here.
}
```
[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet1)]

### [Visual Basic](#tab/vb-3)
```vb
' Open the spreadsheet document for read-only access.
Using document As SpreadsheetDocument =
SpreadsheetDocument.Open(fileName, False)

' Retrieve a reference to the workbook part.
Dim wbPart As WorkbookPart = document.WorkbookPart
' Code removed here…
End Using
```
[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet1)]
***


Expand All @@ -139,30 +52,10 @@ Given the workbook part, the next step is simple. The code uses the
defined names that are contained within the workbook. If the property returns a non-null value, the code then iterates through the collection, retrieving information about each named part and adding the key name) and value (range description) to the dictionary for each defined name.

### [C#](#tab/cs-4)
```csharp
// Retrieve a reference to the defined names collection.
DefinedNames definedNames = wbPart.Workbook.DefinedNames;

// If there are defined names, add them to the dictionary.
if (definedNames != null)
{
foreach (DefinedName dn in definedNames)
returnValue.Add(dn.Name.Value, dn.Text);
}
```
[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-4)
```vb
' Retrieve a reference to the defined names collection.
Dim definedNames As DefinedNames = wbPart.Workbook.DefinedNames

' If there are defined names, add them to the dictionary.
If definedNames IsNot Nothing Then
For Each dn As DefinedName In definedNames
returnValue.Add(dn.Name.Value, dn.Text)
Next
End If
```
[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet2)]
***


Expand All @@ -171,10 +64,10 @@ defined names that are contained within the workbook. If the property returns a
The following is the complete **GetDefinedNames** code sample in C\# and Visual Basic.

### [C#](#tab/cs)
[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs)]
[!code-csharp[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/cs/Program.cs#snippet0)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb)]
[!code-vb[](../../samples/spreadsheet/retrieve_a_dictionary_of_all_named_ranges/vb/Program.vb#snippet0)]

## See also

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// <Snippet0>
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;

GetDefinedNames(args[0]);

static Dictionary<String, String>
GetDefinedNames(String fileName)
static Dictionary<String, String>GetDefinedNames(String fileName)
{
// Given a workbook name, return a dictionary of defined names.
// The pairs include the range name and a string representing the range.
var returnValue = new Dictionary<String, String>();

// <Snippet1>
// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document =
SpreadsheetDocument.Open(fileName, false))
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
// Retrieve a reference to the workbook part.
var wbPart = document.WorkbookPart;

// </Snippet1>

// <Snippet2>
// Retrieve a reference to the defined names collection.
DefinedNames? definedNames = wbPart?.Workbook?.DefinedNames;

Expand All @@ -33,12 +34,18 @@ static Dictionary<String, String>
}
}
}
}

foreach (var pair in returnValue)
{
Console.WriteLine("{0} {1}", pair.Key, pair.Value);
// </Snippet2>
}

return returnValue;
}
}

// </Snippet0>

var definedNames = GetDefinedNames(args[0]);

foreach (var pair in definedNames)
{
Console.WriteLine("Name: {0} Value: {1}", pair.Key, pair.Value);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
' <Snippet0>
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Module Program
Sub Main(args As String())
End Sub
Dim definedNames = GetDefinedNames(args(0))

For Each definedName In definedNames
Console.WriteLine("Name: {0} Value: {1}", definedName.Key, definedName.Value)
Next

End Sub

Public Function GetDefinedNames(
ByVal fileName As String) As Dictionary(Of String, String)
Public Function GetDefinedNames(ByVal fileName As String) As Dictionary(Of String, String)

' Given a workbook name, return a dictionary of defined names.
' The pairs include the range name and a string representing the range.
Dim returnValue As New Dictionary(Of String, String)

' <Snippet1>
' Open the spreadsheet document for read-only access.
Using document As SpreadsheetDocument =
SpreadsheetDocument.Open(fileName, False)
Using document As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)

' Retrieve a reference to the workbook part.
Dim wbPart As WorkbookPart = document.WorkbookPart

' </Snippet1>

' <Snippet2>
' Retrieve a reference to the defined names collection.
Dim definedNames As DefinedNames = wbPart.Workbook.DefinedNames

Expand All @@ -30,7 +37,11 @@ Module Program
returnValue.Add(dn.Name.Value, dn.Text)
Next
End If

'</Snippet2>
End Using
Return returnValue
End Function
End Module
End Module

' </Snippet0>

0 comments on commit 06dd059

Please sign in to comment.