Experiment multiple pivot tables

This commit is contained in:
dfinke
2017-10-11 19:23:45 -04:00
parent 85151f8375
commit f5acf88a17
2 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
ipmo .\ImportExcel.psd1 -Force
$file = "c:\temp\testPT.xlsx"
rm $file -ErrorAction Ignore
$pt=[ordered]@{}
$pt.PT1=@{
PivotRows = "Status"
PivotData= @{'Status'='count'}
IncludePivotChart=$true
}
$pt.PT2=@{
PivotRows = "StartType"
PivotData= @{'StartType'='count'}
IncludePivotChart=$true
}
$data = gsv | select status, Name, displayName, starttype
$data | Export-Excel -Path $file -Show -PivotTable $pt -AutoSize

View File

@@ -211,6 +211,7 @@ Function Export-Excel {
[Switch]$PivotDataToColumn,
[String]$Password,
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = 'Pie',
[Hashtable]$PivotTableDefinition,
[Switch]$IncludePivotTable,
[Switch]$IncludePivotChart,
[Switch]$NoLegend,
@@ -532,6 +533,64 @@ Function Export-Excel {
$tbl.TableStyle = $TableStyle
}
if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$targetName = $item.Key
$pivotTableName = $targetName + 'PivotTable'
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
$pivotTableDataName = $targetName + 'PivotTableData'
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $ws.Cells[$dataRange], $pivotTableDataName)
switch ($item.Value.Keys) {
"PivotRows" {
foreach ($Row in $item.Value.PivotRows) {
$null = $pivotTable.RowFields.Add($pivotTable.Fields[$Row])
}
}
"PivotColumns" {
foreach ($Column in $item.Value.PivotColumns) {
$null = $pivotTable.ColumnFields.Add($pivotTable.Fields[$Column])
}
}
"PivotData" {
$pivotData = $item.Value.PivotData
if ($PivotData -is [HashTable] -or $PivotData -is [System.Collections.Specialized.OrderedDictionary]) {
$PivotData.Keys | ForEach-Object {
$df = $pivotTable.DataFields.Add($pivotTable.Fields[$_])
$df.Function = $PivotData.$_
}
}
else {
foreach ($Item in $PivotData) {
$df = $pivotTable.DataFields.Add($pivotTable.Fields[$Item])
$df.Function = 'Count'
}
}
if ($PivotDataToColumn) {
$pivotTable.DataOnRows = $false
}
}
"IncludePivotChart" {
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
#$chart.DataLabel.ShowCategory = $ShowCategory
#$chart.DataLabel.ShowPercent = $ShowPercent
#if ($NoLegend) {
# $chart.Legend.Remove()
#}
$chart.SetPosition(1, 0, 6, 0)
$chart.SetSize(600, 400)
}
}
}
}
if ($IncludePivotTable) {
$pivotTableName = $WorkSheetname + 'PivotTable'
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber