Fixed Send-SQLData behavior with tables/ranges when adding to a sheet

This commit is contained in:
jhoneill
2019-03-01 08:31:43 +00:00
parent 878ca570fb
commit f2c13949a4
2 changed files with 30 additions and 4 deletions

View File

@@ -53,6 +53,7 @@ Install-Module ImportExcel -scope CurrentUser
Install-Module ImportExcel
```
# What's new
- Modified Send-SQLDataToExcel so it creates tables and ranges itself; previously it relied on export-excel to do this which cause problems when adding data to an existing sheet (#555)
- Added new command Add-ExcelDataValidation which will apply different data-validation rules to ranges of cells
- Changed the export behavior so that (1) attempts to convert to a number only apply if the the value was a string; (2) Nulls are no longer converted to an empty string (3) there is a specific check for URIs and not just text which is a valid URI. Using UNC names in hyperlinks remains problematic.
- Changed the behavior of AutoSize in export excel so it only applies to the exported columns. Previously if something was exported next to pre-existing data, AutoSize would resize the whole sheet, potentially undoing things which had been set on the earlier data. If anyone relied on this behavior they will need to explictly tell the sheet to auto size with $sheet.cells.autofitColumns. (where $sheet points to the sheet, it might be $ExcelPackage.Workbook.Worksheets['Name'])

View File

@@ -278,17 +278,42 @@
else {$r = $StartRow}
#Get our Excel sheet and fill it with the data
$excelPackage = Export-Excel -Path $Path -WorkSheetname $WorkSheetname -PassThru
$excelPackage.Workbook.Worksheets[$WorkSheetname].Cells[$r,$StartColumn].LoadFromDataTable($dataTable, $printHeaders ) | Out-Null
$ws = $excelPackage.Workbook.Worksheets[$WorkSheetname]
$ws.Cells[$r,$StartColumn].LoadFromDataTable($dataTable, $printHeaders ) | Out-Null
#Apply date format
$LastRow = $StartRow + $DataTable.Rows.Count # if start row is 1, row 1 will be the header, row 2 will be data, so don't need to subtract 1
$LastCol = $StartColumn + $DataTable.Columns.Count - 1
$endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol)
$startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn)
$dataRange = "{0}:{1}" -f $startAddress, $endAddress
#Apply date format and range names
for ($c=0 ; $c -lt $DataTable.Columns.Count ; $c++) {
if ($DataTable.Columns[$c].DataType -eq [datetime]) {
Set-ExcelColumn -Worksheet $excelPackage.Workbook.Worksheets[$WorkSheetname] -Column ($c +1) -NumberFormat 'Date-Time'
Set-ExcelColumn -Worksheet $ws -Column ($c + $StartColumn) -NumberFormat 'Date-Time'
}
if ($AutoNameRange) {
Add-ExcelName -RangeName $DataTable.Columns[$c].ColumnName -Range $ws.Cells[($StartRow+1), ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
}
}
#Apply range or table to whole - we can't leave this to Export-Excel if we are inserting onto a sheet where there is already data
if ($RangeName) {
Add-ExcelName -Range $ws.Cells[$dataRange] -RangeName $RangeName
$null = $PSBoundParameters.Remove('RangeName')
}
if ($TableName) {
if ($PSBoundParameters.ContainsKey('TableStyle')) {
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle
$null = $PSBoundParameters.Remove('TableStyle')
}
else {Add-ExcelTable -Range $excelPackage.Workbook.Worksheets[$WorkSheetname].Cells[$dataRange] -TableName $TableName}
$null = $PSBoundParameters.Remove('TableName')
}
#Call export-excel with any parameters which don't relate to the SQL query
"Connection", "Database" , "Session", "MsSQLserver", "Destination" , "SQL" , "DataTable", "Path" | ForEach-Object {$null = $PSBoundParameters.Remove($_) }
"AutoNameRange", "Connection", "Database" , "Session", "MsSQLserver", "Destination" , "SQL" , "DataTable", "Path" | ForEach-Object {$null = $PSBoundParameters.Remove($_) }
Export-Excel -ExcelPackage $excelPackage @PSBoundParameters
}
else {Write-Warning -Message "No Data to insert."}