Two things, checks for $Chart.DataLabel and if the directory for the xlsx path does not exist, it creates it

This commit is contained in:
dfinke
2017-12-02 12:38:24 -05:00
parent 9b57853881
commit dc4b66fffe

View File

@@ -321,13 +321,13 @@
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param(
[Parameter(ParameterSetName="Default",Position=0)]
[Parameter(ParameterSetName="Table" ,Position=0)]
[Parameter(ParameterSetName = "Default", Position = 0)]
[Parameter(ParameterSetName = "Table" , Position = 0)]
[String]$Path,
[Parameter(Mandatory=$true,ParameterSetName="PackageDefault")]
[Parameter(Mandatory=$true,ParameterSetName="PackageTable")]
[Parameter(Mandatory = $true, ParameterSetName = "PackageDefault")]
[Parameter(Mandatory = $true, ParameterSetName = "PackageTable")]
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
[Parameter(ValueFromPipeline=$true)]
[Parameter(ValueFromPipeline = $true)]
$TargetData,
[String]$Password,
[String]$WorkSheetname = 'Sheet1',
@@ -376,8 +376,8 @@
$true
}
})]
[Parameter(ParameterSetName = 'Table' ,Mandatory = $true)]
[Parameter(ParameterSetName = 'PackageTable' ,Mandatory = $true)]
[Parameter(ParameterSetName = 'Table' , Mandatory = $true)]
[Parameter(ParameterSetName = 'PackageTable' , Mandatory = $true)]
[String]$TableName,
[Parameter(ParameterSetName = 'Table')]
[Parameter(ParameterSetName = 'PackageTable')]
@@ -548,7 +548,12 @@
Else {
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
if (Test-Path $Path) {
$targetPath = Split-Path $Path
if (!(Test-Path $targetPath)) {
Write-Debug "Base path $($targetPath) does not exist, creating"
$null = mkdir $targetPath -ErrorAction Ignore
}
elseif (Test-Path $Path) {
Write-Debug "Path '$Path' already exists"
}
@@ -563,16 +568,17 @@
}
if ($append) {
$headerRange = $ws.Dimension.Address -replace "\d+$","1"
$headerRange = $ws.Dimension.Address -replace "\d+$", "1"
#if there is a title or anything else above the header row, specifying StartRow will skip it.
if ($StartRow -ne 1) {$headerRange = $headerRange -replace "1","$StartRow"}
if ($StartRow -ne 1) {$headerRange = $headerRange -replace "1", "$StartRow"}
#$script:Header = $ws.Cells[$headerrange].Value
#using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ }
$row = $ws.Dimension.Rows
Write-Debug -Message ("Appending: headers are " + ($script:Header -join ", ") + "Start row $row")
}
elseif($Title) { #Can only add a title if not appending
elseif ($Title) {
#Can only add a title if not appending
$Row = $StartRow
Add-Title
$Row ++ ; $startRow ++
@@ -587,7 +593,8 @@
$pattern = 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort'
}
Catch {
if ($AlreadyExists) { #Is this set anywhere ?
if ($AlreadyExists) {
#Is this set anywhere ?
throw "Failed exporting worksheet '$WorkSheetname' to '$Path': The worksheet '$WorkSheetname' already exists."
}
else {
@@ -655,9 +662,9 @@
Try {
if ($AutoNameRange) {
if (-not $script:header) {
$headerRange = $ws.Dimension.Address -replace "\d+$","1"
$headerRange = $ws.Dimension.Address -replace "\d+$", "1"
#if there is a title or anything else above the header row, specifying StartRow will skip it.
if ($StartRow -ne 1) {$headerRange = $headerRange -replace "1","$StartRow"}
if ($StartRow -ne 1) {$headerRange = $headerRange -replace "1", "$StartRow"}
#using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ }
}
@@ -666,7 +673,7 @@
foreach ($c in 0..($totalColumns - 1)) {
$targetRangeName = "$($script:Header[$c])"
$targetColumn = $c + $StartColumn
$theCell = $ws.Cells[($startrow+1), $targetColumn, $totalRows , $targetColumn ]
$theCell = $ws.Cells[($startrow + 1), $targetColumn, $totalRows , $targetColumn ]
$ws.Names.Add($targetRangeName, $theCell) | Out-Null
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($targetRangeName)) {
@@ -769,8 +776,10 @@
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
$chart.SetPosition(0, 0, 4, 0) #Changed position to top row, next to a chart which doesn't pivot on columns
$chart.SetSize(600, 400)
if ($chart.DataLabel) {
$chart.DataLabel.ShowCategory = [boolean]$item.value.ShowCategory
$chart.DataLabel.ShowPercent = [boolean]$item.value.ShowPercent
}
if ([boolean]$item.value.NoLegend) {$chart.Legend.Remove()}
if ($item.value.ChartTitle) {$chart.Title.Text = $item.value.chartTitle}
}
@@ -778,7 +787,8 @@
}
}
if ($IncludePivotTable -or $IncludePivotChart) { #changed so -includePivotChart Implies -includePivotTable.
if ($IncludePivotTable -or $IncludePivotChart) {
#changed so -includePivotChart Implies -includePivotTable.
$pivotTableName = $WorkSheetname + 'PivotTable'
#Make sure the Pivot table sheet doesn't already exist
try { $pkg.Workbook.Worksheets.Delete( $pivotTableName) } catch {}
@@ -822,9 +832,11 @@
if ($IncludePivotChart) {
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
if ($chart.DataLabel) {
$chart.DataLabel.ShowCategory = $ShowCategory
$chart.DataLabel.ShowPercent = $ShowPercent
$chart.SetPosition(0,26,2,26) # if Pivot table is rows+data only it will be 2 columns wide if has pivot columns we don't know how wide it will be
}
$chart.SetPosition(0, 26, 2, 26) # if Pivot table is rows+data only it will be 2 columns wide if has pivot columns we don't know how wide it will be
if ($NoLegend) {
$chart.Legend.Remove()
}
@@ -963,7 +975,7 @@
$pkg
}
else {
if($ReturnRange) {
if ($ReturnRange) {
$ws.Dimension.Address
}
@@ -1001,5 +1013,5 @@ function New-PivotTableDefinition {
$parameters = @{} + $PSBoundParameters
$parameters.Remove('PivotTableName')
@{$PivotTableName=$parameters}
@{$PivotTableName = $parameters}
}