mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-15 07:43:23 +00:00
Revert "Revert "Added 'DataOnly' parameter""
This reverts commit 782c027960.
This commit is contained in:
@@ -78,7 +78,8 @@ function Import-Excel {
|
|||||||
$WorkSheetname=1,
|
$WorkSheetname=1,
|
||||||
[int]$HeaderRow=1,
|
[int]$HeaderRow=1,
|
||||||
[string[]]$Header,
|
[string[]]$Header,
|
||||||
[switch]$NoHeader
|
[switch]$NoHeader,
|
||||||
|
[switch]$DataOnly
|
||||||
)
|
)
|
||||||
|
|
||||||
Process {
|
Process {
|
||||||
@@ -98,14 +99,33 @@ function Import-Excel {
|
|||||||
$Columns=$dimension.Columns
|
$Columns=$dimension.Columns
|
||||||
|
|
||||||
if($NoHeader) {
|
if($NoHeader) {
|
||||||
foreach ($Row in 0..($Rows-1)) {
|
if ($DataOnly) {
|
||||||
$newRow = [Ordered]@{}
|
$CellsWithValues = $worksheet.Cells | where Value
|
||||||
foreach ($Column in 0..($Columns-1)) {
|
|
||||||
$propertyName = "P$($Column+1)"
|
|
||||||
$newRow.$propertyName = $worksheet.Cells[($Row+1),($Column+1)].Value
|
|
||||||
}
|
|
||||||
|
|
||||||
[PSCustomObject]$newRow
|
$Script:i = 0
|
||||||
|
$ColumnReference = $CellsWithValues | Select-Object -ExpandProperty End | Group-Object Column |
|
||||||
|
Select-Object @{L='Column';E={$_.Name}}, @{L='NewColumn';E={$Script:i++; $Script:i}}
|
||||||
|
|
||||||
|
$CellsWithValues | Select-Object -ExpandProperty End | Group-Object Row | ForEach-Object {
|
||||||
|
$newRow = [Ordered]@{}
|
||||||
|
|
||||||
|
foreach ($C in $ColumnReference) {
|
||||||
|
$newRow."P$($C.NewColumn)" = $worksheet.Cells[($_.Name),($C.Column)].Value
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]$newRow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
foreach ($Row in 0..($Rows-1)) {
|
||||||
|
$newRow = [Ordered]@{}
|
||||||
|
foreach ($Column in 0..($Columns-1)) {
|
||||||
|
$propertyName = "P$($Column+1)"
|
||||||
|
$newRow.$propertyName = $worksheet.Cells[($Row+1),($Column+1)].Value
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]$newRow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(!$Header) {
|
if(!$Header) {
|
||||||
@@ -116,16 +136,36 @@ function Import-Excel {
|
|||||||
|
|
||||||
if($Rows -eq 1) {
|
if($Rows -eq 1) {
|
||||||
$Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h}
|
$Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h}
|
||||||
} else {
|
}
|
||||||
foreach ($Row in ($HeaderRow+1)..$Rows) {
|
else {
|
||||||
$h=[Ordered]@{}
|
if ($DataOnly) {
|
||||||
foreach ($Column in 0..($Columns-1)) {
|
$CellsWithValues = $worksheet.Cells | where Value
|
||||||
|
|
||||||
|
$Script:i = -1
|
||||||
|
$ColumnReference = $CellsWithValues | Select-Object -ExpandProperty End | Group-Object Column |
|
||||||
|
Select-Object @{L='Column';E={$_.Name}}, @{L='NewColumn';E={$Script:i++; $Header[$Script:i]}}
|
||||||
|
|
||||||
|
$CellsWithValues | Select-Object -ExpandProperty End | Group-Object Row | ForEach-Object {
|
||||||
|
$newRow = [Ordered]@{}
|
||||||
|
|
||||||
|
foreach ($C in $ColumnReference) {
|
||||||
|
$newRow."$($C.NewColumn)" = $worksheet.Cells[($_.Name),($C.Column)].Value
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]$newRow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
foreach ($Row in ($HeaderRow+1)..$Rows) {
|
||||||
|
$h=[Ordered]@{}
|
||||||
|
foreach ($Column in 0..($Columns-1)) {
|
||||||
if($Header[$Column].Length -gt 0) {
|
if($Header[$Column].Length -gt 0) {
|
||||||
$Name = $Header[$Column]
|
$Name = $Header[$Column]
|
||||||
$h.$Name = $worksheet.Cells[$Row,($Column+1)].Value
|
$h.$Name = $worksheet.Cells[$Row,($Column+1)].Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[PSCustomObject]$h
|
[PSCustomObject]$h
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user