diff --git a/Examples/TryMultiplePivotTables.ps1 b/Examples/TryMultiplePivotTables.ps1 new file mode 100644 index 0000000..449ac2c --- /dev/null +++ b/Examples/TryMultiplePivotTables.ps1 @@ -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 \ No newline at end of file diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 794cd1b..d542f6a 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -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