mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-14 23:33:18 +00:00
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:
174
Export-Excel.ps1
174
Export-Excel.ps1
@@ -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')]
|
||||||
@@ -493,7 +493,7 @@
|
|||||||
if ($TitleBackgroundColor -AND ($TitleFillPattern -ne 'None')) {
|
if ($TitleBackgroundColor -AND ($TitleFillPattern -ne 'None')) {
|
||||||
$ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
|
$ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
|
||||||
}
|
}
|
||||||
elseif ($TitleBackgroundColor) {
|
elseif ($TitleBackgroundColor) {
|
||||||
Write-Warning "Title Background Color ignored. You must set the TitleFillPattern parameter to a value other than 'None'. Try 'Solid'."
|
Write-Warning "Title Background Color ignored. You must set the TitleFillPattern parameter to a value other than 'None'. Try 'Solid'."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -542,37 +542,43 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($ExcelPackage) {
|
if ($ExcelPackage) {
|
||||||
$pkg = $ExcelPackage
|
$pkg = $ExcelPackage
|
||||||
$Path = $pkg.File
|
$Path = $pkg.File
|
||||||
}
|
}
|
||||||
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
||||||
}
|
}
|
||||||
|
|
||||||
[OfficeOpenXml.ExcelWorksheet]$ws = $pkg | Add-WorkSheet -WorkSheetname $WorkSheetname -NoClobber:$NoClobber -ClearSheet:$ClearSheet #Add worksheet doesn't take any action for -noClobber
|
[OfficeOpenXml.ExcelWorksheet]$ws = $pkg | Add-WorkSheet -WorkSheetname $WorkSheetname -NoClobber:$NoClobber -ClearSheet:$ClearSheet #Add worksheet doesn't take any action for -noClobber
|
||||||
foreach ($format in $ConditionalFormat ) {
|
foreach ($format in $ConditionalFormat ) {
|
||||||
$target = "Add$($format.Formatter)"
|
$target = "Add$($format.Formatter)"
|
||||||
$rule = ($ws.ConditionalFormatting).PSObject.Methods[$target].Invoke($format.Range, $format.IconType)
|
$rule = ($ws.ConditionalFormatting).PSObject.Methods[$target].Invoke($format.Range, $format.IconType)
|
||||||
$rule.Reverse = $format.Reverse
|
$rule.Reverse = $format.Reverse
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@@ -597,76 +604,76 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
Process {
|
Process {
|
||||||
if ($TargetData) {
|
if ($TargetData) {
|
||||||
Try {
|
Try {
|
||||||
if ($firstTimeThru) {
|
if ($firstTimeThru) {
|
||||||
$firstTimeThru = $false
|
$firstTimeThru = $false
|
||||||
$isDataTypeValueType = $TargetData.GetType().name -match $pattern
|
$isDataTypeValueType = $TargetData.GetType().name -match $pattern
|
||||||
Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'"
|
Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'"
|
||||||
}
|
|
||||||
|
|
||||||
if ($isDataTypeValueType) {
|
|
||||||
$ColumnIndex = $StartColumn
|
|
||||||
|
|
||||||
Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData
|
|
||||||
|
|
||||||
$ColumnIndex += 1
|
|
||||||
$Row += 1
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
#region Add headers
|
|
||||||
if (-not $script:Header) {
|
|
||||||
$ColumnIndex = $StartColumn
|
|
||||||
$script:Header = $TargetData.PSObject.Properties.Name | Where-Object {$_ -notin $ExcludeProperty}
|
|
||||||
|
|
||||||
if ($NoHeader) {
|
|
||||||
# Don't push the headers to the spread sheet
|
|
||||||
$Row -= 1
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
foreach ($Name in $script:Header) {
|
|
||||||
$ws.Cells[$Row, $ColumnIndex].Value = $Name
|
|
||||||
Write-Verbose "Cell '$Row`:$ColumnIndex' add header '$Name'"
|
|
||||||
$ColumnIndex += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
$Row += 1
|
if ($isDataTypeValueType) {
|
||||||
$ColumnIndex = $StartColumn
|
$ColumnIndex = $StartColumn
|
||||||
|
|
||||||
foreach ($Name in $script:Header) {
|
Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData
|
||||||
#region Add non header values
|
|
||||||
Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData.$Name
|
|
||||||
|
|
||||||
$ColumnIndex += 1
|
$ColumnIndex += 1
|
||||||
|
$Row += 1
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
#region Add headers
|
||||||
|
if (-not $script:Header) {
|
||||||
|
$ColumnIndex = $StartColumn
|
||||||
|
$script:Header = $TargetData.PSObject.Properties.Name | Where-Object {$_ -notin $ExcludeProperty}
|
||||||
|
|
||||||
|
if ($NoHeader) {
|
||||||
|
# Don't push the headers to the spread sheet
|
||||||
|
$Row -= 1
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
foreach ($Name in $script:Header) {
|
||||||
|
$ws.Cells[$Row, $ColumnIndex].Value = $Name
|
||||||
|
Write-Verbose "Cell '$Row`:$ColumnIndex' add header '$Name'"
|
||||||
|
$ColumnIndex += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
$Row += 1
|
||||||
|
$ColumnIndex = $StartColumn
|
||||||
|
|
||||||
|
foreach ($Name in $script:Header) {
|
||||||
|
#region Add non header values
|
||||||
|
Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData.$Name
|
||||||
|
|
||||||
|
$ColumnIndex += 1
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Catch {
|
||||||
|
throw "Failed exporting worksheet '$WorkSheetname' to '$Path': $_"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Catch {
|
|
||||||
throw "Failed exporting worksheet '$WorkSheetname' to '$Path': $_"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
End {
|
End {
|
||||||
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 += $_ }
|
||||||
}
|
}
|
||||||
$totalRows = $ws.Dimension.End.Row
|
$totalRows = $ws.Dimension.End.Row
|
||||||
$totalColumns = $ws.Dimension.Columns
|
$totalColumns = $ws.Dimension.Columns
|
||||||
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)) {
|
||||||
@@ -676,7 +683,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($Title) {
|
if ($Title) {
|
||||||
$startAddress = $ws.Dimension.Start.address -replace "$($ws.Dimension.Start.row)`$", "$($ws.Dimension.Start.row + 1)"
|
$startAddress = $ws.Dimension.Start.address -replace "$($ws.Dimension.Start.row)`$", "$($ws.Dimension.Start.row + 1)"
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$startAddress = $ws.Dimension.Start.Address
|
$startAddress = $ws.Dimension.Start.Address
|
||||||
@@ -709,7 +716,7 @@
|
|||||||
$targetName = $item.Key
|
$targetName = $item.Key
|
||||||
$pivotTableName = $targetName #+ 'PivotTable'
|
$pivotTableName = $targetName #+ '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 {}
|
||||||
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
||||||
$pivotTableDataName = $targetName + 'PivotTableData'
|
$pivotTableDataName = $targetName + 'PivotTableData'
|
||||||
|
|
||||||
@@ -769,19 +776,22 @@
|
|||||||
$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)
|
||||||
$chart.DataLabel.ShowCategory = [boolean]$item.value.ShowCategory
|
if ($chart.DataLabel) {
|
||||||
$chart.DataLabel.ShowPercent = [boolean]$item.value.ShowPercent
|
$chart.DataLabel.ShowCategory = [boolean]$item.value.ShowCategory
|
||||||
if ([boolean]$item.value.NoLegend) {$chart.Legend.Remove()}
|
$chart.DataLabel.ShowPercent = [boolean]$item.value.ShowPercent
|
||||||
if ($item.value.ChartTitle) {$chart.Title.Text = $item.value.chartTitle}
|
}
|
||||||
|
if ([boolean]$item.value.NoLegend) {$chart.Legend.Remove()}
|
||||||
|
if ($item.value.ChartTitle) {$chart.Title.Text = $item.value.chartTitle}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {}
|
||||||
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
||||||
|
|
||||||
$wsPivot.View.TabSelected = $true
|
$wsPivot.View.TabSelected = $true
|
||||||
@@ -822,9 +832,11 @@
|
|||||||
|
|
||||||
if ($IncludePivotChart) {
|
if ($IncludePivotChart) {
|
||||||
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
|
$chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
|
||||||
$chart.DataLabel.ShowCategory = $ShowCategory
|
if ($chart.DataLabel) {
|
||||||
$chart.DataLabel.ShowPercent = $ShowPercent
|
$chart.DataLabel.ShowCategory = $ShowCategory
|
||||||
$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.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
|
||||||
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}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user