Added -PivotTableName to export excel (see also Readme.md)

This commit is contained in:
jhoneill
2018-06-28 12:14:13 +01:00
parent a3693a7faf
commit 6c24db9593
3 changed files with 26 additions and 23 deletions

View File

@@ -2,7 +2,7 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $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} if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { Write-Warning -Message "You need to close Excel before running the tests." ; return}
Describe ExportExcel { Describe ExportExcel {
@@ -441,10 +441,10 @@ Describe ExportExcel {
it "Cloned 'Sheet1' to 'NewSheet' "{ it "Cloned 'Sheet1' to 'NewSheet' "{
$newWs = $excel.Workbook.Worksheets["NewSheet"] $newWs = $excel.Workbook.Worksheets["NewSheet"]
$newWs.Dimension.Address | should be ($excel.Workbook.Worksheets["Sheet1"].Dimension.Address) $newWs.Dimension.Address | should be ($excel.Workbook.Worksheets["Sheet1"].Dimension.Address)
$newWs.ConditionalFormatting.Count | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting.Count) $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].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" $path = "$env:TEMP\Test.xlsx"
#Catch warning #Catch warning
$warnVar = $null $warnVar = $null
#Test Append with no existing sheet. #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 -Path $path -WorkSheetname withOffset -append 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 -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar 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 $Excel = Open-ExcelPackage $path
$dataWs = $Excel.Workbook.Worksheets["withOffset"] $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 " { it "Created and appended to a sheet offset from the top left corner " {
$dataWs.Cells[1,1].Value | Should beNullOrEmpty $dataWs.Cells[1,1].Value | Should beNullOrEmpty
$dataWs.Cells[2,2].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" { Context "#Example 11 # Create and append with title, inc ranges and Pivot table" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
#Catch warning
$ptDef = [ordered]@{} $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 "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 "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue
it "Built a pivot definition using New-PivotTableDefinition " { it "Built a pivot definition using New-PivotTableDefinition " {
$ptDef.PT1.SourceWorkSheet | Should be 'Sheet1' $ptDef.PT1.SourceWorkSheet | Should be 'Sheet1'
@@ -493,9 +492,10 @@ Describe ExportExcel {
$ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D' $ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D'
} }
Remove-Item -Path $path Remove-Item -Path $path
#Catch warning
$warnvar = $null $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-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 $Excel = Open-ExcelPackage $path
$ws1 = $Excel.Workbook.Worksheets["Sheet1"] $ws1 = $Excel.Workbook.Worksheets["Sheet1"]
$ws2 = $Excel.Workbook.Worksheets["Sheet2"] $ws2 = $Excel.Workbook.Worksheets["Sheet2"]

View File

@@ -354,6 +354,7 @@
[Int]$TitleSize = 22, [Int]$TitleSize = 22,
[System.Drawing.Color]$TitleBackgroundColor, [System.Drawing.Color]$TitleBackgroundColor,
[Switch]$IncludePivotTable, [Switch]$IncludePivotTable,
[String]$PivotTableName,
[String[]]$PivotRows, [String[]]$PivotRows,
[String[]]$PivotColumns, [String[]]$PivotColumns,
$PivotData, $PivotData,
@@ -730,21 +731,22 @@
} }
if ($IncludePivotTable -or $IncludePivotChart) { if ($IncludePivotTable -or $IncludePivotChart) {
$params = @{ $params = @{
"PivotTableName" = ($WorkSheetname + 'PivotTable') ;
"SourceRange" = $dataRange "SourceRange" = $dataRange
} }
if ($PivotFilter) {$params.PivotFilter = $PivotFilter} if ($PivotTableName) {$params.PivotTableName = $PivotTableName}
if ($PivotRows) {$params.PivotRows = $PivotRows} else {$params.PivotTableName = $WorkSheetname + 'PivotTable'}
if ($PivotColumns) {$Params.PivotColumns = $PivotColumns} if ($PivotFilter) {$params.PivotFilter = $PivotFilter}
if ($PivotData) {$Params.PivotData = $PivotData} if ($PivotRows) {$params.PivotRows = $PivotRows}
if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true} if ($PivotColumns) {$Params.PivotColumns = $PivotColumns}
if ($PivotData) {$Params.PivotData = $PivotData}
if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true}
if ($PivotDataToColumn) {$params.PivotDataToColumn = $true} if ($PivotDataToColumn) {$params.PivotDataToColumn = $true}
if ($IncludePivotChart) { if ($IncludePivotChart) {
$params.IncludePivotChart = $true $params.IncludePivotChart = $true
$Params.ChartType = $ChartType $Params.ChartType = $ChartType
if ($ShowCategory) {$params.ShowCategory = $true} if ($ShowCategory) {$params.ShowCategory = $true}
if ($ShowPercent) {$params.ShowPercent = $true} if ($ShowPercent) {$params.ShowPercent = $true}
if ($NoLegend) {$params.NoLegend = $true} if ($NoLegend) {$params.NoLegend = $true}
} }
Add-PivotTable -ExcelPackage $pkg -SourceWorkSheet $ws @params Add-PivotTable -ExcelPackage $pkg -SourceWorkSheet $ws @params
} }

View File

@@ -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. - 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 - 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 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 - 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. - 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" - Re-ordered the choices in the switch and added an option to say "If it is numeric already post it as is"