From 84684ae27bf43d60ef538f89bb128829b3ca0856 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Fri, 17 May 2024 22:05:49 -0500 Subject: [PATCH 1/3] only get cell value once --- Public/Import-Excel.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index 3734f2b..920ce1c 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -219,14 +219,15 @@ $NewRow = [Ordered]@{ } if ($TextColRegEx) { foreach ($P in $PropertyNames) { + $cell = $sheet.Cells[$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 { From cfb556ea7737a9f8a0a7d9331fb633745641cc72 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Sun, 19 May 2024 21:50:25 -0500 Subject: [PATCH 2/3] only lookup cells once per row --- Public/Import-Excel.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index 920ce1c..44657aa 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -217,9 +217,13 @@ #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 = $sheet.Cells[$R, $P.Column] + $cell = $row[$R, $P.Column] $MatchTest = $TextColRegEx.Match($P.value) if ($MatchTest.groups.name -eq "astext") { $NewRow[$P.Value] = $cell.Text @@ -232,7 +236,7 @@ } 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)'." } } From 71ef167868d1ab46c9d5cf019c09581f13fc2f11 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Mon, 20 May 2024 12:01:56 -0500 Subject: [PATCH 3/3] only lookup cells once --- Public/Import-Excel.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index 44657aa..ec8307a 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -1,4 +1,4 @@ -function Import-Excel { +function Import-Excel { [CmdLetBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectUsageOfAssignmentOperator', '', Justification = 'Intentional')] @@ -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[$_] })