From 5ab9d6f23fa037d79bfc23e812de3e424e9b35ee Mon Sep 17 00:00:00 2001 From: James O'Neill Date: Thu, 14 Jul 2022 17:43:55 +0100 Subject: [PATCH] Add column to GroupNumeric/Date in PivotTables. --- Examples/Grouping/GroupDateColumn.ps1 | 13 +++++++ .../{GroupDate.ps1 => GroupDateRow.ps1} | 0 Examples/Grouping/GroupNumericColumn.ps1 | 13 +++++++ .../{GroupNumeric.ps1 => GroupNumericRow.ps1} | 0 Public/Add-PivotTable.ps1 | 12 +++++++ Public/New-PivotTableDefinition.ps1 | 2 ++ README.original.md | 5 +++ mdHelp/en/add-pivottable.md | 32 +++++++++++++++++ mdHelp/en/new-pivottabledefinition.md | 35 +++++++++++++++++-- 9 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 Examples/Grouping/GroupDateColumn.ps1 rename Examples/Grouping/{GroupDate.ps1 => GroupDateRow.ps1} (100%) create mode 100644 Examples/Grouping/GroupNumericColumn.ps1 rename Examples/Grouping/{GroupNumeric.ps1 => GroupNumericRow.ps1} (100%) diff --git a/Examples/Grouping/GroupDateColumn.ps1 b/Examples/Grouping/GroupDateColumn.ps1 new file mode 100644 index 0000000..6ee05cf --- /dev/null +++ b/Examples/Grouping/GroupDateColumn.ps1 @@ -0,0 +1,13 @@ +try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return} + +#Get rid of pre-exisiting sheet +$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx" +Write-Verbose -Verbose -Message "Save location: $xlSourcefile" +Remove-Item $xlSourcefile -ErrorAction Ignore + +$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Points ` + -PivotRows Driver -PivotColumns Date -PivotData @{Points = "SUM"} -GroupDateColumn Date -GroupDatePart Years, Months + +Import-Csv "$PSScriptRoot\First10Races.csv" | + Select-Object Race, @{n = "Date"; e = {[datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture))}}, FinishPosition, Driver, GridPosition, Team, Points | + Export-Excel $xlSourcefile -Show -AutoSize -PivotTableDefinition $PivotTableDefinition \ No newline at end of file diff --git a/Examples/Grouping/GroupDate.ps1 b/Examples/Grouping/GroupDateRow.ps1 similarity index 100% rename from Examples/Grouping/GroupDate.ps1 rename to Examples/Grouping/GroupDateRow.ps1 diff --git a/Examples/Grouping/GroupNumericColumn.ps1 b/Examples/Grouping/GroupNumericColumn.ps1 new file mode 100644 index 0000000..4b97391 --- /dev/null +++ b/Examples/Grouping/GroupNumericColumn.ps1 @@ -0,0 +1,13 @@ +try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return} + +#Get rid of pre-exisiting sheet +$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx" +Write-Verbose -Verbose -Message "Save location: $xlSourcefile" +Remove-Item $xlSourcefile -ErrorAction Ignore + +$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Places ` + -PivotRows Driver -PivotColumns FinishPosition -PivotData @{Date = "Count"} -GroupNumericColumn FinishPosition -GroupNumericMin 1 -GroupNumericMax 25 -GroupNumericInterval 3 + +Import-Csv "$PSScriptRoot\First10Races.csv" | + Select-Object Race, @{n = "Date"; e = {[datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture))}}, FinishPosition, Driver, GridPosition, Team, Points | + Export-Excel $xlSourcefile -Show -AutoSize -PivotTableDefinition $PivotTableDefinition \ No newline at end of file diff --git a/Examples/Grouping/GroupNumeric.ps1 b/Examples/Grouping/GroupNumericRow.ps1 similarity index 100% rename from Examples/Grouping/GroupNumeric.ps1 rename to Examples/Grouping/GroupNumericRow.ps1 diff --git a/Public/Add-PivotTable.ps1 b/Public/Add-PivotTable.ps1 index 1b3f967..ebeea21 100644 --- a/Public/Add-PivotTable.ps1 +++ b/Public/Add-PivotTable.ps1 @@ -18,8 +18,10 @@ [String]$PivotTotals = "Both", [Switch]$NoTotalsInPivot, [String]$GroupDateRow, + [String]$GroupDateColumn, [OfficeOpenXml.Table.PivotTable.eDateGroupBy[]]$GroupDatePart, [String]$GroupNumericRow, + [String]$GroupNumericColumn, [double]$GroupNumericMin = 0 , [double]$GroupNumericMax = [Double]::MaxValue , [double]$GroupNumericInterval = 100 , @@ -139,11 +141,21 @@ if (-not $r ) {Write-Warning -Message "Could not find a Row field named '$GroupNumericRow'; no numeric grouping will be done."} else {$r.AddNumericGrouping($GroupNumericMin, $GroupNumericMax, $GroupNumericInterval)} } + elseif ($GroupNumericColumn) { + $c = $pivotTable.ColumnFields.Where( {$_.name -eq $GroupNumericColumn }) + if (-not $c ) {Write-Warning -Message "Could not find a Column field named '$GroupNumericColumn'; no numeric grouping will be done."} + else {$c.AddNumericGrouping($GroupNumericMin, $GroupNumericMax, $GroupNumericInterval)} + } if ($GroupDateRow -and $PSBoundParameters.ContainsKey("GroupDatePart")) { $r = $pivotTable.RowFields.Where( {$_.name -eq $GroupDateRow }) if (-not $r ) {Write-Warning -Message "Could not find a Row field named '$GroupDateRow'; no date grouping will be done."} else {$r.AddDateGrouping($GroupDatePart)} } + elseif ($GroupDateColumn -and $PSBoundParameters.ContainsKey("GroupDatePart")) { + $c = $pivotTable.ColumnFields.Where( {$_.name -eq $GroupDateColumn }) + if (-not $c ) {Write-Warning -Message "Could not find a Column field named '$GroupDateColumn'; no date grouping will be done."} + else {$c.AddDateGrouping($GroupDatePart)} + } } catch {Write-Warning -Message "Failed adding PivotTable '$pivotTableName': $_"} } diff --git a/Public/New-PivotTableDefinition.ps1 b/Public/New-PivotTableDefinition.ps1 index 884579e..0568cf0 100644 --- a/Public/New-PivotTableDefinition.ps1 +++ b/Public/New-PivotTableDefinition.ps1 @@ -16,8 +16,10 @@ function New-PivotTableDefinition { [String]$PivotTotals = "Both", [Switch]$NoTotalsInPivot, [String]$GroupDateRow, + [String]$GroupDateColumn, [OfficeOpenXml.Table.PivotTable.eDateGroupBy[]]$GroupDatePart, [String]$GroupNumericRow, + [String]$GroupNumericColumn, [double]$GroupNumericMin = 0 , [double]$GroupNumericMax = [Double]::MaxValue , [double]$GroupNumericInterval = 100 , diff --git a/README.original.md b/README.original.md index b614b3b..0c2648d 100644 --- a/README.original.md +++ b/README.original.md @@ -65,6 +65,11 @@ Plus, wiring the [PowerShell ScriptAnalyzer Excel report](https://github.com/dfi ![](.gitbook/assets/ScriptAnalyzerReport.png) + +## What's new for 7.1.4 + +- Added GroupNumericColumn and GroupDateColumn to New-PivotTableDefinition and Add-PivotTable. + ## What's new 7.1.3 - Changed to `ProviderPath`. Thanks [Trevor Walker](https://github.com/sporkabob) diff --git a/mdHelp/en/add-pivottable.md b/mdHelp/en/add-pivottable.md index f060d9a..f24352e 100644 --- a/mdHelp/en/add-pivottable.md +++ b/mdHelp/en/add-pivottable.md @@ -331,6 +331,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -GroupDateColumn + +The name of a Column field which should be grouped by parts of the date/time \(ignored if GroupDateRow is not specified\) + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -GroupDatePart The Part\(s\) of the date to use in the grouping \(ignored if GroupDateRow is not specified\) @@ -364,6 +380,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -GroupNumericColumn + +The name of a Column field which should be grouped by Number \(e.g. 0-99, 100-199, 200-299 \) + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -GroupNumericMin The starting point for grouping diff --git a/mdHelp/en/new-pivottabledefinition.md b/mdHelp/en/new-pivottabledefinition.md index cc96c6e..75b838b 100644 --- a/mdHelp/en/new-pivottabledefinition.md +++ b/mdHelp/en/new-pivottabledefinition.md @@ -225,6 +225,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -GroupDateColumn + +The name of a column field which should be grouped by parts of the date/time \(ignored if GroupDatePart is not specified\) + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -GroupDatePart The Part\(s\) of the date to use in the grouping \(ignored if GroupDateRow is not specified\) @@ -234,7 +250,6 @@ Type: eDateGroupBy[] Parameter Sets: (All) Aliases: Accepted values: Years, Quarters, Months, Days, Hours, Minutes, Seconds - Required: False Position: Named Default value: None @@ -250,7 +265,6 @@ The name of a row field which should be grouped by Number \(e.g 0-99, 100-199, 2 Type: String Parameter Sets: (All) Aliases: - Required: False Position: Named Default value: None @@ -258,6 +272,23 @@ Accept pipeline input: False Accept wildcard characters: False ``` + +### -GroupNumericColumn + +The name of a column field which should be grouped by Number \(e.g 0-99, 100-199, 200-299 \) + +```yaml +Type: String +Parameter Sets: (All) +Aliases: +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + + ### -GroupNumericMin The starting point for grouping