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')] [CmdletBinding(DefaultParameterSetName = 'Default')]
Param( Param(
[Parameter(ParameterSetName="Default",Position=0)] [Parameter(ParameterSetName = "Default", Position = 0)]
[Parameter(ParameterSetName="Table" ,Position=0)] [Parameter(ParameterSetName = "Table" , Position = 0)]
[String]$Path, [String]$Path,
[Parameter(Mandatory=$true,ParameterSetName="PackageDefault")] [Parameter(Mandatory = $true, ParameterSetName = "PackageDefault")]
[Parameter(Mandatory=$true,ParameterSetName="PackageTable")] [Parameter(Mandatory = $true, ParameterSetName = "PackageTable")]
[OfficeOpenXml.ExcelPackage]$ExcelPackage, [OfficeOpenXml.ExcelPackage]$ExcelPackage,
[Parameter(ValueFromPipeline=$true)] [Parameter(ValueFromPipeline = $true)]
$TargetData, $TargetData,
[String]$Password, [String]$Password,
[String]$WorkSheetname = 'Sheet1', [String]$WorkSheetname = 'Sheet1',
@@ -376,8 +376,8 @@
$true $true
} }
})] })]
[Parameter(ParameterSetName = 'Table' ,Mandatory = $true)] [Parameter(ParameterSetName = 'Table' , Mandatory = $true)]
[Parameter(ParameterSetName = 'PackageTable' ,Mandatory = $true)] [Parameter(ParameterSetName = 'PackageTable' , Mandatory = $true)]
[String]$TableName, [String]$TableName,
[Parameter(ParameterSetName = 'Table')] [Parameter(ParameterSetName = 'Table')]
[Parameter(ParameterSetName = 'PackageTable')] [Parameter(ParameterSetName = 'PackageTable')]
@@ -548,7 +548,12 @@
Else { Else {
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $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" Write-Debug "Path '$Path' already exists"
} }
@@ -563,16 +568,17 @@
} }
if ($append) { 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 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 #$script:Header = $ws.Cells[$headerrange].Value
#using a slightly odd syntax otherwise header ends up as a 2D array #using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ } $ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ }
$row = $ws.Dimension.Rows $row = $ws.Dimension.Rows
Write-Debug -Message ("Appending: headers are " + ($script:Header -join ", ") + "Start row $row") 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 $Row = $StartRow
Add-Title Add-Title
$Row ++ ; $startRow ++ $Row ++ ; $startRow ++
@@ -587,7 +593,8 @@
$pattern = 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort' $pattern = 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort'
} }
Catch { 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." throw "Failed exporting worksheet '$WorkSheetname' to '$Path': The worksheet '$WorkSheetname' already exists."
} }
else { else {
@@ -655,9 +662,9 @@
Try { Try {
if ($AutoNameRange) { if ($AutoNameRange) {
if (-not $script:header) { 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 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 #using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ } $ws.Cells[$headerRange].Value | foreach -Begin {$Script:header = @()} -Process {$Script:header += $_ }
} }
@@ -666,7 +673,7 @@
foreach ($c in 0..($totalColumns - 1)) { foreach ($c in 0..($totalColumns - 1)) {
$targetRangeName = "$($script:Header[$c])" $targetRangeName = "$($script:Header[$c])"
$targetColumn = $c + $StartColumn $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 $ws.Names.Add($targetRangeName, $theCell) | Out-Null
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($targetRangeName)) { if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($targetRangeName)) {
@@ -769,8 +776,10 @@
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable) $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.SetPosition(0, 0, 4, 0) #Changed position to top row, next to a chart which doesn't pivot on columns
$chart.SetSize(600, 400) $chart.SetSize(600, 400)
if ($chart.DataLabel) {
$chart.DataLabel.ShowCategory = [boolean]$item.value.ShowCategory $chart.DataLabel.ShowCategory = [boolean]$item.value.ShowCategory
$chart.DataLabel.ShowPercent = [boolean]$item.value.ShowPercent $chart.DataLabel.ShowPercent = [boolean]$item.value.ShowPercent
}
if ([boolean]$item.value.NoLegend) {$chart.Legend.Remove()} if ([boolean]$item.value.NoLegend) {$chart.Legend.Remove()}
if ($item.value.ChartTitle) {$chart.Title.Text = $item.value.chartTitle} 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' $pivotTableName = $WorkSheetname + 'PivotTable'
#Make sure the Pivot table sheet doesn't already exist #Make sure the Pivot table sheet doesn't already exist
try { $pkg.Workbook.Worksheets.Delete( $pivotTableName) } catch {} try { $pkg.Workbook.Worksheets.Delete( $pivotTableName) } catch {}
@@ -822,9 +832,11 @@
if ($IncludePivotChart) { if ($IncludePivotChart) {
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable) $chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
if ($chart.DataLabel) {
$chart.DataLabel.ShowCategory = $ShowCategory $chart.DataLabel.ShowCategory = $ShowCategory
$chart.DataLabel.ShowPercent = $ShowPercent $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) { if ($NoLegend) {
$chart.Legend.Remove() $chart.Legend.Remove()
} }
@@ -963,7 +975,7 @@
$pkg $pkg
} }
else { else {
if($ReturnRange) { if ($ReturnRange) {
$ws.Dimension.Address $ws.Dimension.Address
} }
@@ -1001,5 +1013,5 @@ function New-PivotTableDefinition {
$parameters = @{} + $PSBoundParameters $parameters = @{} + $PSBoundParameters
$parameters.Remove('PivotTableName') $parameters.Remove('PivotTableName')
@{$PivotTableName=$parameters} @{$PivotTableName = $parameters}
} }