works with multiple sheets

This commit is contained in:
dfinke
2017-10-12 11:33:49 -04:00
parent 1004d8a9ea
commit 57c02a466d
2 changed files with 47 additions and 19 deletions

View File

@@ -1,25 +1,37 @@
"To ship, is to choose" # To ship, is to choose
ipmo .\ImportExcel.psd1 -Force ipmo .\ImportExcel.psd1 -Force
$file = "c:\temp\testPT.xlsx"
rm $file -ErrorAction Ignore
$pt=[ordered]@{} $pt=[ordered]@{}
$pt.PT1=@{ $pt.PT1=@{
SourceWorkSheet='Sheet1'
PivotRows = "Status" PivotRows = "Status"
PivotData= @{'Status'='count'} PivotData= @{'Status'='count'}
IncludePivotChart=$true IncludePivotChart=$true
ChartType='BarClustered3D'
} }
$pt.PT2=@{ $pt.PT2=@{
PivotRows = "StartType" SourceWorkSheet='Sheet2'
PivotData= @{'StartType'='count'} PivotRows = "Company"
PivotData= @{'Company'='count'}
IncludePivotChart=$true IncludePivotChart=$true
ChartType='PieExploded3D'
} }
$gsv=Get-Service | Select-Object status, Name, displayName, starttype
$ps=Get-Process | Select-Object Name,Company, Handles
$file = "c:\temp\testPT.xlsx"
rm $file -ErrorAction Ignore
$gsv| Export-Excel -Path $file -AutoSize
$ps | Export-Excel -Path $file -AutoSize -WorkSheetname Sheet2 -PivotTableDefinition $pt -Show
return
Get-Service | Get-Service |
select status, Name, displayName, starttype | select status, Name, displayName, starttype |
Export-Excel -Path $file -Show -PivotTable $pt -AutoSize Export-Excel -Path $file -Show -PivotTable $pt -AutoSize

View File

@@ -263,6 +263,14 @@ Function Export-Excel {
) )
Begin { Begin {
function Find-WorkSheet {
param (
$WorkSheetName
)
$pkg.Workbook.Worksheets | Where-Object {$_.name -match $WorkSheetName}
}
Function Add-CellValue { Function Add-CellValue {
<# <#
.SYNOPSIS .SYNOPSIS
@@ -539,7 +547,20 @@ Function Export-Excel {
$pivotTableName = $targetName + 'PivotTable' $pivotTableName = $targetName + 'PivotTable'
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber $wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
$pivotTableDataName = $targetName + 'PivotTableData' $pivotTableDataName = $targetName + 'PivotTableData'
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $ws.Cells[$dataRange], $pivotTableDataName)
if (!$item.Value.SourceWorkSheet) {
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $ws.Cells[$dataRange], $pivotTableDataName)
}
else {
$workSheet = Find-WorkSheet $item.Value.SourceWorkSheet
if($workSheet) {
$targetStartAddress = $workSheet.Dimension.Start.Address
$targetDataRange = "{0}:{1}" -f $targetStartAddress, $workSheet.Dimension.End.Address
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $workSheet.Cells[$targetDataRange], $pivotTableDataName)
}
}
switch ($item.Value.Keys) { switch ($item.Value.Keys) {
"PivotRows" { "PivotRows" {
@@ -575,17 +596,14 @@ Function Export-Excel {
} }
"IncludePivotChart" { "IncludePivotChart" {
$ChartType = "Pie"
if ($item.Value.ChartType) {
$ChartType = $item.Value.ChartType
}
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable) $chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
#$chart.DataLabel.ShowCategory = $ShowCategory $chart.SetPosition(1, 0, 3, 0)
#$chart.DataLabel.ShowPercent = $ShowPercent
#if ($NoLegend) {
# $chart.Legend.Remove()
#}
$chart.SetPosition(1, 0, 6, 0)
$chart.SetSize(600, 400) $chart.SetSize(600, 400)
} }
} }
} }
@@ -640,8 +658,6 @@ Function Export-Excel {
$chart.Legend.Remove() $chart.Legend.Remove()
} }
$chart.SetPosition(1, 0, 6, 0)
$chart.SetSize(600, 400)
} }
} }
@@ -788,4 +804,4 @@ Function Export-Excel {
throw "Failed exporting worksheet '$WorkSheetname' to '$Path': $_" throw "Failed exporting worksheet '$WorkSheetname' to '$Path': $_"
} }
} }
} }