mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Merge pull request #449 from jhoneill/master
Fix for bug handling null and zero; date format applied in send-sql
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user