diff --git a/Export-Excel.Tests.ps1 b/Export-Excel.Tests.ps1 index c5a77db..1a19d6b 100644 --- a/Export-Excel.Tests.ps1 +++ b/Export-Excel.Tests.ps1 @@ -2,7 +2,7 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path -Import-Module $here -Force +Import-Module $here -Force -Verbose if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { Write-Warning -Message "You need to close Excel before running the tests." ; return} Describe ExportExcel { @@ -441,10 +441,10 @@ Describe ExportExcel { it "Cloned 'Sheet1' to 'NewSheet' "{ $newWs = $excel.Workbook.Worksheets["NewSheet"] - $newWs.Dimension.Address | should be ($excel.Workbook.Worksheets["Sheet1"].Dimension.Address) - $newWs.ConditionalFormatting.Count | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting.Count) + $newWs.Dimension.Address | should be ($excel.Workbook.Worksheets["Sheet1"].Dimension.Address) + $newWs.ConditionalFormatting.Count | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting.Count) $newWs.ConditionalFormatting[0].Address.Address | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Address.Address) - $newWs.ConditionalFormatting[0].Formula | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula) + $newWs.ConditionalFormatting[0].Formula | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula) } } @@ -453,12 +453,12 @@ Describe ExportExcel { $path = "$env:TEMP\Test.xlsx" #Catch warning $warnVar = $null - #Test Append with no existing sheet. - get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -Path $path -WorkSheetname withOffset -append - get-process | Select-Object -last 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar + #Test Append with no existing sheet. Test adding a named pivot table from a command line parameter + get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append + get-process | Select-Object -last 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path $dataWs = $Excel.Workbook.Worksheets["withOffset"] - $pt = $Excel.Workbook.Worksheets["withOffsetPivotTable"].PivotTables[0] + $pt = $Excel.Workbook.Worksheets["PTOffset"].PivotTables[0] it "Created and appended to a sheet offset from the top left corner " { $dataWs.Cells[1,1].Value | Should beNullOrEmpty $dataWs.Cells[2,2].Value | Should beNullOrEmpty @@ -479,10 +479,9 @@ Describe ExportExcel { Context "#Example 11 # Create and append with title, inc ranges and Pivot table" { $path = "$env:TEMP\Test.xlsx" - #Catch warning $ptDef = [ordered]@{} - $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend - $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue + $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend + $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue it "Built a pivot definition using New-PivotTableDefinition " { $ptDef.PT1.SourceWorkSheet | Should be 'Sheet1' @@ -493,9 +492,10 @@ Describe ExportExcel { $ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D' } Remove-Item -Path $path + #Catch warning $warnvar = $null Get-Service | Select-Object -Property Status, Name, DisplayName, StartType | 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 + 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 $ws1 = $Excel.Workbook.Worksheets["Sheet1"] $ws2 = $Excel.Workbook.Worksheets["Sheet2"] diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index c6dddfe..913d239 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -354,6 +354,7 @@ [Int]$TitleSize = 22, [System.Drawing.Color]$TitleBackgroundColor, [Switch]$IncludePivotTable, + [String]$PivotTableName, [String[]]$PivotRows, [String[]]$PivotColumns, $PivotData, @@ -730,21 +731,22 @@ } if ($IncludePivotTable -or $IncludePivotChart) { $params = @{ - "PivotTableName" = ($WorkSheetname + 'PivotTable') ; "SourceRange" = $dataRange } - if ($PivotFilter) {$params.PivotFilter = $PivotFilter} - if ($PivotRows) {$params.PivotRows = $PivotRows} - if ($PivotColumns) {$Params.PivotColumns = $PivotColumns} - if ($PivotData) {$Params.PivotData = $PivotData} - if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true} + if ($PivotTableName) {$params.PivotTableName = $PivotTableName} + else {$params.PivotTableName = $WorkSheetname + 'PivotTable'} + if ($PivotFilter) {$params.PivotFilter = $PivotFilter} + if ($PivotRows) {$params.PivotRows = $PivotRows} + if ($PivotColumns) {$Params.PivotColumns = $PivotColumns} + if ($PivotData) {$Params.PivotData = $PivotData} + if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true} if ($PivotDataToColumn) {$params.PivotDataToColumn = $true} if ($IncludePivotChart) { $params.IncludePivotChart = $true - $Params.ChartType = $ChartType - if ($ShowCategory) {$params.ShowCategory = $true} - if ($ShowPercent) {$params.ShowPercent = $true} - if ($NoLegend) {$params.NoLegend = $true} + $Params.ChartType = $ChartType + if ($ShowCategory) {$params.ShowCategory = $true} + if ($ShowPercent) {$params.ShowPercent = $true} + if ($NoLegend) {$params.NoLegend = $true} } Add-PivotTable -ExcelPackage $pkg -SourceWorkSheet $ws @params } diff --git a/README.md b/README.md index 4eb9bc3..6e3663a 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi - Fixed a bug where Append can overwrite the last rows of data if the initial export had blank rows at the top of the sheet. - Removed the need to specify a fill type when specifying a title background color - Added MoveToStart, MoveToEnd, MoveBefore and MoveAfter Parameters - these go straight through to Add worksheet - - Added "NoScriptOrAliasProperties" "DisplayPropertySet" switches (names subject to change) - combined with ExcludeProperty these are a quick way to reduce the data exported (and speed things up) + - Added "NoScriptOrAliasProperties" "DisplayPropertySet" switches (names subject to change) - combined with ExcludeProperty these are a quick way to reduce the data exported (and speed things up) + - Added PivotTableName Switch (in line with 5.0.1 release) - Add-CellValue now understands URI item properties. If a property is of type URI it is created as a hyperlink to speed up Add-CellValue - Commented out the write verbose statements even if verbose is silenced they cause a significiant performance impact and if it's on they will cause a flood of messages. - Re-ordered the choices in the switch and added an option to say "If it is numeric already post it as is"