From aef4513803e1a3e0bcf3eb50fa84b2146de0f289 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Sun, 19 May 2024 21:43:55 -0500 Subject: [PATCH] only lookup cells once per row --- Public/Import-Excel.ps1 | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index 920ce1c9..812422fb 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -217,25 +217,33 @@ #Disabled write-verbose for speed # Write-Verbose "Import row '$R'" $NewRow = [Ordered]@{ } + + # Get the entire row + $rowCells = $sheet.Cells[$R, $StartColumn, $R, $EndColumn].Value + + $i = 0 + if ($TextColRegEx) { foreach ($P in $PropertyNames) { - $cell = $sheet.Cells[$R, $P.Column] + $cellValue = $rowCells[0, $i] $MatchTest = $TextColRegEx.Match($P.value) if ($MatchTest.groups.name -eq "astext") { - $NewRow[$P.Value] = $cell.Text + $NewRow[$P.Value] = $cellValue } - elseif ($MatchTest.groups.name -eq "asdate" -and $cell.Value -is [System.ValueType]) { - $NewRow[$P.Value] = [datetime]::FromOADate($cell.Value) + elseif ($MatchTest.groups.name -eq "asdate" -and $cellValue -is [System.ValueType]) { + $NewRow[$P.Value] = [datetime]::FromOADate($cellValue) } - else { $NewRow[$P.Value] = $cell.Value } + else { $NewRow[$P.Value] = $cellValue } + $i++ } } else { foreach ($P in $PropertyNames) { - $NewRow[$P.Value] = $sheet.Cells[$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)'." + $NewRow[$P.Value] = $rowCells[0, $i] + $i++ } } + $xlBook["$targetSheetname"] += [PSCustomObject]$NewRow } #endregion