-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1110 from DavisHenckel/FAQ_Docs
- Loading branch information
Showing
8 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Create an Empty Excel File | ||
Use an empty string to export to an excel file. | ||
```powershell | ||
#Build an Excel file named: "file.xlsx" containing a worksheet: "MyWorksheet" | ||
"" | Export-Excel -Path "C:\Test\file.xlsx" -WorksheetName "MyWorksheet" | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# How to Read an Existing Excel File | ||
## Enumerate the Excel File Contents | ||
```powershell | ||
#Load the Excel file into a PSCustomObject | ||
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1" | ||
``` | ||
|
||
## Visual of Data Structure | ||
The File C:\Test\file.xlsx contains | ||
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png) | ||
|
||
After loading this data into ```$ExcelFile``` the data is stored like: | ||
![ExcelFileDebugImg](/images/FAQ_Images/ExcelFileDebugImg.jpg) | ||
|
||
## Other Common Operations | ||
|
||
### Load a Column | ||
```powershell | ||
$SpecificColumn = $ExcelFile."anotherHeader" #loads column with the header "anotherHeader" -- data stored in an array | ||
``` | ||
|
||
### Load a Row | ||
```powershell | ||
$SpecificRow = $ExcelFile[1] #Loads row at index 1. Index 1 is the first row instead of 0. | ||
``` | ||
|
||
### Map Contents to Hashtable to Interpret Data | ||
Sometimes mapping to a Hashtable is more convenient to have access to common Hashtable operations. Enumerate a Hashtable with the row's data by: | ||
```powershell | ||
$HashTable = @{} | ||
$SpecificRow= $ExcelFile[2] | ||
$SpecificRow.psobject.properties | ForEach-Object { | ||
$HashTable[$_.Name] = $_.Value | ||
} | ||
``` | ||
To then iterate through the enumerated Hashtable: | ||
```powershell | ||
ForEach ($Key in ($HashTable.GetEnumerator()) | Where-Object {$_.Value -eq "x"}){ #Only grabs a key where the value is "x" | ||
#values accessible with $Key.Name or $Key.Value | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Write to an Existing Excel File | ||
### Enumerate the Excel File | ||
The cmdlets ```Open-ExcelPackage``` and ```Close-ExcelPackage``` allow for direct modification to Excel file contents. | ||
```powershell | ||
$ExcelPkg = Open-ExcelPackage -Path "C:\Test\file.xlsx" | ||
``` | ||
Contents of file.xlsx: | ||
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png) | ||
### Enumerate the Worksheet to View or Modify the Data | ||
```powershell | ||
$WorkSheet = $ExcelPkg.Workbook.Worksheets["sheet1"].Cells #open excel worksheet cells from worksheet "sheet1" | ||
``` | ||
Visual of data structure: | ||
![DataStructureExcelPkg](/images/FAQ_Images/DataStructureExcelPkg.png) | ||
A1 contains "someHeader", A2 contains "data1" etc. | ||
### Modify a Specific Value in a File | ||
Values can be accessed by row, column. Similar to a 2D array. | ||
```powershell | ||
$WorkSheet[1,4].Value = "New Column Header" #Starts at index 1 not 0 | ||
``` | ||
Contents of file.xlsx after modifying: | ||
![ExcelFileContentsPostAdd](/images/FAQ_Images/ExcelFileContentsPostAdd.png) | ||
### Load Value at Specific Index | ||
```powershell | ||
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A | ||
``` | ||
```$ValueAtIndex``` now contains: ![ValueAtIndexData](/images/FAQ_Images/ValueAtIndexData.png) | ||
### Save File After Modifying | ||
The changes will not display in the Excel file until Close-ExcelPackage is called. | ||
```powershell | ||
Close-ExcelPackage $ExcelPkg #close and save changes made to the Excel file. | ||
``` | ||
**Note**: If the file is currently in use, Close-ExcelPackage will return an error and will not save the information. | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.