Added several new features

This commit is contained in:
dfinke
2015-12-17 16:53:13 -05:00
parent dc753b67b2
commit 4c620ee630
4 changed files with 103 additions and 28 deletions

0
Examples/notes.md Normal file
View File

View File

@@ -40,11 +40,22 @@ function Export-Excel {
[Switch]$NoHeader, [Switch]$NoHeader,
[string]$RangeName, [string]$RangeName,
[string]$TableName, [string]$TableName,
[OfficeOpenXml.Table.TableStyles]$TableStyle="Medium6",
[Object[]]$ConditionalFormat, [Object[]]$ConditionalFormat,
[string[]]$HideSheet [Object[]]$ExcelChartDefinition,
[string[]]$HideSheet,
[Switch]$KillExcel,
[Switch]$AutoNameRange,
$StartRow=1,
$StartColumn=1
) )
Begin { Begin {
if($KillExcel) {
Get-Process excel -ErrorAction Ignore | Stop-Process
while (Get-Process excel -ErrorAction Ignore) {}
}
try { try {
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
if (Test-Path $path) { if (Test-Path $path) {
@@ -56,25 +67,26 @@ function Export-Excel {
foreach($format in $ConditionalFormat ) { foreach($format in $ConditionalFormat ) {
$target = "Add$($format.Formatter)" $target = "Add$($format.Formatter)"
$rule = ($ws.ConditionalFormatting).$target.Invoke($format.Address, $format.IconType) $rule = ($ws.ConditionalFormatting).$target($format.Address, $format.IconType)
$rule.Reverse = $format.Reverse $rule.Reverse = $format.Reverse
} }
# Force at least one cell value # Force at least one cell value
$ws.Cells[1, 1].Value = "" #$ws.Cells[1, 1].Value = ""
$Row = 1
$Row = $StartRow
if($Title) { if($Title) {
$ws.Cells[$Row, 1].Value = $Title $ws.Cells[$Row, $StartColumn].Value = $Title
$ws.Cells[$Row, 1].Style.Font.Size = $TitleSize $ws.Cells[$Row, $StartColumn].Style.Font.Size = $TitleSize
$ws.Cells[$Row, 1].Style.Font.Bold = $TitleBold $ws.Cells[$Row, $StartColumn].Style.Font.Bold = $TitleBold
$ws.Cells[$Row, 1].Style.Fill.PatternType = $TitleFillPattern $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
if($TitleBackgroundColor) { if($TitleBackgroundColor) {
$ws.Cells[$Row, 1].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) $ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
} }
$Row = 2 $Row += 1
} }
} Catch { } Catch {
@@ -97,7 +109,7 @@ function Export-Excel {
} }
if($isDataTypeValueType) { if($isDataTypeValueType) {
$ColumnIndex = 1 $ColumnIndex = $StartColumn
$targetCell = $ws.Cells[$Row, $ColumnIndex] $targetCell = $ws.Cells[$Row, $ColumnIndex]
@@ -119,7 +131,8 @@ function Export-Excel {
} else { } else {
if(!$Header) { if(!$Header) {
$ColumnIndex = 1 $ColumnIndex = $StartColumn
$Header = $TargetData.psobject.properties.name $Header = $TargetData.psobject.properties.name
if($NoHeader) { if($NoHeader) {
@@ -134,7 +147,7 @@ function Export-Excel {
} }
$Row += 1 $Row += 1
$ColumnIndex = 1 $ColumnIndex = $StartColumn
foreach ($Name in $Header) { foreach ($Name in $Header) {
@@ -142,11 +155,16 @@ function Export-Excel {
$cellValue=$TargetData.$Name $cellValue=$TargetData.$Name
$r=$null if($cellValue -is [string] -and $cellValue.StartsWith('=')) {
if([double]::tryparse($cellValue, [ref]$r)) { $targetCell.Formula = $cellValue
$targetCell.Value = $r
} else { } else {
$targetCell.Value = $cellValue
$r=$null
if([double]::tryparse($cellValue, [ref]$r)) {
$targetCell.Value = $r
} else {
$targetCell.Value = $cellValue
}
} }
switch ($TargetData.$Name) { switch ($TargetData.$Name) {
@@ -159,15 +177,43 @@ function Export-Excel {
} }
End { End {
if($AutoNameRange) {
$totalRows=$ws.Dimension.Rows
$totalColumns=$ws.Dimension.Columns
foreach($c in 0..($totalColumns-1)) {
$targetRangeName = "$($Header[$c])"
$targetColumn = $c+1
$theCell = $ws.Cells[2,$targetColumn,$totalRows,$targetColumn ]
$ws.Names.Add($targetRangeName, $theCell) | Out-Null
}
}
$startAddress=$ws.Dimension.Start.Address $startAddress=$ws.Dimension.Start.Address
$dataRange="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address $dataRange="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
Write-Debug "Data Range $dataRange" Write-Debug "Data Range $dataRange"
if (-not [string]::IsNullOrEmpty($RangeName)) { if (-not [string]::IsNullOrEmpty($RangeName)) {
$ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null $ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null
} }
if (-not [string]::IsNullOrEmpty($TableName)) { if (-not [string]::IsNullOrEmpty($TableName)) {
$ws.Tables.Add($ws.Cells[$dataRange], $TableName) | Out-Null #$ws.Tables.Add($ws.Cells[$dataRange], $TableName) | Out-Null
#"$($StartRow),$($StartColumn),$($ws.Dimension.End.Row-$StartRow),$($Header.Count)"
$csr=$StartRow
$csc=$StartColumn
$cer=$ws.Dimension.End.Row #-$StartRow+1
$cec=$Header.Count
$targetRange=$ws.Cells[$csr, $csc, $cer,$cec]
$tbl = $ws.Tables.Add($targetRange, $TableName)
$tbl.TableStyle=$TableStyle
$idx
} }
if($IncludePivotTable) { if($IncludePivotTable) {
@@ -235,6 +281,27 @@ function Export-Excel {
foreach($Sheet in $HideSheet) { foreach($Sheet in $HideSheet) {
$pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden" $pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden"
}
$chartCount=0
foreach ($chartDef in $ExcelChartDefinition) {
$ChartName = "Chart"+(Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.',''
$chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType)
$chart.Title.Text = $chartDef.Title
$chart.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels,$chartDef.Column, $chartDef.ColumnOffsetPixels)
$chart.SetSize($chartDef.Width, $chartDef.Height)
$chartDefCount = @($chartDef.XRange).Count
if($chartDefCount -eq 1) {
$Series=$chart.Series.Add($chartDef.YRange, $chartDef.XRange)
$Series.Header = $chartDef.Header
} else {
for($idx=0; $idx -lt $chartDefCount; $idx+=1) {
$Series=$chart.Series.Add($chartDef.YRange[$idx], $chartDef.XRange)
$Series.Header = $chartDef.Header[$idx]
}
}
} }
$pkg.Save() $pkg.Save()
@@ -242,4 +309,4 @@ function Export-Excel {
if($Show) {Invoke-Item $Path} if($Show) {Invoke-Item $Path}
} }
} }

View File

@@ -3,6 +3,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\Export-Excel.ps1 . $PSScriptRoot\Export-Excel.ps1
. $PSScriptRoot\New-ConditionalFormattingIconSet.ps1 . $PSScriptRoot\New-ConditionalFormattingIconSet.ps1
. $PSScriptRoot\Export-ExcelSheet.ps1 . $PSScriptRoot\Export-ExcelSheet.ps1
. $PSScriptRoot\New-ExcelChart.ps1
function Import-Excel { function Import-Excel {
param( param(
@@ -77,17 +78,14 @@ function Add-WorkSheet {
[string] $WorkSheetname, [string] $WorkSheetname,
[Switch] $NoClobber [Switch] $NoClobber
) )
if($ExcelPackage.Workbook.Worksheets[$WorkSheetname]) {
if($NoClobber) { $ws = $ExcelPackage.Workbook.Worksheets[$WorkSheetname]
$AlreadyExists = $true
Write-Error "Worksheet `"$WorkSheetname`" already exists." if(!$ws) {
} else { $ws=$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname)
Write-Debug "Worksheet `"$WorkSheetname`" already exists. Deleting"
$ExcelPackage.Workbook.Worksheets.Delete($WorkSheetname)
}
} }
$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname) return $ws
} }
function ConvertFrom-ExcelSheet { function ConvertFrom-ExcelSheet {

View File

@@ -25,6 +25,16 @@ Known Issues
What's new What's new
- -
#### 12/17/2015
These new features open the door for really sophisticated work sheet creation.
Stay tuned for a [blog post](http://www.dougfinke.com/blog/) and examples.
***Quick List***
* StartRow, StartColumn for placing data anywhere in a sheet
* New-ExcelChart - Add charts to a sheet, multiple series for a chart, locate the chart anywhere on the sheet
* AutoNameRange, Use functions and/or calculations in a cell
#### 10/20/2015 #### 10/20/2015
Big bug fix for version 3.0 PowerShell folks! Big bug fix for version 3.0 PowerShell folks!