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] Performance optimization #1602

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 15 additions & 7 deletions Public/Import-Excel.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Import-Excel {
function Import-Excel {
[CmdLetBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectUsageOfAssignmentOperator', '', Justification = 'Intentional')]
Expand Down Expand Up @@ -161,7 +161,10 @@
#using Hash tables: "we've seen it" is all we need, no need to worry about "seen it before" / "Seen it many times".
$colHash = @{ }
$rowHash = @{ }
foreach ($cell in $sheet.Cells[$range]) {

$cells = $sheet.Cells[$range]

foreach ($cell in $cells) {
if ($null -ne $cell.Value ) { $colHash[$cell.Start.Column] = 1; $rowHash[$cell.Start.row] = 1 }
}
$rows = ( $StartRow..$EndRow ).Where( { $rowHash[$_] })
Expand Down Expand Up @@ -217,21 +220,26 @@
#Disabled write-verbose for speed
# Write-Verbose "Import row '$R'"
$NewRow = [Ordered]@{ }

# Get the entire row first
$row = $sheet.Cells[$R, 1, $R, $sheet.Dimension.End.Column]

if ($TextColRegEx) {
foreach ($P in $PropertyNames) {
$cell = $row[$R, $P.Column]
$MatchTest = $TextColRegEx.Match($P.value)
if ($MatchTest.groups.name -eq "astext") {
$NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Text
$NewRow[$P.Value] = $cell.Text
}
elseif ($MatchTest.groups.name -eq "asdate" -and $sheet.Cells[$R, $P.Column].Value -is [System.ValueType]) {
$NewRow[$P.Value] = [datetime]::FromOADate(($sheet.Cells[$R, $P.Column].Value))
elseif ($MatchTest.groups.name -eq "asdate" -and $cell.Value -is [System.ValueType]) {
$NewRow[$P.Value] = [datetime]::FromOADate($cell.Value)
}
else { $NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Value }
else { $NewRow[$P.Value] = $cell.Value }
}
}
else {
foreach ($P in $PropertyNames) {
$NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Value
$NewRow[$P.Value] = $row[$R, $P.Column].Value
# Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$($p.Value)' and value '$($Worksheet.Cells[$R, $P.Column].Value)'."
}
}
Expand Down