Fixed zero exported as String instead of numerical

This commit is contained in:
DarkLite1
2017-10-16 13:59:22 +02:00
parent 304a96e955
commit 4823424ae3
2 changed files with 81 additions and 51 deletions

View File

@@ -1,64 +1,94 @@
# Contributed by https://github.com/W1M0R
#Requires -Modules Pester
#Requires -Modules Assert
Import-Module ImportExcel -Force
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
function New-TestWorkbook {
$testWorkbook = "$($PSScriptRoot)\test.xlsx"
Import-Module $here -Force
Remove-Item $testWorkbook -ErrorAction Ignore
$testWorkbook
$WarningPreference = 'SilentlyContinue'
$ProgressPreference = 'SilentlyContinue'
Function Test-isNumeric {
Param (
[Parameter(ValueFromPipeline)]$x
)
Return $x -is [byte] -or $x -is [int16] -or $x -is [int32] -or $x -is [int64] `
-or $x -is [sbyte] -or $x -is [uint16] -or $x -is [uint32] -or $x -is [uint64] `
-or $x -is [float] -or $x -is [double] -or $x -is [decimal]
}
function Remove-TestWorkbook {
New-TestWorkbook | Out-Null
$fakeData = [PSCustOmobject]@{
Property_1_Date = (Get-Date).ToString('d') # US '10/16/2017' BE '16/10/2107'
Property_2_Formula = '=SUM(G2:H2)'
Property_3_String = 'My String'
Property_4_String = 'a'
Property_5_IPAddress = '10.10.25.5'
Property_6_Number = '0'
Property_7_Number = '5'
Property_8_Number = '007'
Property_9_Number = (33).ToString('F2') # US '33.00' BE '33,00'
Property_10_Number = (5/3).ToString('F2') # US '1.67' BE '1,67'
Property_11_Number = (15999998/3).ToString('N2') # US '5,333,332.67' BE '5.333.332,67'
Property_12_Number = '1.555,83'
Property_13_PhoneNr = '+32 44'
Property_14_PhoneNr = '+32 4 4444 444'
Property_15_PhoneNr = '+3244444444'
}
function New-TestDataCsv {
@"
ID,Product,Quantity,Price,Total
12001,Nails,37,3.99,147.63
12002,Hammer,5,12.10,60.5
12003,Saw,12,15.37,184.44
01200,Drill,20,8,160
00120,Crowbar,7,23.48,164.36
true,Bla,7,82,12
false,Bla,7,82,12
2009-05-01 14:57:32.8,Yay,1,3,2
"@ | ConvertFrom-Csv
}
$Path = 'Test.xlsx'
Describe "Export-Excel" {
Describe 'Export-Excel' {
in $TestDrive {
Describe 'Number conversion' {
Context 'numerical values expected' {
#region Create test file
$fakeData | Export-Excel -Path $Path
$csvData = New-TestDataCsv
$workbook = New-TestWorkbook
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
$Excel = New-Object OfficeOpenXml.ExcelPackage $Path
$Worksheet = $Excel.Workbook.WorkSheets[1]
#endregion
Context "Importing CSV data from a here string" {
It "All properties are type [string]" {
$csvData | % {
$_.PSObject.Properties | % {
$_.Value -is [string] | Should Be $true
it 'zero' {
$fakeData.Property_6_Number | Should -BeExactly '0'
$Worksheet.Cells[2, 6].Text | Should -BeExactly $fakeData.Property_6_Number
$Worksheet.Cells[2, 6].Value | Test-isNumeric | Should -Be $true
}
It 'regular number' {
$fakeData.Property_7_Number | Should -BeExactly '5'
$Worksheet.Cells[2, 7].Text | Should -BeExactly $fakeData.Property_7_Number
$Worksheet.Cells[2, 7].Value | Test-isNumeric | Should -Be $true
}
It 'number starting with zero' {
$fakeData.Property_8_Number | Should -BeExactly '007'
$Worksheet.Cells[2, 8].Text | Should -BeExactly '7'
$Worksheet.Cells[2, 8].Value | Test-isNumeric | Should -Be $true
}
It 'decimal number' {
# US '33.00' BE '33,00'
$fakeData.Property_9_Number | Should -BeExactly (33).ToString('F2')
$Worksheet.Cells[2, 9].Text | Should -BeExactly '33'
$Worksheet.Cells[2, 9].Value | Test-isNumeric | Should -Be $true
# US '1.67' BE '1,67'
$fakeData.Property_10_Number | Should -BeExactly (5/3).ToString('F2')
$Worksheet.Cells[2, 10].Text | Should -BeExactly $fakeData.Property_10_Number
$Worksheet.Cells[2, 10].Value | Test-isNumeric | Should -Be $true
}
It 'thousand seperator and decimal number' {
# US '5,333,332.67' BE '5.333.332,67'
# Excel BE '5333332,67'
$fakeData.Property_11_Number | Should -BeExactly (15999998/3).ToString('N2')
$Worksheet.Cells[2, 11].Text | Should -BeExactly $fakeData.Property_11_Number
$Worksheet.Cells[2, 11].Value | Test-isNumeric | Should -Be $true
}
}
}
It "Leading zeroes are preserved" {
$csvData[4] | Select-Object -ExpandProperty ID | Should Be "00120"
}
}
Context "Piping CSV data to Export-Excel" {
$xlPkg = $csvData | Export-Excel $workbook -PassThru
$ws = $xlPkg.Workbook.WorkSheets[1]
It "Exports numeric strings as numbers" {
$csvData[2] | Select-Object -ExpandProperty ID | Should Be "12003"
$ws.Cells["A4"].Value -is [double] | Should Be $true
$ws.Cells["A4"].Value | Should Be 12003
}
$xlPkg.Save()
$xlPkg.Dispose()
}
Remove-TestWorkbook
}

View File

@@ -310,7 +310,7 @@ Function Export-Excel {
Default {
#region Save a value as a number if possible
if ($Number = ConvertTo-Number $_) {
if (($Number = ConvertTo-Number $_) -ne $null) {
$TargetCell.Value = $Number
$targetCell.Style.Numberformat.Format = $Numberformat
Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($TargetCell.Value)' as number converted from '$_' with format '$Numberformat'"