Skip to content

Commit

Permalink
Import-ExcelExtended HeaderName functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ili101 committed Apr 8, 2019
1 parent 0febb4a commit cbbf205
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Examples/Import-ExcelWithHeaderName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Create Test File
$path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue
$processes = Get-Process | Select-Object -First 10
$Processes | Export-Excel $path

# Import the file as is.
Import-Excel -Path $path | Format-Table
# Import the file replacing the name of the 2 first headers and discarding the rest.
Import-Excel -Path $path -HeaderName 'NewA', 'NewB'
# Import the file selecting 3 of it's columns by name, renaming 2, changing the order and discarding the rest.
Import-Excel -Path $path -HeaderName ([Ordered]@{Name = 'Process Name' ; StartTime = 'Start'; Path = 'Path'})
<# Output Example:
Process Name Start Path
------------ ----- ----
ApplicationFrameHost 07/04/2019 11:02:19 C:\WINDOWS\system32\ApplicationFrameHost.exe
AppVShNotify
audiodg 08/04/2019 19:21:32
chrome 08/04/2019 16:49:47 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
chrome 08/04/2019 16:49:27 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
chrome 08/04/2019 16:49:27 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
#>
15 changes: 12 additions & 3 deletions ImportExcel.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function Import-Excel {
[ValidateNotNullOrEmpty()]
[String]$WorksheetName,
[Parameter(ParameterSetName = 'B', Mandatory)]
[String[]]$HeaderName ,
$HeaderName,
[Parameter(ParameterSetName = 'C', Mandatory)]
[Switch]$NoHeader ,
[Alias('HeaderRow', 'TopRow')]
Expand Down Expand Up @@ -312,7 +312,7 @@ function Import-Excel {
$C | Select-Object @{N = 'Column'; E = {$_}}, @{N = 'Value'; E = {'P' + $i}}
}
}
elseif ($HeaderName) {
elseif ($HeaderName -and $HeaderName -isnot [Collections.Hashtable] -and $HeaderName -isnot [Collections.Specialized.OrderedDictionary]) {
$i = 0
foreach ($H in $HeaderName) {
$H | Select-Object @{N = 'Column'; E = {$Columns[$i]}}, @{N = 'Value'; E = {$H}}
Expand All @@ -324,9 +324,18 @@ function Import-Excel {
throw 'The top row can never be equal to 0 when we need to retrieve headers from the worksheet.'
}

foreach ($C in $Columns) {
$PropertyNames = foreach ($C in $Columns) {
$Worksheet.Cells[$StartRow, $C] | Where-Object {$_.Value} | Select-Object @{N = 'Column'; E = {$C}}, Value
}

if ($HeaderName) {
foreach ($H in $HeaderName.GetEnumerator()) {
$H | Select-Object @{N = 'Column'; E = {($PropertyNames | Where-Object {$_.Value -eq $H.Name}).Column}}, @{N = 'Value'; E = {$H.Value}}
}
}
else {
$PropertyNames
}
}
}
Catch {
Expand Down

0 comments on commit cbbf205

Please sign in to comment.