diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 07636a9..2b67bde 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -1260,6 +1260,8 @@ function Add-ExcelTable { [Switch]$ShowFilter, #Show total adds a totals row. This does not automatically sum the columns but provides a drop-down in each to select sum, average etc [Switch]$ShowTotal, + #Hashtable in the form ColumnName = "Average"|"Count"|"CountNums"|"Max"|"Min"|"None"|"StdDev"|"Sum"|"Var" - if specified ShowTotal is not needed. + [hashtable]$TotalSettings, #Highlights the first column in bold [Switch]$ShowFirstColumn, #Highlights the last column in bold @@ -1292,14 +1294,25 @@ function Add-ExcelTable { Write-Verbose -Message "Defined table '$TableName' at $($Range.Address)" } #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('ShowTotal')) {$tbl.ShowTotal = [bool]$ShowTotal} - if ($PSBoundParameters.ContainsKey('ShowFilter')) {$tbl.ShowFilter = [bool]$ShowFilter} - if ($PSBoundParameters.ContainsKey('ShowFirstColumn')) {$tbl.ShowFirstColumn = [bool]$ShowFirstColumn} - if ($PSBoundParameters.ContainsKey('ShowLastColumn')) {$tbl.ShowLastColumn = [bool]$ShowLastColumn} - if ($PSBoundParameters.ContainsKey('ShowRowStripes')) {$tbl.ShowRowStripes = [bool]$ShowRowStripes} - if ($PSBoundParameters.ContainsKey('ShowColumnStripes')) {$tbl.ShowColumnStripes = [bool]$ShowColumnStripes} - if ($PSBoundParameters.ContainsKey('TableStyle')) {$tbl.TableStyle = $TableStyle} + if ($PSBoundParameters.ContainsKey('ShowHeader')) {$tbl.ShowHeader = [bool]$ShowHeader} + if ($PSBoundParameters.ContainsKey('TotalSettings')) { + $tbl.ShowTotal = $true + foreach ($k in $TotalSettings.keys) { + if (-not $tbl.Columns[$k]) {Write-Warning -Message "Table does not have a Column '$k'."} + elseif ($TotalSettings[$k] -notin @("Average", "Count", "CountNums", "Max", "Min", "None", "StdDev", "Sum", "Var") ) { + Write-wanring "'$($TotalSettings[$k])' is not a valid total function." + } + else {$tbl.Columns[$k].TotalsRowFunction = $TotalSettings[$k]} + } + } + elseif ($PSBoundParameters.ContainsKey('ShowTotal')) {$tbl.ShowTotal = [bool]$ShowTotal} + if ($PSBoundParameters.ContainsKey('ShowFilter')) {$tbl.ShowFilter = [bool]$ShowFilter} + if ($PSBoundParameters.ContainsKey('ShowFirstColumn')) {$tbl.ShowFirstColumn = [bool]$ShowFirstColumn} + if ($PSBoundParameters.ContainsKey('ShowLastColumn')) {$tbl.ShowLastColumn = [bool]$ShowLastColumn} + if ($PSBoundParameters.ContainsKey('ShowRowStripes')) {$tbl.ShowRowStripes = [bool]$ShowRowStripes} + if ($PSBoundParameters.ContainsKey('ShowColumnStripes')) {$tbl.ShowColumnStripes = [bool]$ShowColumnStripes} + if ($PSBoundParameters.ContainsKey('TableStyle')) {$tbl.TableStyle = $TableStyle} + if ($PassThru) {return $tbl} } catch {Write-Warning -Message "Failed adding table '$TableName' to worksheet '$WorksheetName': $_"} diff --git a/README.md b/README.md index 20bd5cb..ff759a7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Install-Module ImportExcel - `Add-ConditionalFormatting`: Now wraps those strings in quotes when needed (for = <= >= string needs to be in double quotes). Parameter intellisense has been improved. There are new parameters: -StopIfTrue and -Priority and support for using the -Reverse parameter with Color-scale rules. Booleans in the sheet are now supported as the value for a condition. Also brought the two different kinds of condition together inside export-excel, and fixed a bug where named-ranges didn't work in some places. In `New-ConditionalText`, more types of conditional format are supported, and -ConditionalTextColor now has an argument completer - Improved handling of hyperlinks in `Export-Excel`. - `Export-Excel` has better checking of Table and PivotTable names (for uniqueness) and a new test in quick charts that there is suitable data for charting. -- Moved logic for adding a named range out of Export-Excel and into a new function named `Add-ExcelName`, and logic for adding a table into a function named `Add-ExcelTable`; this is to make it easier to do these things independently of Export-Excel, but minimize duplication. The Add-ExcelTable command has extra parameters to toggle the options from table tools toolbar (show totals etc) +- Moved logic for adding a named range out of Export-Excel and into a new function named `Add-ExcelName`, and logic for adding a table into a function named `Add-ExcelTable`; this is to make it easier to do these things independently of Export-Excel, but minimize duplication. The Add-ExcelTable command has extra parameters to toggle the options from table tools toolbar (show totals etc) and set options in the totals row. - Moved PivotTable Functions out of Export-Excel.PS1 into their own file and moved Add-ExcelChart out of Export-Excel.ps1 into New-ExcelChart.ps1 - Fixed issues where formatting could be reset when using Export-Excel to manipulate an existing sheet without appending data; this applied to number-formats and tables. - `Add-PivotTable` has some new parameters -PassThru returns the pivot table (e.g. to allow names /sort orders of data series to be tweaked ) -Address parameter allows Pivot to be placed on an existing sheet; -PivotTableStyle allows a change from "Medium6", -PivotNumberFormat sets data cells. Chart creation has been moved out to Add-ExcelChart, and -PivotChartDefinition allows a defintion created with New-ExcelChartDefinition to be used with a PivotTable. This opens up all the things that Add-Excel chart can do without duplicating the parameters on Add-Pivot table and Export-Excel. Definition, TableStyle, Numberformat and ChartDefiniton can be used in `New-PivotTableDefinition` . diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index aeaa6c8..d0b8f08 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -308,10 +308,19 @@ Describe "Table Formatting" { Remove-Item $path $excel = $data2 | Export-excel -path $path -WorksheetName Hardware -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru $ws = $excel.Workbook.Worksheets[1] + Add-ExcelTable -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -TotalSettings @{"Total"="Sum"} -ShowFirstColumn -ShowFilter:$false + Set-Column -Worksheet $ws -Column 4 -NumberFormat 'Currency' Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency' - Add-ExcelTable -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -ShowTotal -ShowFirstColumn -ShowFilter:$false + $PtDef =New-PivotTableDefinition -PivotTableName Totals -PivotRows Product -PivotData @{"Total"="Sum"} -PivotNumberFormat Currency -PivotTotals None -PivotTableSyle Dark2 + Export-excel -ExcelPackage $excel -WorksheetName Hardware -PivotTableDefinition $PtDef + Remove-Item $path + $excel = $data2 | Export-excel -path $path -WorksheetName Hardware -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru + $ws = $excel.Workbook.Worksheets[1] + $Table = Add-ExcelTable -PassThru -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -TotalSettings @{"Total"="Sum"} -ShowFirstColumn -ShowFilter:$false + Set-Column -Worksheet $ws -Column 4 -NumberFormat 'Currency' + Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency' $PtDef =New-PivotTableDefinition -PivotTableName Totals -PivotRows Product -PivotData @{"Total"="Sum"} -PivotNumberFormat Currency -PivotTotals None -PivotTableSyle Dark2 Export-excel -ExcelPackage $excel -WorksheetName Hardware -PivotTableDefinition $PtDef $excel= Open-ExcelPackage -Path $path @@ -325,10 +334,10 @@ Describe "Table Formatting" { $ws1.Tables[0].ShowFirstColumn | should be $true $ws1.Tables[0].ShowLastColumn | should not be $true $ws1.Tables[0].ShowTotal | should be $true + $ws1.Tables[0].Columns["Total"].TotalsRowFunction | Should be "Sum" $ws1.Tables[0].StyleName | should be "TableStyleLight1" $ws1.Cells["D4"].Style.Numberformat.Format | Should match ([regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol)) $ws1.Cells["E5"].Style.Numberformat.Format | Should match ([regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol)) - $ws1.Cells["E5"].Style.Numberformat.Format | Should match ([regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol)) } it "Set the Pivot Options " { $ws2.PivotTables[0].DataFields[0].Format | Should match ([regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol))