From 65b1f79d53d7d8e39aeb499b045e02af43ba1fd5 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Sun, 9 Sep 2018 15:10:05 +0100 Subject: [PATCH] Range handling for Add-Conditional format and range tests --- AddConditionalFormatting.ps1 | 84 +++++++++-------------- Export-Excel.ps1 | 2 +- README.md | 6 +- __tests__/Export-Excel.Tests.ps1 | 14 +++- __tests__/RangePassing.ps1 | 114 +++++++++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 59 deletions(-) create mode 100644 __tests__/RangePassing.ps1 diff --git a/AddConditionalFormatting.ps1 b/AddConditionalFormatting.ps1 index e483583..d29119d 100644 --- a/AddConditionalFormatting.ps1 +++ b/AddConditionalFormatting.ps1 @@ -42,106 +42,70 @@ This rule is move to first in the priority list, and where cells have a value of 1, no other rules will be processed. #> Param ( - #The worksheet where the format is to be applied - [Parameter(Mandatory = $true, ParameterSetName = "NamedRule")] - [Parameter(Mandatory = $true, ParameterSetName = "DataBar")] - [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FourIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSet")] - [OfficeOpenXml.ExcelWorksheet]$WorkSheet , - #The area of the worksheet where the format is to be applied - [Parameter(Mandatory = $true, ParameterSetName = "NamedRule")] - [Parameter(Mandatory = $true, ParameterSetName = "DataBar")] - [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FourIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSet")] - $Range , #A block of cells to format - you can use a named range with -Address $ws.names[1] or $ws.cells["RangeName"] - [Parameter(Mandatory = $true, ParameterSetName = "NamedRuleAddress")] - [Parameter(Mandatory = $true, ParameterSetName = "DataBarAddress")] - [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSetAddress")] - [Parameter(Mandatory = $true, ParameterSetName = "FourIconSetAddress")] - [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSetAddress")] + [Parameter(Mandatory = $true, Position = 0)] + [Alias("Range")] $Address , + #The worksheet where the format is to be applied + [OfficeOpenXml.ExcelWorksheet]$WorkSheet , #One of the standard named rules - Top / Bottom / Less than / Greater than / Contains etc. - [Parameter(Mandatory = $true, ParameterSetName = "NamedRule", Position = 3)] - [Parameter(Mandatory = $true, ParameterSetName = "NamedRuleAddress", Position = 3)] + [Parameter(Mandatory = $true, ParameterSetName = "NamedRule", Position = 1)] [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType]$RuleType , #Text colour for matching objects [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [Alias("ForeGroundColour")] [System.Drawing.Color]$ForeGroundColor, #colour for databar type charts [Parameter(Mandatory = $true, ParameterSetName = "DataBar")] - [Parameter(Mandatory = $true, ParameterSetName = "DataBarAddress")] [Alias("DataBarColour")] [System.Drawing.Color]$DataBarColor, #One of the three-icon set types (e.g. Traffic Lights) [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSetAddress")] [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting3IconsSetType]$ThreeIconsSet, #A four-icon set name [Parameter(Mandatory = $true, ParameterSetName = "FourIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FourIconSetAddress")] [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting4IconsSetType]$FourIconsSet, #A five-icon set name [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSet")] - [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSetAddress")] [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting5IconsSetType]$FiveIconsSet, #Use the icon set in reverse order, or reverse the orders of Two- & Three-Color Scales [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [Parameter(ParameterSetName = "ThreeIconSet")] - [Parameter(ParameterSetName = "ThreeIconSetAddress")] [Parameter(ParameterSetName = "FourIconSet")] - [Parameter(ParameterSetName = "FourIconSetAddress")] [Parameter(ParameterSetName = "FiveIconSet")] - [Parameter(ParameterSetName = "FiveIconSetAddress")] [switch]$Reverse, #A value for the condition (e.g. 2000 if the test is 'lessthan 2000' ; Formulas should begin with "=" ) [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] $ConditionValue, #A second value for the conditions like "between x and Y" [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] $ConditionValue2, #Background colour for matching items [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [System.Drawing.Color]$BackgroundColor, #Background pattern for matching items [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern = [OfficeOpenXml.Style.ExcelFillStyle]::None , #Secondary colour when a background pattern requires it [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [System.Drawing.Color]$PatternColor, #Sets the numeric format for matching items [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] $NumberFormat, #Put matching items in bold face [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [switch]$Bold, #Put matching items in italic [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [switch]$Italic, #Underline matching items [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [switch]$Underline, #Strikethrough text of matching items [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [switch]$StrikeThru, #Prevent the processing of subsequent rules [Parameter(ParameterSetName = "NamedRule")] - [Parameter(ParameterSetName = "NamedRuleAddress")] [switch]$StopIfTrue, #Set the sequence for rule processong [int]$Priority, @@ -150,22 +114,38 @@ ) #Allow conditional formatting to work like Set-ExcelRange (with single ADDRESS parameter), split it to get worksheet and range of cells. - If ($Address -and -not $WorkSheet -and -not $Range) { + If ($Address -is [OfficeOpenXml.Table.ExcelTable]) { + $WorkSheet = $Address.Address.Worksheet + $Address = $Address.Address.Address + } + elseif ($Address.Address -and $Address.Worksheet -and -not $WorkSheet) { #Address is a rangebase or similar $WorkSheet = $Address.Worksheet[0] - $Range = $Address.Address + $Address = $Address.Address } - elseif ($Range -and $WorkSheet -and $WorkSheet.Names[$Range] ) { - $Range = $WorkSheet.Names[$Range].Address + elseif ($Address -is [String] -and $WorkSheet -and $WorkSheet.Names[$Address] ) { #Address is the name of a named range. + $Address = $WorkSheet.Names[$Address].Address } - if ($Range -match "!") {$Range = $Range -replace '^.*!',''} - + if (($Address -is [OfficeOpenXml.ExcelRow] -and -not $WorkSheet) -or + ($Address -is [OfficeOpenXml.ExcelColumn] -and -not $WorkSheet) ){ #EPPLUs Can't get the worksheet object from a row or column object, so bail if that was tried + Write-Warning -Message "Add-ConditionalFormatting does not support Row or Column objects as an address; use a worksheet and/or specify 'R:R' or 'C:C' instead. "; return + } + elseif ($Address -is [OfficeOpenXml.ExcelRow]) { #But if we have a column or row object and a worksheet (I don't know *why*) turn them into a string for the range + $Address = "$($Address.Row):$($Address.Row)" + } + elseif ($Address -is [OfficeOpenXml.ExcelColumn]) { + $Address = [OfficeOpenXml.ExcelAddress]::new(1,$address.ColumnMin,1,$address.ColumnMax).Address -replace '1','' + if ($Address -notmatch ':') {$Address = "$Address`:$Address"} + } + if ( $Address -is [string] -and $Address -match "!") {$Address = $Address -replace '^.*!',''} + #By this point we should have a worksheet object whose ConditionalFormatting collection we will add to. If not, bail. + if (-not $worksheet -or $WorkSheet -isnot [OfficeOpenXml.ExcelWorksheet]) {write-warning "You need to provide a worksheet object." ; return} #region create a rule of the right type if ($RuleType -match 'IconSet$') {Write-warning -Message "You cannot configure a IconSet rule in this way; please use -$RuleType ." ; return} - if ($PSBoundParameters.ContainsKey("ThreeIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Range , $ThreeIconsSet)} - elseif ($PSBoundParameters.ContainsKey("FourIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Range , $FourIconsSet )} - elseif ($PSBoundParameters.ContainsKey("FiveIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Range , $FiveIconsSet )} - elseif ($PSBoundParameters.ContainsKey("DataBarColor" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Range , $DataBarColor )} - else {$rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Range ) } + if ($PSBoundParameters.ContainsKey("ThreeIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Address , $ThreeIconsSet)} + elseif ($PSBoundParameters.ContainsKey("FourIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Address , $FourIconsSet )} + elseif ($PSBoundParameters.ContainsKey("FiveIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Address , $FiveIconsSet )} + elseif ($PSBoundParameters.ContainsKey("DataBarColor" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Address , $DataBarColor )} + else {$rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Address ) } if ($Reverse) { if ($rule.type -match 'IconSet$' ) {$rule.reverse = $true} elseif ($rule.type -match 'ColorScale$') {$temp =$rule.LowValue.Color ; $rule.LowValue.Color = $rule.HighValue.Color; $rule.HighValue.Color = $temp} diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index f4c1427..2bd5767 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -1147,7 +1147,7 @@ function Add-WorkSheet { } #endregion if ($Activate) {Select-Worksheet -ExcelWorksheet $ws } - if (-not (Get-Member -InputObject $ExcelPackage -Name $ws.Name)) { + if ($ExcelPackage -and -not (Get-Member -InputObject $ExcelPackage -Name $ws.Name)) { $sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $ws.name)) Add-Member -InputObject $ExcelPackage -MemberType ScriptProperty -Name $ws.name -Value $sb } diff --git a/README.md b/README.md index 0e873f2..8dd3480 100644 --- a/README.md +++ b/README.md @@ -51,11 +51,11 @@ Install-Module ImportExcel -scope CurrentUser ```PowerShell Install-Module ImportExcel ``` -- Lots of help improvements and some tidying up of example +- Lots of help improvements and some tidying up of examples +- Open-Excel Package and Add-Worksheet now add worksheets as script properties so if you do $Excel = open-excelpackage -path test.xlsx ; $excel.sheet1 ; it will return the sheet named sheet 1 Excel.sheetName is a script property which returns $this.workbook.worksheets["Sheetname"] - Renamed Set-Column to Set-ExcelColum, Set-Row to Set-ExcelRow, and Set-Format, to Set-ExcelRange. Added aliases so the old names still work - Set-ExcelRange (or set-Format) used "Address" and "Range" incorrectly. There is now a single parameter "Range", with an alias of "Address" and the code beneath ensures that the function accepts a string specifying cells ("A1:Z10") or a range name; and worksheet. OR A named range, Or Table, or a tables's address, or part of the worksheet.cells collection. -- Open-Excel Package and Add-Worksheet now add worksheets as script properties so if you do $Excel = open-excelpackage -path test.xlsx ; $excel.sheet1 ; it will return the sheet named sheet 1 Excel.sheetName is a script property which returns $this.workbook.worksheets["Sheetname"] -- `Add-ConditionalFormatting`: Now wraps those strings in quotes when needed (for = <= >= string needs to be in double quotes). Parameter intellisense has been improved. There are new parameters: -StopIfTrue and -Priority and support for using the -Reverse parameter with Color-scale rules. Booleans in the sheet are now supported as the value for a condition. Also brought the two different kinds of condition together inside export-excel, and fixed a bug where named-ranges didn't work in some places. In `New-ConditionalText`, more types of conditional format are supported, and -ConditionalTextColor now has an argument completer +- `Add-ConditionalFormatting`: Used "address" correctly, and it will accept ranges in the address parameter (range is now an alias for address). It now wraps conditional value strings in quotes when needed (for = <= >= string needs to be in double quotes). Parameter intellisense has been improved. There are new parameters: -StopIfTrue and -Priority and support for using the -Reverse parameter with Color-scale rules. Booleans in the sheet are now supported as the value for a condition. Also brought the two different kinds of condition together inside export-excel, and fixed a bug where named-ranges didn't work in some places. In `New-ConditionalText`, more types of conditional format are supported, and -ConditionalTextColor now has an argument completer - Improved handling of hyperlinks in `Export-Excel`. - `Export-Excel` has better checking of Table and PivotTable names (for uniqueness) and a new test in quick charts that there is suitable data for charting. It also accepts hash tables for chart, pivot table and conditional formatting parameters which are splatted into the functions which add these. - 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. diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index acfb32b..3d51996 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -19,7 +19,7 @@ Describe ExportExcel { } # it "Started Excel to display the file " { - # Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should not benullorempty + # Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should not beNullOrEmpty # } #Start-Sleep -Seconds 5 ; @@ -28,11 +28,16 @@ Describe ExportExcel { #TODO Need to test opening pre-existing file with no -create switch (and graceful failure when file does not exist) somewhere else $Excel = Open-ExcelPackage -Path $path -KillExcel it "Killed Excel when Open-Excelpackage was told to " { - Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should benullorempty + Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should beNullOrEmpty } - it "Created 1 worksheet " { + it "Created 1 worksheet, named 'Sheet1' " { $Excel.Workbook.Worksheets.count | Should be 1 + $Excel.Workbook.Worksheets["Sheet1"] | Should not beNullOrEmpty + } + + it "Added a 'Sheet1' property to the Package object " { + $Excel.Sheet1 | Should not beNullOrEmpty } $ws = $Excel.Workbook.Worksheets[1] @@ -453,6 +458,7 @@ Describe ExportExcel { $PTws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] $wCount = $Excel.Workbook.Worksheets.Count it "Added the named sheet and pivot table to the workbook " { + $excel.ProcessesPivotTable | Should not beNullOrEmpty $PTws | Should not beNullOrEmpty $PTws.PivotTables.Count | Should be 1 $Excel.Workbook.Worksheets["Processes"] | Should not beNullOrEmpty @@ -703,8 +709,10 @@ Describe ExportExcel { Set-ExcelRange -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold Set-ExcelRange -Address $sheet.Row(1) -Bold -HorizontalAlignment Center Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red + #test Add-ConditionalFormatting -passthru and using a range (and no worksheet) $rule = Add-ConditionalFormatting -passthru -Address $sheet.cells["C:C"] -RuleType TopPercent -ConditionValue 20 -Bold -StrikeThru Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red -Bold -Italic -Underline -BackgroundColor Beige -BackgroundPattern LightUp -PatternColor Gray + #Test Set-ExcelRange with a column foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit } Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet 1 -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend Close-ExcelPackage $excel diff --git a/__tests__/RangePassing.ps1 b/__tests__/RangePassing.ps1 new file mode 100644 index 0000000..d71e1b8 --- /dev/null +++ b/__tests__/RangePassing.ps1 @@ -0,0 +1,114 @@ +$path = "$env:temp\test.xlsx" +describe "Consistent passing of ranges." { + Context "Conditional Formatting" { + Remove-Item -path $path -ErrorAction SilentlyContinue + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -AutoNameRange -Title "Services on $Env:COMPUTERNAME" + it "accepts named ranges, cells['name'], worksheet + Name, worksheet + column " { + {Add-ConditionalFormatting $excel.Services.Names["Status"] -StrikeThru -RuleType ContainsText -ConditionValue "Stopped" } | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 1 + {Add-ConditionalFormatting $excel.Services.Cells["Name"] -Italic -RuleType ContainsText -ConditionValue "SVC" } | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 2 + $warnvar = $null + Add-ConditionalFormatting $excel.Services.Column(3) ` + -underline -RuleType ContainsText -ConditionValue "Windows" -WarningVariable warnvar -WarningAction SilentlyContinue + $warnvar | should not beNullOrEmpty + $excel.Services.ConditionalFormatting.Count | Should be 2 + $warnvar = $null + Add-ConditionalFormatting $excel.Services.Column(3) -WorkSheet $excel.Services` + -underline -RuleType ContainsText -ConditionValue "Windows" -WarningVariable warnvar -WarningAction SilentlyContinue + $warnvar | should beNullOrEmpty + $excel.Services.ConditionalFormatting.Count | Should be 3 + {Add-ConditionalFormatting "Status" -WorkSheet $excel.Services ` + -ForeGroundColor Green -RuleType ContainsText -ConditionValue "Running"} | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 4 + } + Close-ExcelPackage -NoSave $excel + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" + it "accepts table, table.Address and worksheet + 'C:C' " { + {Add-ConditionalFormatting $excel.Services.Tables[0] ` + -Italic -RuleType ContainsText -ConditionValue "Svc" } | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 1 + {Add-ConditionalFormatting $excel.Services.Tables["ServiceTable"].Address ` + -Bold -RuleType ContainsText -ConditionValue "windows" } | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 2 + {Add-ConditionalFormatting -WorkSheet $excel.Services -Address "a:a" ` + -RuleType ContainsText -ConditionValue "stopped" -ForeGroundColor Red } | Should not throw + $excel.Services.ConditionalFormatting.Count | Should be 3 + } + Close-ExcelPackage -NoSave $excel + } + + Context "Formating (Set-ExcelRange or its alias set-Format) " { + it "accepts Named Range, cells['Name'], cells['A1:Z9'], row, Worksheet + 'A1:Z9'" { + $excel = Get-Service | Export-Excel -Path test2.xlsx -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -RangeName servicerange -Title "Services on $Env:COMPUTERNAME" + {set-format $excel.Services.Names["serviceRange"] -Bold } | Should Not Throw + $excel.Services.cells["B2"].Style.Font.Bold | Should be $true + {Set-ExcelRange -Range $excel.Services.Cells["serviceRange"] -italic:$true } | Should not throw + $excel.Services.cells["C3"].Style.Font.Italic | Should be $true + {set-format $excel.Services.Row(4) -underline -Bold:$false } | Should not throw + $excel.Services.cells["A4"].Style.Font.UnderLine | Should be $true + $excel.Services.cells["A4"].Style.Font.Bold | Should not be $true + {Set-ExcelRange $excel.Services.Cells["A3:B3"] -StrikeThru } | Should not throw + $excel.Services.cells["B3"].Style.Font.Strike | Should be $true + {Set-ExcelRange -WorkSheet $excel.Services -Range "A5:B6" -FontSize 8 } | Should not throw + $excel.Services.cells["A5"].Style.Font.Size | Should be 8 + } + Close-ExcelPackage -NoSave $excel + it "Accepts Table, Table.Address , worksheet + Name, Column," { + $excel = Get-Service | Export-Excel -Path test2.xlsx -WorksheetName Services -PassThru -AutoNameRange -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" + {set-ExcelRange $excel.Services.Tables[0] -Italic } | Should not throw + $excel.Services.cells["C3"].Style.Font.Italic | Should be $true + {set-format $excel.Services.Tables["ServiceTable"].Address -Underline } | Should not throw + $excel.Services.cells["C3"].Style.Font.UnderLine | Should be $true + {Set-ExcelRange -WorkSheet $excel.Services -Range "Name" -Bold } | Should not throw + $excel.Services.cells["B4"].Style.Font.Bold | Should be $true + {$excel.Services.Column(3) | Set-ExcelRange -FontColor red } | Should not throw + $excel.Services.cells["C4"].Style.Font.Color.Rgb | Should be "FFFF0000" + } + Close-ExcelPackage -NoSave $excel + } + + Context "PivotTables" { + it "Accepts Named range, .Cells['Name'], name&Worksheet, cells['A1:Z9'], worksheet&'A1:Z9' "{ + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -RangeName servicerange -Title "Services on $Env:COMPUTERNAME" + $ws = $excel.Workbook.Worksheets[1] #can get a worksheet by name or index - starting at 1 + $end = $ws.Dimension.End.Address + #can get a named ranged by name or index - starting at zero + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt0 -SourceRange $ws.Names[0]` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt0"] | Should not beNullOrEmpty + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.Names["servicerange"]` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt1"] | Should not beNullOrEmpty + #Can specify the range for a pivot as NamedRange or Table or TableAddress or Worksheet + "A1:Z10" or worksheet + RangeName, or worksheet.cells["A1:Z10"] or worksheet.cells["RangeName"] + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange "servicerange" -SourceWorkSheet $ws ` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt2"] | Should not beNullOrEmpty + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt3 -SourceRange $ws.cells["servicerange"]` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt3"] | Should not beNullOrEmpty + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt4 -SourceRange $ws.cells["A2:$end"]` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt4"] | Should not beNullOrEmpty + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt5 -SourceRange "A2:$end" -SourceWorkSheet $ws ` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt5"] | Should not beNullOrEmpty + Close-ExcelPackage -NoSave $excel + } + it "Accepts Table, Table.Addres " { + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" + $ws = $excel.Workbook.Worksheets["Services"] #can get a worksheet by name or index - starting at 1 + #Can get a table by name or -stating at zero. Can specify the range for a pivot as or Table or TableAddress + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt1 -SourceRange $ws.tables["servicetable"]` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt1"] | Should not beNullOrEmpty + {Add-PivotTable -ExcelPackage $excel -PivotTableName pt2 -SourceRange $ws.tables[0].Address ` + -PivotRows Status -PivotData Name } | Should not throw + $excel.Workbook.Worksheets["pt2"] | Should not beNullOrEmpty + Close-ExcelPackage -NoSave $excel + } + + + + } +} \ No newline at end of file