Improved behavior of -NumberFormat in Export-Excel

This commit is contained in:
jhoneill
2018-08-15 22:52:22 +01:00
parent ce3fd2021f
commit bc65699068
3 changed files with 31 additions and 5 deletions

View File

@@ -498,7 +498,7 @@
$TargetCell.Value = $_ $TargetCell.Value = $_
$TargetCell.Style.Numberformat.Format = '[h]:mm:ss' $TargetCell.Style.Numberformat.Format = '[h]:mm:ss'
break break
} }
{ $_ -is [System.ValueType]} { { $_ -is [System.ValueType]} {
# Save numerics, setting format if need be. # Save numerics, setting format if need be.
$TargetCell.Value = $_ $TargetCell.Value = $_
@@ -641,7 +641,7 @@
#Otherwise the default will be unbolded. #Otherwise the default will be unbolded.
$ws.Cells[$Row, $StartColumn].Style.Font.Bold = $True $ws.Cells[$Row, $StartColumn].Style.Font.Bold = $True
} }
if ($TitleBackgroundColor ) { if ($TitleBackgroundColor ) {
$ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
$ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) $ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
} }
@@ -650,7 +650,11 @@
else { $Row = $StartRow } else { $Row = $StartRow }
$ColumnIndex = $StartColumn $ColumnIndex = $StartColumn
$Numberformat = Expand-NumberFormat -NumberFormat $Numberformat $Numberformat = Expand-NumberFormat -NumberFormat $Numberformat
$setNumformat = ($Numberformat -ne $ws.Cells.Style.Numberformat.Format) if ($row -le 2 -and $ColumnIndex -eq 1 -and $Numberformat -ne $ws.Cells.Style.Numberformat.Format) {
$ws.Cells.Style.Numberformat.Format = $Numberformat
$setNumformat = $false
}
else { $setNumformat = ($Numberformat -ne $ws.Cells.Style.Numberformat.Format) }
$firstTimeThru = $true $firstTimeThru = $true
$isDataTypeValueType = $false $isDataTypeValueType = $false
@@ -671,7 +675,7 @@
Try { Try {
if ($firstTimeThru) { if ($firstTimeThru) {
$firstTimeThru = $false $firstTimeThru = $false
$isDataTypeValueType = $TargetData.GetType().name -match 'string|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort' $isDataTypeValueType = $TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort'
if ($isDataTypeValueType) {$row -= 1} #row incremented before adding values, so it is set to the number of rows inserted at the end if ($isDataTypeValueType) {$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'" Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'"
} }

View File

@@ -51,7 +51,8 @@ Install-Module ImportExcel -scope CurrentUser
```PowerShell ```PowerShell
Install-Module ImportExcel Install-Module ImportExcel
``` ```
# New to Aug 14th # New to Aug 15h
- NumberFormat in Export-Excel now sets the default for on a new / blank sheet; but [still] sets individual cells when adding to a sheet
- Added support for timespans in Export excel ; set as elapsed hours, mins, secs [h]:mm:sss - Added support for timespans in Export excel ; set as elapsed hours, mins, secs [h]:mm:sss
- In Export-Excel improved the catch-all handler for insering values to cope better with nested objects (#419) and reduce the number of parse operations - In Export-Excel improved the catch-all handler for insering values to cope better with nested objects (#419) and reduce the number of parse operations
- Added -Calculate switch to Export-Excel and Close-Excel Package; EPPlus needs formulas to OMIT the leading = sign so where formula is set it now strips a leading = sign - Added -Calculate switch to Export-Excel and Close-Excel Package; EPPlus needs formulas to OMIT the leading = sign so where formula is set it now strips a leading = sign

View File

@@ -143,6 +143,27 @@ Describe ExportExcel {
} }
} }
Context " # Number format parameter" {
BeforeAll {
$path = "$env:temp\test.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
1..10 | Export-Excel -Path $path -Numberformat 'Number'
1..10 | Export-Excel -Path $path -Numberformat 'Percentage' -Append
21..30 | Export-Excel -Path $path -Numberformat 'Currency' -StartColumn 3
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets[1]
}
it "Set the worksheet default number format correctly " {
$ws.Cells.Style.Numberformat.Format | Should be "0.00"
}
it "Set number formats on specific blocks of cells w " {
$ws.Cells["A2" ].Style.Numberformat.Format | Should be "0.00"
$ws.Cells["c19"].Style.Numberformat.Format | Should be "0.00"
$ws.Cells["A19"].Style.Numberformat.Format | Should be "0.00%"
$ws.Cells["C6" ].Style.Numberformat.Format | Should be (Expand-NumberFormat "currency")
}
}
Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" { Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" {
if ((Get-Culture).NumberFormat.CurrencySymbol -eq "£") {$OtherCurrencySymbol = "$"} if ((Get-Culture).NumberFormat.CurrencySymbol -eq "£") {$OtherCurrencySymbol = "$"}