Skip to content

Commit

Permalink
only lookup cells once per row
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmiller-mesirow committed May 20, 2024
1 parent 84684ae commit aef4513
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Public/Import-Excel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aef4513

Please sign in to comment.