From 5b29ccd9c2422bcbbe704bbe7a4381cdb92ce95d Mon Sep 17 00:00:00 2001 From: jhoneill Date: Sun, 30 Sep 2018 22:49:35 +0100 Subject: [PATCH 1/2] Send-SQLDataToExcel applies formatting to date fields. --- Send-SqlDataToExcel.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Send-SqlDataToExcel.ps1 b/Send-SqlDataToExcel.ps1 index c501efa..f487acd 100644 --- a/Send-SqlDataToExcel.ps1 +++ b/Send-SqlDataToExcel.ps1 @@ -269,6 +269,13 @@ $excelPackage = Export-Excel -Path $Path -WorkSheetname $WorkSheetname -PassThru $excelPackage.Workbook.Worksheets[$WorkSheetname].Cells[$r,$StartColumn].LoadFromDataTable($dataTable, $printHeaders ) | Out-Null + #Apply date format + 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' + } + } + #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($_) } Export-Excel -ExcelPackage $excelPackage @PSBoundParameters From 74e5fee1610feaeccc6f937e833d242e2746c5a2 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Tue, 2 Oct 2018 21:15:16 +0100 Subject: [PATCH 2/2] Fixed bugs relating to handling of null. zero and empty string as data. --- Export-Excel.ps1 | 27 +++++++++++++-------------- __tests__/Export-Excel.Tests.ps1 | 16 +++++++++++----- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index b2e336c..1ab996a 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -173,7 +173,7 @@ .PARAMETER ReturnRange If specified, Export-Excel returns the range of added cells in the format "A1:Z100" .PARAMETER PassThru - If specified, Export-Excel returns an object representing the Excel package without saving the package first. + If specified, Export-Excel returns an object representing the Excel package without saving the package first. To save, you need to call Close-ExcelPackage or send the object back to Export-Excel, or use its .Save() or SaveAs() method. .EXAMPLE Get-Process | Export-Excel .\Test.xlsx -show @@ -684,15 +684,14 @@ } Process { - if ($TargetData) { + if ($PSBoundParameters.ContainsKey("TargetData")) { try { if ($firstTimeThru) { $firstTimeThru = $false - $isDataTypeValueType = $TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort|URI|ExcelHyperLink' + $isDataTypeValueType = ($null -eq $TargetData) -or ($TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort|URI|ExcelHyperLink') if ($isDataTypeValueType -and -not $Append) {$row -= 1} #row incremented before adding values, so it is set to the number of rows inserted at the end - Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'" + if ($null -ne $TargetData) {Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'" } } - if ($isDataTypeValueType) { $ColumnIndex = $StartColumn $Row += 1 @@ -744,18 +743,18 @@ } End { - if ($firstTimeThru) { - $LastRow = $ws.Dimension.End.Row - $LastCol = $ws.Dimension.End.Column - $endAddress = $ws.Dimension.End.Address + if ($firstTimeThru -and $ws.Dimension) { + $LastRow = $ws.Dimension.End.Row + $LastCol = $ws.Dimension.End.Column + $endAddress = $ws.Dimension.End.Address } else { - $LastRow = $Row - $LastCol = $ColumnIndex - $endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol) + $LastRow = $Row + $LastCol = $ColumnIndex + $endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol) } - $startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn) - $dataRange = "{0}:{1}" -f $startAddress, $endAddress + $startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn) + $dataRange = "{0}:{1}" -f $startAddress, $endAddress Write-Debug "Data Range '$dataRange'" if ($AutoNameRange) { diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index db56e3c..0a67602 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -120,10 +120,10 @@ Describe ExportExcel { $path = "$env:TEMP\Test.xlsx" Remove-item -Path $path -ErrorAction SilentlyContinue #testing -ReturnRange switch and applying number format to Formulas as well as values. - $returnedRange = Write-Output -1 668 34 777 860 -0.5 119 -0.1 234 788,"=A9+A10" | Export-Excel -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00' -Path $path -ReturnRange + $returnedRange = @($null, -1, 0, 34, 777, "", -0.5, 119, -0.1, 234, 788,"=A9+A10") | Export-Excel -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00' -Path $path -ReturnRange it "Created a new file and returned the expected range " { Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true - $returnedRange | Should be "A1:A11" + $returnedRange | Should be "A1:A12" } $Excel = Open-ExcelPackage -Path $path @@ -135,16 +135,22 @@ Describe ExportExcel { it "Created the worksheet with the expected name, number of rows and number of columns " { $ws.Name | Should be "sheet1" $ws.Dimension.Columns | Should be 1 - $ws.Dimension.Rows | Should be 11 + $ws.Dimension.Rows | Should be 12 } it "Set the default style for the sheet as expected " { $ws.cells.Style.Numberformat.Format | Should be '[Blue]$#,##0.00;[Red]-$#,##0.00' } - it "Set the default style and value for Cell A1 as expected " { + it "Set the default style and set values for Cells as expected, handling null,0 and '' " { $ws.cells[1, 1].Style.Numberformat.Format | Should be '[Blue]$#,##0.00;[Red]-$#,##0.00' - $ws.cells[1, 1].Value | Should be -1 + $ws.cells[1, 1].Value | Should beNullorEmpty + $ws.cells[2, 1].Value | Should be -1 + $ws.cells[3, 1].Value | Should be 0 + $ws.cells[5, 1].Value | Should be 777 + $ws.cells[6, 1].Value | Should be "" + $ws.cells[4, 1].Style.Numberformat.Format | Should be '[Blue]$#,##0.00;[Red]-$#,##0.00' + } }