From f47887f7fe8690cb2af792b2719dedeccedebfc2 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Fri, 7 Sep 2018 13:07:40 +0100 Subject: [PATCH] Support for hash tables as definitions; more tests --- Export-Excel.ps1 | 21 +++++-- New-ConditionalText.ps1 | 3 + New-ExcelChart.ps1 | 2 +- PivotTable.ps1 | 11 +++- README.md | 3 +- __tests__/ExtraLongCmd.tests.ps1 | 62 +++++++++++++++++++ .../Set-Row_Set-Column-SetFormat.tests.ps1 | 11 +++- 7 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 __tests__/ExtraLongCmd.tests.ps1 diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 2b67bde..4f8d96f 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -910,9 +910,14 @@ } foreach ($chartDef in $ExcelChartDefinition) { - $params = @{} - $chartDef.PSObject.Properties | ForEach-Object {if ( $null -ne $_.value) {$params[$_.name] = $_.value}} - Add-ExcelChart -Worksheet $ws @params + if ($chartDef -is [System.Management.Automation.PSCustomObject]) { + $params = @{} + $chartDef.PSObject.Properties | ForEach-Object {if ( $null -ne $_.value) {$params[$_.name] = $_.value}} + Add-ExcelChart -Worksheet $ws @params + } + elseif ($chartDef -is [hashtable] -or $chartDef -is[System.Collections.Specialized.OrderedDictionary]) { + Add-ExcelChart -Worksheet $ws @chartDef + } } if ($Calculate) { @@ -951,10 +956,12 @@ Add-ExcelChart -Worksheet $ws @params } } + # It now doesn't matter if the conditional formating rules are passed in $conditionalText or $conditional format. # Just one with an alias for compatiblity it will break things for people who are using both at once foreach ($c in (@() + $ConditionalText + $ConditionalFormat) ) { try { + #we can take an object with a .ConditionalType property made by New-ConditionalText or with a .Formatter Property made by New-ConditionalFormattingIconSet or a hash table if ($c.ConditionalType) { $cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ; BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ; @@ -972,9 +979,14 @@ } Write-Verbose -Message "Added conditional formatting to range $($c.range)" } + elseif ($c -is [hashtable] -or $c -is[System.Collections.Specialized.OrderedDictionary]) { + if (-not $c.Range) {$c.Range = $ws.Dimension.Address } + Add-ConditionalFormatting -WorkSheet $ws @c + } } - catch {throw "Error applying confitional formatting to worksheet $_"} + catch {throw "Error applying conditional formatting to worksheet $_"} } + if ($CellStyleSB) { try { $TotalRows = $ws.Dimension.Rows @@ -984,7 +996,6 @@ catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorksheetName': $_"} } - if ($Password) { try { $ws.Protection.SetPassword($Password) diff --git a/New-ConditionalText.ps1 b/New-ConditionalText.ps1 index 60b2a4d..d97aae2 100644 --- a/New-ConditionalText.ps1 +++ b/New-ConditionalText.ps1 @@ -43,7 +43,9 @@ function New-ConditionalText { [cmdletbinding()] param( #[Parameter(Mandatory=$true)] + [Alias("ConditionValue")] $Text, + [Alias("ForeGroundColor")] [System.Drawing.Color]$ConditionalTextColor="DarkRed", [System.Drawing.Color]$BackgroundColor="LightPink", [String]$Range, @@ -60,6 +62,7 @@ function New-ConditionalText { "NextMonth", "ThisMonth", "LastMonth", "AboveAverage", "AboveOrEqualAverage", "BelowAverage", "BelowOrEqualAverage" )] + [Alias("RuleType")] $ConditionalType="ContainsText" ) diff --git a/New-ExcelChart.ps1 b/New-ExcelChart.ps1 index c66b82f..605ace3 100644 --- a/New-ExcelChart.ps1 +++ b/New-ExcelChart.ps1 @@ -286,7 +286,7 @@ function Add-ExcelChart { [OfficeOpenXml.ExcelWorksheet]$Worksheet, [Parameter(ParameterSetName='PivotTable',Mandatory=$true)] [OfficeOpenXml.Table.PivotTable.ExcelPivotTable]$PivotTable , - [String]$Title = "Chart Title", + [String]$Title, #$Header, Not used but referenced previously [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked", $XRange, diff --git a/PivotTable.ps1 b/PivotTable.ps1 index 2485e2a..4296f10 100644 --- a/PivotTable.ps1 +++ b/PivotTable.ps1 @@ -191,9 +191,14 @@ catch {Write-Warning -Message "Failed adding chart for pivotable '$pivotTableName': $_"} } elseif ($PivotChartDefinition -and -not $wsPivot.Drawings["Chart$pivotTableName"]) { - $params = @{PivotTable= $pivotTable } - $PivotChartDefinition.PSObject.Properties | ForEach-Object {if ( $null -ne $_.value) {$params[$_.name] = $_.value}} - Add-ExcelChart @params + if ($PivotChartDefinition -is [System.Management.Automation.PSCustomObject]) { + $params = @{PivotTable= $pivotTable } + $PivotChartDefinition.PSObject.Properties | ForEach-Object {if ( $null -ne $_.value) {$params[$_.name] = $_.value}} + Add-ExcelChart @params + } + elseif ($PivotChartDefinition -is [hashtable] -or $PivotChartDefinition -is[System.Collections.Specialized.OrderedDictionary]) { + Add-ExcelChart -PivotTable $pivotTable @PivotChartDefinition + } } if ($PassThru) {return $pivotTable} } diff --git a/README.md b/README.md index ff759a7..151c99a 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,9 @@ Install-Module ImportExcel - Moved logic for adding a named range out of Export-Excel and into a new function named `Add-ExcelName`, and logic for adding a table into a function named `Add-ExcelTable`; this is to make it easier to do these things independently of Export-Excel, but minimize duplication. The Add-ExcelTable command has extra parameters to toggle the options from table tools toolbar (show totals etc) and set options in the totals row. - Moved PivotTable Functions out of Export-Excel.PS1 into their own file and moved Add-ExcelChart out of Export-Excel.ps1 into New-ExcelChart.ps1 - Fixed issues where formatting could be reset when using Export-Excel to manipulate an existing sheet without appending data; this applied to number-formats and tables. -- `Add-PivotTable` has some new parameters -PassThru returns the pivot table (e.g. to allow names /sort orders of data series to be tweaked ) -Address parameter allows Pivot to be placed on an existing sheet; -PivotTableStyle allows a change from "Medium6", -PivotNumberFormat sets data cells. Chart creation has been moved out to Add-ExcelChart, and -PivotChartDefinition allows a defintion created with New-ExcelChartDefinition to be used with a PivotTable. This opens up all the things that Add-Excel chart can do without duplicating the parameters on Add-Pivot table and Export-Excel. Definition, TableStyle, Numberformat and ChartDefiniton can be used in `New-PivotTableDefinition` . +- `Add-PivotTable` has some new parameters -PassThru returns the pivot table (e.g. to allow names /sort orders of data series to be tweaked ) -Address parameter allows Pivot to be placed on an existing sheet; -PivotTableStyle allows a change from "Medium6", -PivotNumberFormat sets data cells. Chart creation has been moved out to Add-ExcelChart, and -PivotChartDefinition allows a defintion created with New-ExcelChartDefinition to be used with a PivotTable. This opens up all the things that Add-ExcelChart can do without duplicating the parameters on Add-Pivot table and Export-Excel. Definition, TableStyle, Numberformat and ChartDefiniton can be used in `New-PivotTableDefinition` . - `Add-ExcelChart` now supports -PassThru to return the chart for tweaking after creation; there is now a -PivotTable parameter to allow Add-PivotTable to call the code in Add-ExcelChart. And in `New-ExcelChartDefinition` Legend parameters (for size, bold & position ) are now supported +- ChartDefinition and conditional formatting parameters can now be hashtables - if it would will splat into Add-ExcelChart or Add-ConditionalFormatting, it should be acceptable as a definition. # What's new in Release 5.2 diff --git a/__tests__/ExtraLongCmd.tests.ps1 b/__tests__/ExtraLongCmd.tests.ps1 new file mode 100644 index 0000000..73b68f3 --- /dev/null +++ b/__tests__/ExtraLongCmd.tests.ps1 @@ -0,0 +1,62 @@ + +$path = "$Env:TEMP\test.xlsx" +remove-item -path $path -ErrorAction SilentlyContinue +ConvertFrom-Csv @" +Product, City, Gross, Net +Apple, London , 300, 250 +Orange, London , 400, 350 +Banana, London , 300, 200 +Orange, Paris, 600, 500 +Banana, Paris, 300, 200 +Apple, New York, 1200,700 +"@ | Export-Excel -Path $path -TableStyle Medium13 -tablename "RawData" -ConditionalFormat @{Range="C2:C7"; DataBarColor="Green"} -ExcelChartDefinition @{ChartType="Doughnut";XRange="A2:B7"; YRange="C2:C7"; width=800; } -PivotTableDefinition @{Sales=@{ + PivotRows="City"; PivotColumns="Product"; PivotData=@{Gross="Sum";Net="Sum"}; PivotNumberFormat="$#,##0.00"; PivotTotals="Both"; PivotTableSyle="Medium12"; Activate=$true + PivotChartDefinition=@{Title="Gross and net by city and product"; ChartType="ColumnClustered"; Column=6; Width=600; Height=360; YMajorUnit=500; YMinorUnit=100; YAxisNumberformat="$#,##0"; LegendPostion="Bottom"}}} + +$excel = Open-ExcelPackage $path +$ws1 = $excel.Workbook.Worksheets[1] +$ws2 = $excel.Workbook.Worksheets[2] +Describe "Creating workbook with a single line" { + Context "Data Page" { + It "Inserted the data and created the table " { + $ws1.Tables[0] | Should not beNullOrEmpty + $ws1.Tables[0].Address.Address | Should be "A1:D7" + $ws1.Tables[0].StyleName | Should be "TableStyleMedium13" + } + It "Applied conditional formatting " { + $ws1.ConditionalFormatting[0] | Should not beNullOrEmpty + $ws1.ConditionalFormatting[0].type.ToString() | Should be "DataBar" + $ws1.ConditionalFormatting[0].Color.G | Should beGreaterThan 100 + $ws1.ConditionalFormatting[0].Color.R | Should beLessThan 100 + $ws1.ConditionalFormatting[0].Address.Address | Should be "C2:C7" + } + It "Added the chart " { + $ws1.Drawings[0] | Should not beNullOrEmpty + $ws1.Drawings[0].ChartType.ToString() | Should be "DoughNut" + $ws1.Drawings[0].Series[0].Series | Should be "'Sheet1'!C2:C7" + } + } + Context "PivotTable" { + it "Created the PivotTable on a new page and made it active " { + $ws2 | Should not beNullOrEmpty + $ws2.PivotTables[0] | Should not beNullOrEmpty + $ws2.PivotTables[0].Fields.Count | Should be 4 + $ws2.PivotTables[0].DataFields[0].Format | Should be "$#,##0.00" + $ws2.PivotTables[0].RowFields[0].Name | Should be "City" + $ws2.PivotTables[0].ColumnFields[0].Name | Should be "Product" + $ws2.PivotTables[0].RowGrandTotals | Should be $true + $ws2.PivotTables[0].ColumGrandTotals | Should be $true #Epplus's mis-spelling of column not mine + $ws2.View.TabSelected | Should be $true + } + it "Created the Pivot Chart " { + $ws2.Drawings[0] | Should not beNullOrEmpty + $ws2.Drawings[0].ChartType.ToString() | Should be ColumnClustered + $ws2.Drawings[0].YAxis.MajorUnit | Should be 500 + $ws2.Drawings[0].YAxis.MinorUnit | Should be 100 + $ws2.Drawings[0].YAxis.Format | Should be "$#,##0" + $ws2.Drawings[0].Legend.Position.ToString() | Should be "Bottom" + } + + } + +} diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index d0b8f08..88fe735 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -37,8 +37,7 @@ Describe "Number format expansion and setting" { Context "Expand-NumberFormat function" { It "Expanded named number formats as expected " { $r = [regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol) - - Expand-NumberFormat 'Currency' | Should match "^[$r\(\)\[\]RED0#\?\-;,.]+$" + Expand-NumberFormat 'Currency' | Should match "^[$r\(\)\[\] RED0#\?\-;,.]+$" Expand-NumberFormat 'Number' | Should be "0.00" Expand-NumberFormat 'Percentage' | Should be "0.00%" Expand-NumberFormat 'Scientific' | Should be "0.00E+00" @@ -318,7 +317,9 @@ Describe "Table Formatting" { Remove-Item $path $excel = $data2 | Export-excel -path $path -WorksheetName Hardware -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru $ws = $excel.Workbook.Worksheets[1] + #test showfilter & TotalSettings $Table = Add-ExcelTable -PassThru -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -TotalSettings @{"Total"="Sum"} -ShowFirstColumn -ShowFilter:$false + #test expnading named number formats Set-Column -Worksheet $ws -Column 4 -NumberFormat 'Currency' Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency' $PtDef =New-PivotTableDefinition -PivotTableName Totals -PivotRows Product -PivotData @{"Total"="Sum"} -PivotNumberFormat Currency -PivotTotals None -PivotTableSyle Dark2 @@ -345,4 +346,8 @@ Describe "Table Formatting" { $ws2.PivotTables[0].StyleName | Should be "PivotStyleDark2" } } -} \ No newline at end of file +} + + + +