diff --git a/Public/Add-ExcelTable.ps1 b/Public/Add-ExcelTable.ps1 index 5a179a8..aee0bcb 100644 --- a/Public/Add-ExcelTable.ps1 +++ b/Public/Add-ExcelTable.ps1 @@ -9,7 +9,7 @@ function Add-ExcelTable { [Switch]$ShowHeader , [Switch]$ShowFilter, [Switch]$ShowTotal, - [hashtable]$TotalSettings, + [hashtable]$TableTotalSettings, [Switch]$ShowFirstColumn, [Switch]$ShowLastColumn, [Switch]$ShowRowStripes, @@ -51,16 +51,28 @@ function Add-ExcelTable { } #it seems that show total changes some of the others, so the sequence matters. if ($PSBoundParameters.ContainsKey('ShowHeader')) {$tbl.ShowHeader = [bool]$ShowHeader} - if ($PSBoundParameters.ContainsKey('TotalSettings')) { + if ($PSBoundParameters.ContainsKey('TableTotalSettings')) { $tbl.ShowTotal = $true - foreach ($k in $TotalSettings.keys) { + foreach ($k in $TableTotalSettings.keys) { + + # Get the Function to be added in the totals row + if ($TableTotalSettings[$k] -is [HashTable]) { + If ($TableTotalSettings[$k].Keys -contains "Function") { + $TotalFunction = $TableTotalSettings[$k]["Function"] + } + Else { Write-Warning -Message "TableTotalSettings parameter for column '$k' needs a key 'Function'" } + } + else { + $TotalFunction = [String]($TableTotalSettings[$k]) + } + + # Add the totals row if (-not $tbl.Columns[$k]) {Write-Warning -Message "Table does not have a Column '$k'."} - elseif ($TotalSettings[$k] -is [HashTable] -and $TotalSettings[$k].Keys.Count -eq 1 -and $TotalSettings[$k].Keys[0] -eq "Custom") { - $formula = $TotalSettings[$k][($TotalSettings[$k].Keys[0])] | Select -First 1 + elseif ($TotalFunction -match "^=") { ### A function in Excel uses ";" between parameters but the OpenXML parameter separator is "," ### Only replace semicolon when it's NOT somewhere between quotes quotes. # Get all text between quotes - $QuoteMatches = [Regex]::Matches($formula,"`"[^`"]*`"|'[^']*'") + $QuoteMatches = [Regex]::Matches($TotalFunction,"`"[^`"]*`"|'[^']*'") # Create array with all indexes of characters between quotes (and the quotes themselves) $QuoteCharIndexes = $( Foreach ($QuoteMatch in $QuoteMatches) { @@ -69,21 +81,33 @@ function Add-ExcelTable { ) # Get all semicolons - $SemiColonMatches = [Regex]::Matches($formula, ";") + $SemiColonMatches = [Regex]::Matches($TotalFunction, ";") # Replace the semicolons of which the index is not in the list of quote-text indexes Foreach ($SemiColonMatch in $SemiColonMatches.Index) { If ($QuoteCharIndexes -notcontains $SemiColonMatch) { - $formula = $formula.remove($SemiColonMatch,1).Insert($SemiColonMatch,",") + $TotalFunction = $TotalFunction.remove($SemiColonMatch,1).Insert($SemiColonMatch,",") } } # Configure the formula. The TotalsRowFunction is automatically set to "Custom" when the TotalsRowFormula is set. - $tbl.Columns[$k].TotalsRowFormula = $formula + $tbl.Columns[$k].TotalsRowFormula = $TotalFunction } - elseif ($TotalSettings[$k] -notin @("Average", "Count", "CountNums", "Max", "Min", "None", "StdDev", "Sum", "Var") ) { - Write-Warning -Message "'$($TotalSettings[$k])' is not a valid total function." + elseif ($TotalFunction -notin @("Average", "Count", "CountNums", "Max", "Min", "None", "StdDev", "Sum", "Var") ) { + Write-Warning -Message "'$($TotalFunction)' is not a valid total function." + } + else {$tbl.Columns[$k].TotalsRowFunction = $TotalFunction} + + # Set comment on totals row + If ($TableTotalSettings[$k] -is [HashTable] -and $TableTotalSettings[$k].Keys -contains "Comment" -and ![String]::IsNullOrEmpty($TableTotalSettings[$k]["Comment"])) { + $CellCommentParams = @{ + Worksheet = $tbl.WorkSheet + Row = $tbl.Address.End.Row + ColumnNumber = ($tbl.columns | ? { $_.name -eq $k }).Id + Text = $TableTotalSettings[$k]["Comment"] + } + + Set-CellComment @CellCommentParams } - else {$tbl.Columns[$k].TotalsRowFunction = $TotalSettings[$k]} } } elseif ($PSBoundParameters.ContainsKey('ShowTotal')) {$tbl.ShowTotal = [bool]$ShowTotal} diff --git a/Public/Export-Excel.ps1 b/Public/Export-Excel.ps1 index c4d13cb..7d501e0 100644 --- a/Public/Export-Excel.ps1 +++ b/Public/Export-Excel.ps1 @@ -56,7 +56,7 @@ [Alias('Table')] $TableName, [OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6, - [HashTable]$TotalSettings, + [HashTable]$TableTotalSettings, [Switch]$BarChart, [Switch]$PieChart, [Switch]$LineChart , @@ -212,8 +212,8 @@ $row ++ $null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, $false ) if ($TableName -or $PSBoundParameters.ContainsKey('TableStyle')) { - if ($PSBoundParameters.ContainsKey('TotalSettings')) { - Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle -TotalSettings $TotalSettings + if ($PSBoundParameters.ContainsKey('TableTotalSettings')) { + Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings } Else { Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle @@ -430,8 +430,8 @@ if ($null -ne $TableName -or $PSBoundParameters.ContainsKey('TableStyle')) { #Already inserted Excel table if input was a DataTable if ($InputObject -isnot [System.Data.DataTable]) { - if ($PSBoundParameters.ContainsKey('TotalSettings')) { - Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle -TotalSettings $TotalSettings + if ($PSBoundParameters.ContainsKey('TableTotalSettings')) { + Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings } else { Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle