From c49b7b6db0aa2d77dc462ef6b0008a7d0c642cee Mon Sep 17 00:00:00 2001 From: jhoneill Date: Tue, 14 Aug 2018 15:40:52 +0100 Subject: [PATCH] Epplus is not setting chart axis position properly, so disabled for now --- Export-Excel.ps1 | 12 +++--- __tests__/Export-Excel.Tests.ps1 | 67 ++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index a9032c4..d723c29 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -635,10 +635,8 @@ #Otherwise the default will be unbolded. $ws.Cells[$Row, $StartColumn].Style.Font.Bold = $True } - - $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern - - if ($TitleBackgroundColor ) { + if ($TitleBackgroundColor ) { + $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern $ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) } $Row ++ ; $startRow ++ @@ -1413,7 +1411,8 @@ function Add-ExcelChart { if ($XAxisTitleBold) {$chart.XAxis.Title.Font.Bold = $true} if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize} } - if ($XAxisPosition) {$chart.ChartXml.chartSpace.chart.plotArea.catAx.axPos.val = $XAxisPosition.ToString().substring(0,1)} + if ($XAxisPosition) {Write-Warning "X Axis position is not being set propertly at the moment, parameter ignored" } + #$chart.ChartXml.chartSpace.chart.plotArea.catAx.axPos.val = $XAxisPosition.ToString().substring(0,1)} if ($XMajorUnit) {$chart.XAxis.MajorUnit = $XMajorUnit} if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit} if ($null -ne $XMinValue) {$chart.XAxis.MinValue = $XMinValue} @@ -1425,7 +1424,8 @@ function Add-ExcelChart { if ($YAxisTitleBold) {$chart.YAxis.Title.Font.Bold = $true} if ($YAxisTitleSize) {$chart.YAxis.Title.Font.Size = $YAxisTitleSize} } - if ($YAxisPosition) {$chart.ChartXml.chartSpace.chart.plotArea.valAx.axPos.val= $YAxisPosition.ToString().substring(0,1)} + if ($YAxisPosition) {Write-Warning "Y Axis position is not being set propertly at the moment, parameter ignored" } + #$chart.ChartXml.chartSpace.chart.plotArea.valAx.axPos.val= $YAxisPosition.ToString().substring(0,1)} if ($YMajorUnit) {$chart.YAxis.MajorUnit = $YMajorUnit} if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit} if ($null -ne $YMinValue){$chart.YAxis.MinValue = $YMinValue} diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index 31c28f5..2d35cf6 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -8,7 +8,7 @@ Describe ExportExcel { Context "#Example 1 # Creates and opens a file with the right number of rows and columns" { $path = "$env:TEMP\Test.xlsx" Remove-item -Path $path -ErrorAction SilentlyContinue - #Export maximum of 100 processes for speed; export all properties. + #Test with a maximum of 100 processes for speed; export all properties, then export smaller subsets. $processes = Get-Process | select-object -first 100 $propertyNames = $Processes[0].psobject.properties.name $rowcount = $Processes.Count @@ -68,7 +68,7 @@ Describe ExportExcel { $processes = Get-Process | Select-Object -First 100 $propertyNames = $Processes[0].psobject.properties.where( {$_.MemberType -eq 'Property'}).name $rowcount = $Processes.Count - #Test -NoAliasOrScriptPropeties option and Creating a range with a name which needs illegal chars removing - check this sends back a warning + #Test -NoAliasOrScriptPropeties option and creating a range with a name which needs illegal chars removing - check this sends back a warning $warnVar = $null $Processes | Export-Excel $path -NoAliasOrScriptPropeties -RangeName "No Spaces" -WarningVariable warnvar -WarningAction SilentlyContinue @@ -85,12 +85,12 @@ Describe ExportExcel { $ws.names["No_spaces"].End.Row | Should be ($rowcount + 1 ) # +1 for the header. $warnVar.Count | Should be 1 } - #This time use clearsheet instead of deleting the file + #This time use clearsheet instead of deleting the file test -Exclude properties, including wildcards. $Processes | Export-Excel $path -ClearSheet -NoAliasOrScriptPropeties -ExcludeProperty SafeHandle, threads, modules, MainModule, StartInfo, MachineName, MainWindow*, M*workingSet $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] - it "Created a new file with a further 5 properties excluded and cleared the old sheet " { + it "Created a new file with further properties excluded and cleared the old sheet " { $ws.Name | Should be "sheet1" $ws.Dimension.Columns | Should be ($propertyNames.Count - 10) $ws.Dimension.Rows | Should be ($rowcount + 1) # +1 for the header @@ -98,6 +98,7 @@ Describe ExportExcel { $propertyNames = $Processes[0].psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames Remove-item -Path $path -ErrorAction SilentlyContinue + #Test -DisplayPropertySet $Processes | Export-Excel $path -DisplayPropertySet $Excel = Open-ExcelPackage -Path $path @@ -148,7 +149,7 @@ Describe ExportExcel { else {$OtherCurrencySymbol = "£"} $path = "$env:TEMP\Test.xlsx" $warnVar = $null - #Check various types and number formats, and confirm that a property which is an object gets converted to a string with no warnings. + #Test correct export of different data types and number formats; test hyperlinks, test -NoNumberConversion test object is converted to a string with no warnings, test calcuation of formula Remove-item -Path $path -ErrorAction SilentlyContinue [PSCustOmobject][Ordered]@{ Date = Get-Date @@ -256,6 +257,7 @@ Describe ExportExcel { $path = "$env:TEMP\Test.xlsx" Remove-item -Path $path -ErrorAction SilentlyContinue + #Test -NoHeader & -NoNumberConversion [PSCustOmobject][Ordered]@{ Date = Get-Date Formula1 = '=SUM(F1:G1)' @@ -311,23 +313,25 @@ Describe ExportExcel { } Context "#Example 5 # Adding a single conditional format " { + #Test New-ConditionalText builds correctly $ct = New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor DarkRed -BackgroundColor LightPink it "Created a Conditional format description " { - $ct.BackgroundColor -is [System.Drawing.Color] | Should be $true + $ct.BackgroundColor -is [System.Drawing.Color] | Should be $true $ct.ConditionalTextColor -is [System.Drawing.Color] | Should be $true $ct.ConditionalType -in [enum]::GetNames( [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType] ) | - Should be $true + Should be $true } $path = "$env:TEMP\Test.xlsx" Remove-item -Path $path -ErrorAction SilentlyContinue + #Test -ConditionalText with a single conditional spec. Write-Output 489 668 299 777 860 151 119 497 234 788 | Export-Excel -Path $path -ConditionalText $ct it "Created a new file " { Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true } - #ToDo need to test applying conitional formatting to a pre-existing worksheet + #ToDo need to test applying conitional formatting to a pre-existing worksheet and removing = from formula $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] @@ -342,13 +346,11 @@ Describe ExportExcel { $cf.Type.ToString() | Should be $ct.ConditionalType #$cf.Style.Fill.BackgroundColor | Should be $ct.BackgroundColor # $cf.Style.Font.Color | Should be $ct.ConditionalTextColor - have to compare r.g.b - - } } Context "#Example 6 # Adding multiple conditional formats using short form syntax. " { - #this is a test of adding more than one conditional block and using the minimal syntax for new-ConditionalText = + #Test adding mutliple conditional blocks and using the minimal syntax for new-ConditionalText $path = "$env:TEMP\Test.xlsx" Remove-item -Path $path -ErrorAction SilentlyContinue @@ -396,6 +398,7 @@ Describe ExportExcel { } $Array = $Obj1, $Obj2, $Obj3 + #test Update-FirstObjectProperties $newarray = $Array | Update-FirstObjectProperties it "Outputs as many objects as it input " { $newarray.Count | Should be $Array.Count @@ -411,9 +414,9 @@ Describe ExportExcel { Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" { $path = "$env:TEMP\Test.xlsx" - #This time we are not deleting the XLSX file so this Should create a new, named, sheet. + #Test -passthru and -worksheetName creating a new, named, sheet in an existing file. $Excel = Get-Process | Select-Object -first 20 -Property Name, cpu, pm, handles, company | Export-Excel $path -WorkSheetname Processes -PassThru - #Testing -passthru and adding the Pivot as a second step. Want to save and re-open it ... + #Testing -Excel Pacakage and adding a Pivot-table as a second step. Want to save and re-open it ... Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -NoTotalsInPivot $Excel = Open-ExcelPackage $path @@ -435,7 +438,7 @@ Describe ExportExcel { $pt.DataFields[0].Field.Name | Should be "PM" $PTws.Drawings.Count | Should be 0 } - #using the already open sheet add the pivot chart + #test adding pivot chart using the already open sheet $warnvar = $null Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -ShowCategory -NoLegend -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path @@ -448,6 +451,7 @@ Describe ExportExcel { it "Generated a message on re-processing the Pivot table " { $warnVar | Should not beNullOrEmpty } + #Test appending data extends pivot chart (with a warning) . $warnVar = $null Get-Process | Select-Object -Last 20 -Property Name, cpu, pm, handles, company | Export-Excel $path -WorkSheetname Processes -Append -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path @@ -465,7 +469,7 @@ Describe ExportExcel { Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" { $path = "$env:TEMP\Test.xlsx" - + #Test the -CopySource and -Movexxxx parameters for Add-WorkSheet $Excel = Open-ExcelPackage $path #At this point Sheets Should be in the order Sheet1, Processes, ProcessesPivotTable $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Processes" -MoveToEnd # order now Sheet1, ProcessesPivotTable, Processes @@ -501,7 +505,7 @@ Describe ExportExcel { remove-item -Path $path -ErrorAction SilentlyContinue #Catch warning $warnVar = $null - #Test Append with no existing sheet. Test adding a named pivot table from command line parametesr and extending ranges when they're not specified explictly + #Test -Append with no existing sheet. Test adding a named pivot table from command line parameters and extending ranges when they're not specified explictly Get-Process | Select-Object -first 10 -Property Name, cpu, pm, handles, company | Export-Excel -StartRow 3 -StartColumn 3 -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -Append -PivotFilter Name -NoTotalsInPivot -RangeName procs -AutoFilter -AutoNameRange Get-Process | Select-Object -last 10 -Property Name, cpu, pm, handles, company | Export-Excel -StartRow 3 -StartColumn 3 -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -Append -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path @@ -547,6 +551,7 @@ Describe ExportExcel { Context " # Create and append explicit and auto table and range extension" { $path = "$env:TEMP\Test.xlsx" + #Test -Append automatically extends a table, even when it is not specified in the append command Get-Process | Select-Object -first 10 -Property Name, cpu, pm, handles, company | Export-Excel -Path $path -TableName ProcTab -AutoNameRange -WorkSheetname NoOffset -ClearSheet Get-Process | Select-Object -last 10 -Property Name, cpu, pm, handles, company | Export-Excel -Path $path -AutoNameRange -WorkSheetname NoOffset -Append $Excel = Open-ExcelPackage $path @@ -557,7 +562,8 @@ Describe ExportExcel { $dataWs.Names["CPU"].Rows | should be 20 $dataWs.Names["CPU"].Columns | should be 1 } - + + #Test extneding autofilter and range when explicitly specified in the append $excel = Get-Process | Select-Object -first 10 -Property Name, cpu, pm, handles, company | Export-Excel -ExcelPackage $excel -RangeName procs -AutoFilter -WorkSheetname NoOffset -ClearSheet -PassThru Get-Process | Select-Object -last 10 -Property Name, cpu, pm, handles, company | Export-Excel -ExcelPackage $excel -RangeName procs -AutoFilter -WorkSheetname NoOffset -Append $Excel = Open-ExcelPackage $path @@ -574,6 +580,7 @@ Describe ExportExcel { Context "#Example 11 # Create and append with title, inc ranges and Pivot table" { $path = "$env:TEMP\Test.xlsx" + #Test New-PivotTableDefinition builds definition using -Pivotfilter and -PivotTotals options. $ptDef = [ordered]@{} $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotTotals Columns -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend -PivotColumns CanPauseAndContinue $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -PivotTotalS Rows -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue @@ -590,6 +597,7 @@ Describe ExportExcel { Remove-Item -Path $path #Catch warning $warnvar = $null + #Test create two data pages; as part of adding the second give both their own pivot table, test -autosize switch Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor AliceBlue -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef $Excel = Open-ExcelPackage $path @@ -655,6 +663,7 @@ Describe ExportExcel { Context "#Example 13 # Formatting and another way to do a pivot. " { $path = "$env:TEMP\Test.xlsx" Remove-Item $path + #Test freezing top row/first column, adding formats and a pivot table - from Add-Pivot table not a specification variable - after the export $excel = Get-Process | Select-Object -Property Name, Company, Handles, CPU, PM, NPM, WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru $sheet = $excel.Workbook.Worksheets["Processes"] $sheet.Column(1) | Set-Format -Bold -AutoFit @@ -721,7 +730,8 @@ Describe ExportExcel { Context " # Chart from MultiSeries.ps1 in the Examples\charts Directory" { $path = "$env:TEMP\Test.xlsx" Remove-Item -Path $path -ErrorAction SilentlyContinue - $data = invoke-sum (Get-Process) Company Handles, PM, VirtualMemorySize + #Test Invoke-Sum + $data = Invoke-Sum (Get-Process) Company Handles, PM, VirtualMemorySize it "used Invoke-Sum to create a data set " { $data | Should not beNullOrEmpty $data.count | Should beGreaterThan 1 @@ -746,6 +756,7 @@ Describe ExportExcel { $c.ShowCategory | Should not be $true $c.ShowPercent | Should not be $true } + #Test creating a chart using -ExcelChartDefinition. $data | Export-Excel $path -AutoSize -TableName Processes -ExcelChartDefinition $c $excel = Open-ExcelPackage -Path $path $drawings = $excel.Workbook.Worksheets[1].drawings @@ -764,12 +775,16 @@ Describe ExportExcel { Context " # variation of plot.ps1 from Examples Directory using Add chart outside ExportExcel" { $path = "$env:TEMP\Test.xlsx" + #Test inserting a fomual $excel = 0..360 | ForEach-Object {[pscustomobject][ordered]@{x = $_; Sinx = "=Sin(Radians(x)) "}} | Export-Excel -AutoNameRange -Path $path -WorkSheetname SinX -ClearSheet -FreezeFirstColumn -PassThru + #Test-Add Excel Chart to existing data. Test add Conditional formatting with a formula Add-ExcelChart -Worksheet $excel.Workbook.Worksheets["Sinx"] -ChartType line -XRange "X" -YRange "Sinx" -SeriesHeader "Sin(x)" -Title "Graph of Sine X" -TitleBold -TitleSize 14 ` - -Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000"` -XAxisPosition Bottom ` - -YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 -YAxisPosition Left ` - -LegendSize 8 -legendBold -LegendPostion Bottom - $d = $excel.Workbook.Worksheets["Sinx"].Drawings[0] + -Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000" ` + -YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 ` + -LegendSize 8 -legendBold -LegendPostion Bottom + Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets["Sinx"] -Range "B2:B362" -RuleType LessThan -ConditionValue "=B1" -ForeGroundColor Red + $ws = $Excel.Workbook.Worksheets["Sinx"] + $d = $ws.Drawings[0] It "Controled the axes and title and legend of the chart " { $d.XAxis.MaxValue | Should be 361 $d.XAxis.MajorUnit | Should be 30 @@ -794,6 +809,9 @@ Describe ExportExcel { $d.ChartType.tostring() | Should be "line" $d.From.Column | Should be 2 } + It "Appplied conditional formatting to the data " { + $ws.ConditionalFormatting[0].Formula | Should be "B1" + } Close-ExcelPackage -ExcelPackage $excel -nosave } @@ -816,7 +834,7 @@ Describe ExportExcel { $ct.ConditionalType | Should be "ContainsText" $ct.Text | Should be "Microsoft" } - + #Test -ConditionalFormat & -ConditionalText Export-Excel -Path $path -ConditionalFormat $cf -ConditionalText $ct $excel = Open-ExcelPackage -Path $path $rows = $range -replace "^.*?(\d+)$", '$1' @@ -837,6 +855,7 @@ Describe ExportExcel { Context " # Awkward multiple tables" { $path = "$Env:TEMP\test.xlsx" + #Test creating 3 on overlapping tables on the same page. Create rightmost the left most then middle. remove-item -Path $path -ErrorAction SilentlyContinue $r = Get-ChildItem -path C:\WINDOWS\system32 -File @@ -861,7 +880,7 @@ Describe ExportExcel { $ws.Tables["FileSize"].StyleName | should be "TableStyleMedium2" } it "Created the ExtSize table in the right place with the right size " { - $ws.Tables["ExtSize"].Address.Address | should be "A2:B14" #tile, then 12 rows x 2 columns of data + $ws.Tables["ExtSize"].Address.Address | should be "A2:B14" #tile, then 12 rows x 2 columns of data } it "Created the ExtCount table in the right place with the right size " { $ws.Tables["ExtCount"].Address.Address | should be "D2:E12" #title, then 10 rows x 2 columns of data