mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-17 16:53:46 +00:00
Changed TotalSettings to TableTotalSettings + Changed the way custom formulas are added + added possibility to add comments tot totals row
This commit is contained in:
@@ -9,7 +9,7 @@ function Add-ExcelTable {
|
|||||||
[Switch]$ShowHeader ,
|
[Switch]$ShowHeader ,
|
||||||
[Switch]$ShowFilter,
|
[Switch]$ShowFilter,
|
||||||
[Switch]$ShowTotal,
|
[Switch]$ShowTotal,
|
||||||
[hashtable]$TotalSettings,
|
[hashtable]$TableTotalSettings,
|
||||||
[Switch]$ShowFirstColumn,
|
[Switch]$ShowFirstColumn,
|
||||||
[Switch]$ShowLastColumn,
|
[Switch]$ShowLastColumn,
|
||||||
[Switch]$ShowRowStripes,
|
[Switch]$ShowRowStripes,
|
||||||
@@ -51,16 +51,28 @@ function Add-ExcelTable {
|
|||||||
}
|
}
|
||||||
#it seems that show total changes some of the others, so the sequence matters.
|
#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('ShowHeader')) {$tbl.ShowHeader = [bool]$ShowHeader}
|
||||||
if ($PSBoundParameters.ContainsKey('TotalSettings')) {
|
if ($PSBoundParameters.ContainsKey('TableTotalSettings')) {
|
||||||
$tbl.ShowTotal = $true
|
$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'."}
|
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") {
|
elseif ($TotalFunction -match "^=") {
|
||||||
$formula = $TotalSettings[$k][($TotalSettings[$k].Keys[0])] | Select -First 1
|
|
||||||
### A function in Excel uses ";" between parameters but the OpenXML parameter separator is ","
|
### A function in Excel uses ";" between parameters but the OpenXML parameter separator is ","
|
||||||
### Only replace semicolon when it's NOT somewhere between quotes quotes.
|
### Only replace semicolon when it's NOT somewhere between quotes quotes.
|
||||||
# Get all text between 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)
|
# Create array with all indexes of characters between quotes (and the quotes themselves)
|
||||||
$QuoteCharIndexes = $(
|
$QuoteCharIndexes = $(
|
||||||
Foreach ($QuoteMatch in $QuoteMatches) {
|
Foreach ($QuoteMatch in $QuoteMatches) {
|
||||||
@@ -69,21 +81,33 @@ function Add-ExcelTable {
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Get all semicolons
|
# 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
|
# Replace the semicolons of which the index is not in the list of quote-text indexes
|
||||||
Foreach ($SemiColonMatch in $SemiColonMatches.Index) {
|
Foreach ($SemiColonMatch in $SemiColonMatches.Index) {
|
||||||
If ($QuoteCharIndexes -notcontains $SemiColonMatch) {
|
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.
|
# 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") ) {
|
elseif ($TotalFunction -notin @("Average", "Count", "CountNums", "Max", "Min", "None", "StdDev", "Sum", "Var") ) {
|
||||||
Write-Warning -Message "'$($TotalSettings[$k])' is not a valid total function."
|
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}
|
elseif ($PSBoundParameters.ContainsKey('ShowTotal')) {$tbl.ShowTotal = [bool]$ShowTotal}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
[Alias('Table')]
|
[Alias('Table')]
|
||||||
$TableName,
|
$TableName,
|
||||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
|
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
|
||||||
[HashTable]$TotalSettings,
|
[HashTable]$TableTotalSettings,
|
||||||
[Switch]$BarChart,
|
[Switch]$BarChart,
|
||||||
[Switch]$PieChart,
|
[Switch]$PieChart,
|
||||||
[Switch]$LineChart ,
|
[Switch]$LineChart ,
|
||||||
@@ -212,8 +212,8 @@
|
|||||||
$row ++
|
$row ++
|
||||||
$null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, $false )
|
$null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, $false )
|
||||||
if ($TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
|
if ($TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
|
||||||
if ($PSBoundParameters.ContainsKey('TotalSettings')) {
|
if ($PSBoundParameters.ContainsKey('TableTotalSettings')) {
|
||||||
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle -TotalSettings $TotalSettings
|
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings
|
||||||
}
|
}
|
||||||
Else {
|
Else {
|
||||||
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle
|
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle
|
||||||
@@ -430,8 +430,8 @@
|
|||||||
if ($null -ne $TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
|
if ($null -ne $TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
|
||||||
#Already inserted Excel table if input was a DataTable
|
#Already inserted Excel table if input was a DataTable
|
||||||
if ($InputObject -isnot [System.Data.DataTable]) {
|
if ($InputObject -isnot [System.Data.DataTable]) {
|
||||||
if ($PSBoundParameters.ContainsKey('TotalSettings')) {
|
if ($PSBoundParameters.ContainsKey('TableTotalSettings')) {
|
||||||
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle -TotalSettings $TotalSettings
|
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle
|
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle
|
||||||
|
|||||||
Reference in New Issue
Block a user