5 July See readme.md

This commit is contained in:
jhoneill
2018-07-05 17:34:12 +01:00
parent fae040ced1
commit 892ba5d73f
3 changed files with 807 additions and 109 deletions

View File

@@ -356,7 +356,7 @@
[Int]$TitleSize = 22,
[System.Drawing.Color]$TitleBackgroundColor,
[Switch]$IncludePivotTable,
[String]$PivotTableName,
[String]$PivotTableName,
[String[]]$PivotRows,
[String[]]$PivotColumns,
$PivotData,
@@ -458,17 +458,10 @@
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as value"
break
}
{(($NoNumberConversion) -and ($NoNumberConversion -contains $Name)) -or
($NoNumberConversion -eq '*')} {
#Save text without it to converting to number
$TargetCell.Value = $_
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($TargetCell.Value)' unconverted"
break
}
{($_ -is [String]) -and ($_[0] -eq '=')} {
#region Save an Excel formula
$TargetCell.Formula = $_
if ($setNumformat) {$targetCell.Style.Numberformat.Format = $Numberformat }
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as formula"
break
}
@@ -481,12 +474,18 @@
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($_.AbsoluteUri)' as Hyperlink"
break
}
{(($NoNumberConversion) -and ($NoNumberConversion -contains $Name)) -or
($NoNumberConversion -eq '*')} {
#Save text without it to converting to number
$TargetCell.Value = $_
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($TargetCell.Value)' unconverted"
break
}
Default {
#Save a value as a number if possible
$number = $null
if ([Double]::TryParse( $_ , [ref]$number)) {
#was [Double]::TryParse([String]$_, [System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number)) {
if ( [Double]::TryParse([String]$_, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number)) {
# as simpler version using [Double]::TryParse( $_ , [ref]$number)) was found to cause problems reverted back to the longer version
$TargetCell.Value = $number
if ($setNumformat) {$targetCell.Style.Numberformat.Format = $Numberformat }
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($TargetCell.Value)' as number converted from '$_' with format '$Numberformat'"
@@ -629,6 +628,7 @@
$ColumnIndex += 1
#endregion
}
$ColumnIndex -= 1 # column index will be the last column whether isDataTypeValueType was true or false
}
}
Catch {
@@ -638,6 +638,20 @@
}
End {
if ($firstTimeThru) {
$LastRow = $ws.Dimension.End.Row
$LastCol = $ws.Dimension.End.Column
$endAddress = $ws.Dimension.End.Address
}
else {
$LastRow = $Row
$LastCol = $ColumnIndex
$endAddress = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$LastRow]C[$LastCol]", 0, 0)
}
$startAddress = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$StartRow]C[$StartColumn]", 0, 0)
$dataRange = "{0}:{1}" -f $startAddress, $endAddress
Write-Debug "Data Range '$dataRange'"
if ($AutoNameRange) {
Try {
if (-not $script:header) {
@@ -657,10 +671,10 @@
#, but start.column is the first populated one and .Columns is the count of populated ones.
# if we have 5 columns from 3 to 8, headers are numbered 0..4, so that is in the for loop and used for getting the name...
# but we have to add the start column on when referencing positions
foreach ($c in 0..($ws.Dimension.Columns - 1)) {
foreach ($c in 0..($LastCol - $StartColumn)) {
$targetRangeName = $script:Header[$c] -replace '\W' , '_'
$targetColumn = $c + $StartColumn
$theRange = $ws.Cells[$targetRow, $targetColumn, $ws.Dimension.End.Row , $targetColumn ]
$theRange = $ws.Cells[$targetRow, $targetColumn, $LastRow , $targetColumn ]
if ($ws.names[$targetRangeName]) { $ws.names[$targetRangeName].Address = $theRange.FullAddressAbsolute }
else {$ws.Names.Add($targetRangeName, $theRange) | Out-Null }
@@ -671,87 +685,39 @@
}
Catch {Write-Warning -Message "Failed adding named ranges to worksheet '$WorkSheetname': $_" }
}
try {
if ($Title) {
$startAddress = $ws.Dimension.Start.address -replace "$($ws.Dimension.Start.row)`$", "$($ws.Dimension.Start.row + 1)"
}
else {
$startAddress = $ws.Dimension.Start.Address
}
$dataRange = "{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
Write-Debug "Data Range '$dataRange'"
if (-not [String]::IsNullOrEmpty($RangeName)) {
if ($RangeName) {
try {
if ($RangeName -match "\W") {
Write-Warning -Message "At least one character in $RangeName is illegal in a range name and will be replaced with '_' . "
$RangeName = $RangeName -replace '\W', '_'
}
#If named range exists, update it, else create it
if ($ws.Names[$RangeName]) { $ws.Names[$rangename].Address = $ws.Cells[$dataRange].FullAddressAbsolute }
else {$ws.Names.Add($RangeName, $ws.Cells[$0ange]) | Out-Null }
else {$ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null }
}
Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" }
}
Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" }
if (-not [String]::IsNullOrEmpty($TableName)) {
try {
$csr = $StartRow
$csc = $StartColumn
$cer = $ws.Dimension.End.Row
$cec = $ws.Dimension.End.Column # was $script:Header.Count
if ($TableName) {
try {
if ($TableName -match "\W") {
Write-Warning -Message "At least one character in $TableName is illegal in a table name and will be replaced with '_' . "
$TableName = $TableName -replace '\W', '_'
}
$targetRange = $ws.Cells[$csr, $csc, $cer, $cec]
#if the table exists, update it.
if ($ws.Tables[$TableName]) {
$ws.Tables[$TableName].TableXml.table.ref = $targetRange.Address
$ws.Tables[$TableName].TableXml.table.ref = $dataRange
$ws.Tables[$TableName].TableStyle = $TableStyle
}
else {
$tbl = $ws.Tables.Add($targetRange, $TableName)
$tbl = $ws.Tables.Add($ws.Cells[$dataRange], $TableName)
$tbl.TableStyle = $TableStyle
}
Write-Verbose -Message "Defined table '$TableName' at $($targetRange.Address)"
}
catch {Write-Warning -Message "Failed adding table '$TableName' to worksheet '$WorkSheetname': $_"}
}
if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$params = $item.value
if ($params.keys -notcontains "SourceRange" -and
($params.Keys -notcontains "SourceWorkSheet" -or $params.SourceWorkSheet -eq $WorkSheetname)) {$params.SourceRange = $dataRange}
if ($params.Keys -notcontains "SourceWorkSheet") {$params.SourceWorkSheet = $ws }
if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true}
if ($params.Keys -notcontains "PivotDataToColumn" -and $PivotDataToColumn) {$params.PivotDataToColumn = $true}
Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params
}
}
if ($IncludePivotTable -or $IncludePivotChart) {
$params = @{
"SourceRange" = $dataRange
}
if ($PivotTableName) {$params.PivotTableName = $PivotTableName}
else {$params.PivotTableName = $WorkSheetname + 'PivotTable'}
if ($PivotFilter) {$params.PivotFilter = $PivotFilter}
if ($PivotRows) {$params.PivotRows = $PivotRows}
if ($PivotColumns) {$Params.PivotColumns = $PivotColumns}
if ($PivotData) {$Params.PivotData = $PivotData}
if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true}
if ($PivotDataToColumn) {$params.PivotDataToColumn = $true}
if ($IncludePivotChart) {
$params.IncludePivotChart = $true
$Params.ChartType = $ChartType
if ($ShowCategory) {$params.ShowCategory = $true}
if ($ShowPercent) {$params.ShowPercent = $true}
if ($NoLegend) {$params.NoLegend = $true}
}
Add-PivotTable -ExcelPackage $pkg -SourceWorkSheet $ws @params
}
if ($AutoFilter) {
try {
@@ -761,6 +727,40 @@
catch {Write-Warning -Message "Failed adding autofilter to worksheet '$WorkSheetname': $_"}
}
if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$params = $item.value
if ($params.keys -notcontains "SourceRange" -and
($params.Keys -notcontains "SourceWorkSheet" -or $params.SourceWorkSheet -eq $WorkSheetname)) {$params.SourceRange = $dataRange}
if ($params.Keys -notcontains "SourceWorkSheet") {$params.SourceWorkSheet = $ws }
if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true}
if ($params.Keys -notcontains "PivotDataToColumn" -and $PivotDataToColumn) {$params.PivotDataToColumn = $true}
Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params
}
}
if ($IncludePivotTable -or $IncludePivotChart) {
$params = @{
"SourceRange" = $dataRange
}
if ($PivotTableName) {$params.PivotTableName = $PivotTableName}
else {$params.PivotTableName = $WorkSheetname + 'PivotTable'}
if ($PivotFilter) {$params.PivotFilter = $PivotFilter}
if ($PivotRows) {$params.PivotRows = $PivotRows}
if ($PivotColumns) {$Params.PivotColumns = $PivotColumns}
if ($PivotData) {$Params.PivotData = $PivotData}
if ($NoTotalsInPivot) {$params.NoTotalsInPivot = $true}
if ($PivotDataToColumn) {$params.PivotDataToColumn = $true}
if ($IncludePivotChart) {
$params.IncludePivotChart = $true
$Params.ChartType = $ChartType
if ($ShowCategory) {$params.ShowCategory = $true}
if ($ShowPercent) {$params.ShowPercent = $true}
if ($NoLegend) {$params.NoLegend = $true}
}
Add-PivotTable -ExcelPackage $pkg -SourceWorkSheet $ws @params
}
try {
if ($FreezeTopRow) {
$ws.View.FreezePanes(2, 1)
@@ -823,7 +823,7 @@
foreach ($chartDef in $ExcelChartDefinition) {
$params = @{}
$chartDef.PSObject.Properties | ForEach-Object {if ($_.value -ne $null) {$params[$_.name] = $_.value}}
Add-ExcelChart @params
Add-ExcelChart -Worksheet $ws @params
}
foreach ($ct in $ConditionalText) {
@@ -841,8 +841,8 @@
if ($CellStyleSB) {
try {
$TotalRows = $ws.Dimension.Rows
$LastColumn = $ws.Dimension.Address -replace "^.*:(\w*)\d+$" , '$1'
$TotalRows = $ws.Dimension.Rows
$LastColumn = $ws.Dimension.Address -replace "^.*:(\w*)\d+$" , '$1'
& $CellStyleSB $ws $TotalRows $LastColumn
}
catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorkSheetname': $_"}
@@ -1172,12 +1172,12 @@ function Add-ExcelChart {
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked",
$XRange,
$YRange,
[int]$Width = 500,
[int]$Height = 350,
[int]$Row = 0,
[int]$RowOffSetPixels = 10,
[int]$Column = 6,
[int]$ColumnOffSetPixels = 5,
[int]$Width = 500,
[int]$Height = 350,
[int]$Row = 0,
[int]$RowOffSetPixels = 10,
[int]$Column = 6,
[int]$ColumnOffSetPixels = 5,
[OfficeOpenXml.Drawing.Chart.eLegendPosition]$LegendPostion,
$LegendSize,
[Switch]$legendBold,
@@ -1191,19 +1191,19 @@ function Add-ExcelChart {
[Switch]$XAxisTitleBold,
$XAxisTitleSize ,
[string]$XAxisNumberformat,
[double]$XMajorUnit,
[double]$XMinorUnit,
[double]$XMaxValue,
[double]$XMinValue,
$XMajorUnit,
$XMinorUnit,
$XMaxValue,
$XMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$XAxisPosition ,
[String]$YAxisTitleText,
[Switch]$YAxisTitleBold,
$YAxisTitleSize,
[string]$YAxisNumberformat,
[double]$YMajorUnit,
[double]$YMinorUnit,
[double]$YMaxValue,
[double]$YMinValue,
$YMajorUnit,
$YMinorUnit,
$YMaxValue,
$YMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition )
try {
$ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', ''
@@ -1213,36 +1213,35 @@ function Add-ExcelChart {
if ($TitleSize) {$chart.Title.Font.Size = $TitleSize}
if ($NoLegend) { $chart.Legend.Remove() }
else {
else {
if ($LegendPostion) {$Chart.Legend.Position = $LegendPostion}
if ($LegendSize) {$chart.Legend.Font.Size = $LegendSize}
if ($legendBold) {$chart.Legend.Font.Bold = $legendBold}
if ($legendBold) {$chart.Legend.Font.Bold = $true}
}
if ($XAxisTitleText) {
if ($XAxisTitleText) {
$chart.XAxis.Title.Text = $XAxisTitleText
if ($XAxisTitleBold) {$chart.XAxis.Title.Font.Bold = $true}
if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize}
if ($XAxisTitleBold) {$chart.XAxis.Title.Font.Bold = $true}
if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize}
}
if ($XAxisPosition) {$chart.XAxis.AxisPosition = $XAxisPosition}
if ($XMajorUnit) {$chart.XAxis.MajorUnit = $XMajorUnit}
if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit}
if ($XMinValue) {$chart.XAxis.MinValue = $XMinValue}
if ($XMaxValue) {$chart.XAxis.MaxValue = $XMaxValue}
if ($XAxisNumberformat) {$chart.XAxis.Format = $XAxisNumberformat}
if ($XAxisPosition) {$chart.XAxis.AxisPosition = $XAxisPosition}
if ($XMajorUnit) {$chart.XAxis.MajorUnit = $XMajorUnit}
if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit}
if ($XMinValue -ne $null) {$chart.XAxis.MinValue = $XMinValue}
if ($XMaxValue -ne $null) {$chart.XAxis.MaxValue = $XMaxValue}
if ($XAxisNumberformat) {$chart.XAxis.Format = $XAxisNumberformat}
if ($YAxisTitleText) {
if ($YAxisTitleText) {
$chart.YAxis.Title.Text = $YAxisTitleText
if ($YAxisTitleBold) {$chart.YAxis.Title.Font.Bold = $true}
if ($YAxisTitleSize) {$chart.YAxis.Title.Font.Size = $YAxisTitleSize}
}
if ($YAxisPosition) {$chart.YAxis.AxisPosition = $YAxisPosition}
if ($YMajorUnit) {$chart.YAxis.MajorUnit = $YMajorUnit}
if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit}
if ($YMinValue) {$chart.YAxis.MinValue = $YMinValue}
if ($YMaxValue) {$chart.YAxis.MaxValue = $YMaxValue}
if ($YAxisNumberformat) {$chart.YAxis.Format = $YAxisNumberformat}
if ($YAxisPosition) {$chart.YAxis.AxisPosition = $YAxisPosition}
if ($YMajorUnit) {$chart.YAxis.MajorUnit = $YMajorUnit}
if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit}
if ($YMinValue-ne $null) {$chart.YAxis.MinValue = $YMinValue}
if ($YMaxValue-ne $null) {$chart.YAxis.MaxValue = $YMaxValue}
if ($YAxisNumberformat) {$chart.YAxis.Format = $YAxisNumberformat}
if ($chart.Datalabel -ne $null) {
$chart.Datalabel.ShowCategory = [boolean]$ShowCategory
$chart.Datalabel.ShowPercent = [boolean]$ShowPercent