Fixed bugs relating to handling of null. zero and empty string as data.

This commit is contained in:
jhoneill
2018-10-02 21:15:16 +01:00
parent 5b29ccd9c2
commit 74e5fee161
2 changed files with 24 additions and 19 deletions

View File

@@ -684,15 +684,14 @@
} }
Process { Process {
if ($TargetData) { if ($PSBoundParameters.ContainsKey("TargetData")) {
try { try {
if ($firstTimeThru) { if ($firstTimeThru) {
$firstTimeThru = $false $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 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) { if ($isDataTypeValueType) {
$ColumnIndex = $StartColumn $ColumnIndex = $StartColumn
$Row += 1 $Row += 1
@@ -744,18 +743,18 @@
} }
End { End {
if ($firstTimeThru) { if ($firstTimeThru -and $ws.Dimension) {
$LastRow = $ws.Dimension.End.Row $LastRow = $ws.Dimension.End.Row
$LastCol = $ws.Dimension.End.Column $LastCol = $ws.Dimension.End.Column
$endAddress = $ws.Dimension.End.Address $endAddress = $ws.Dimension.End.Address
} }
else { else {
$LastRow = $Row $LastRow = $Row
$LastCol = $ColumnIndex $LastCol = $ColumnIndex
$endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol) $endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol)
} }
$startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn) $startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn)
$dataRange = "{0}:{1}" -f $startAddress, $endAddress $dataRange = "{0}:{1}" -f $startAddress, $endAddress
Write-Debug "Data Range '$dataRange'" Write-Debug "Data Range '$dataRange'"
if ($AutoNameRange) { if ($AutoNameRange) {

View File

@@ -120,10 +120,10 @@ Describe ExportExcel {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
#testing -ReturnRange switch and applying number format to Formulas as well as values. #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 " { it "Created a new file and returned the expected range " {
Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true
$returnedRange | Should be "A1:A11" $returnedRange | Should be "A1:A12"
} }
$Excel = Open-ExcelPackage -Path $path $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 " { it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | Should be "sheet1" $ws.Name | Should be "sheet1"
$ws.Dimension.Columns | Should be 1 $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 " { it "Set the default style for the sheet as expected " {
$ws.cells.Style.Numberformat.Format | Should be '[Blue]$#,##0.00;[Red]-$#,##0.00' $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].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'
} }
} }