mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-12 06:13:26 +00:00
Add ConvertEmptyStringsToNull parameter, along with logic to replace an empty string with NULL if the parameter is present.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
function ConvertFrom-ExcelToSQLInsert {
|
function ConvertFrom-ExcelToSQLInsert {
|
||||||
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
$TableName,
|
$TableName,
|
||||||
@@ -11,18 +12,28 @@ function ConvertFrom-ExcelToSQLInsert {
|
|||||||
[int]$HeaderRow = 1,
|
[int]$HeaderRow = 1,
|
||||||
[string[]]$Header,
|
[string[]]$Header,
|
||||||
[switch]$NoHeader,
|
[switch]$NoHeader,
|
||||||
[switch]$DataOnly
|
[switch]$DataOnly,
|
||||||
|
[switch]$ConvertEmptyStringsToNull
|
||||||
)
|
)
|
||||||
|
|
||||||
$null = $PSBoundParameters.Remove('TableName')
|
$null = $PSBoundParameters.Remove('TableName')
|
||||||
|
$null = $PSBoundParameters.Remove('ConvertEmptyStringsToNull')
|
||||||
|
|
||||||
$params = @{} + $PSBoundParameters
|
$params = @{} + $PSBoundParameters
|
||||||
|
|
||||||
ConvertFrom-ExcelData @params {
|
ConvertFrom-ExcelData @params {
|
||||||
param($propertyNames, $record)
|
param($propertyNames, $record)
|
||||||
|
|
||||||
$ColumnNames = "'" + ($PropertyNames -join "', '") + "'"
|
$ColumnNames = "'" + ($PropertyNames -join "', '") + "'"
|
||||||
$values = foreach ($propertyName in $PropertyNames) { $record.$propertyName }
|
$values = foreach ($propertyName in $PropertyNames) {
|
||||||
$targetValues = "'" + ($values -join "', '") + "'"
|
if($ConvertEmptyStringsToNull.IsPresent -and [string]::IsNullOrEmpty($record.$propertyName)) {
|
||||||
|
'NULL'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
"'" + $record.$propertyName + "'"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$targetValues = ($values -join ", ")
|
||||||
|
|
||||||
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
|
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user