Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import-Excel Extended HeaderName functionality #579

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -281,7 +281,7 @@ function Import-Excel {
[String]$WorksheetName,
[Parameter(ParameterSetName = 'PathB' , Mandatory)]
[Parameter(ParameterSetName = 'PackageB', Mandatory)]
[String[]]$HeaderName ,
$HeaderName ,
[Parameter(ParameterSetName = 'PathC' , Mandatory)]
[Parameter(ParameterSetName = 'PackageC', Mandatory)]
[Switch]$NoHeader ,
Expand Down Expand Up @@ -321,7 +321,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 @@ -333,9 +333,18 @@ function Import-Excel {
throw 'The top row can never be less than 1 when we need to retrieve headers from the worksheet.' ; return
}

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