From 10f670b4e660e9cfbba3e0d809d0badb270ae895 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Tue, 14 Aug 2018 16:35:19 +0100 Subject: [PATCH] Added timespan support to export Excel --- Export-Excel.ps1 | 8 +++++++- README.md | 5 +++-- __tests__/Export-Excel.Tests.ps1 | 12 +++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index d723c29..713b065 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -486,13 +486,19 @@ #The write-verbose commands have been commented out below - even if verbose is silenced they cause a significiant performance impact and if it's on they will cause a flood of messages. Switch ($CellValue) { { $_ -is [DateTime]} { - # Save a date with an international valid format + # Save a date with one of Excel's built in formats format $TargetCell.Value = $_ $TargetCell.Style.Numberformat.Format = 'm/d/yy h:mm' # This is not a custom format, but a preset recognized as date and localized. #Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as date" break } + { $_ -is [TimeSpan]} { + #Save a timespans with a built in format for elapsed hours, minutes and seconds + $TargetCell.Value = $_ + $TargetCell.Style.Numberformat.Format = '[h]:mm:ss' + break + } { $_ -is [System.ValueType]} { # Save numerics, setting format if need be. $TargetCell.Value = $_ diff --git a/README.md b/README.md index c7f09af..fb4d800 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,9 @@ Install-Module ImportExcel -scope CurrentUser Install-Module ImportExcel ``` # New to Aug 14th -- In Export-Excel improved the catch-all handler for insering values to cope better with nested objects 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 = +- 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 +- 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 -PivotTotals parameter where there was already -NoTotalsInPivot new one allows None, Both, Rows, Columns. (#415) - When appending Export-Excel only extended tables and ranges if they were explicitly specified. It now does it automatically. - Compare and Merge worksheet originally had a problem with > 26 columns, I fixed merge turns out I hadn't fixed compare ... I have now diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index 2d35cf6..e320f37 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -142,7 +142,7 @@ Describe ExportExcel { $ws.cells[1, 1].Value | Should be -1 } } - + Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" { if ((Get-Culture).NumberFormat.CurrencySymbol -eq "£") {$OtherCurrencySymbol = "$"} @@ -177,6 +177,7 @@ Describe ExportExcel { Link3 = "xl://internal/sheet1!A1" Link4 = "xl://internal/sheet1!C5" Process = (Get-Process -Id $PID) + TimeSpan = [datetime]::Now.Subtract([datetime]::Today) } | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -Path $path -Calculate -WarningVariable $warnVar it "Created a new file " { Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true @@ -189,7 +190,7 @@ Describe ExportExcel { $ws = $Excel.Workbook.Worksheets[1] 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 25 + $ws.Dimension.Columns | Should be 26 $ws.Dimension.Rows | Should be 2 } it "Set a date in Cell A2 " { @@ -251,8 +252,13 @@ Describe ExportExcel { it "Converted a nested object to a string (Y2) " { $ws.Cells[2, 25].Value | should match '^System\.Diagnostics\.Process\s+\(.*\)$' } + it "Processed a timespan object (Z2) " { + $ws.cells[2, 26].Value.ToOADate() | should beGreaterThan 0 + $ws.cells[2, 26].Value.ToOADate() | should beLessThan 1 + $ws.cells[2, 26].Style.Numberformat.Format | should be '[h]:mm:ss' + } } - + Context "# # Setting cells for different data types with -noHeader" { $path = "$env:TEMP\Test.xlsx"