From 3c23a064bce2f366628c0ceda5a17cbfbfb46e76 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 10 Dec 2022 18:22:36 -0500 Subject: [PATCH] Add -ShowOnlyIcon to New-ConditionalFormattingIconSet. Base on https://github.com/dfinke/ImportExcel/discussions/1340 --- .../ConditionalFormattingIcontSetOnlyIcon.ps1 | 22 +++++++ ImportExcel.psd1 | 2 +- Public/Add-ConditionalFormatting.ps1 | 4 ++ Public/Export-Excel.ps1 | 6 +- Public/New-ConditionalFormattingIconSet.ps1 | 20 +++--- ...New-ConditionalFormattingIconSet.tests.ps1 | 66 +++++++++++++++++++ changelog.md | 3 + 7 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 Examples/ConditionalFormatting/ConditionalFormattingIcontSetOnlyIcon.ps1 create mode 100644 __tests__/New-ConditionalFormattingIconSet.tests.ps1 diff --git a/Examples/ConditionalFormatting/ConditionalFormattingIcontSetOnlyIcon.ps1 b/Examples/ConditionalFormatting/ConditionalFormattingIcontSetOnlyIcon.ps1 new file mode 100644 index 00000000..619d8641 --- /dev/null +++ b/Examples/ConditionalFormatting/ConditionalFormattingIcontSetOnlyIcon.ps1 @@ -0,0 +1,22 @@ +try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return } + +$data = ConvertFrom-Csv @" +Region,State,Other,Units,Price,InStock +West,Texas,1,927,923.71,1 +North,Tennessee,3,466,770.67,0 +East,Florida,0,1520,458.68,1 +East,Maine,1,1828,661.24,0 +West,Virginia,1,465,053.58,1 +North,Missouri,1,436,235.67,1 +South,Kansas,0,214,992.47,1 +North,North Dakota,1,789,640.72,0 +South,Delaware,-1,712,508.55,1 +"@ + +$xlfile = "$PSScriptRoot\test.xlsx" +Remove-Item $xlfile -ErrorAction SilentlyContinue + +$cfi1 = New-ConditionalFormattingIconSet -Range C:C -ConditionalFormat ThreeIconSet -IconType Symbols -ShowIconOnly +$cfi2 = New-ConditionalFormattingIconSet -Range F:F -ConditionalFormat ThreeIconSet -IconType Symbols2 -ShowIconOnly + +$data | Export-Excel $xlfile -AutoSize -ConditionalFormat $cfi1, $cfi2 -Show \ No newline at end of file diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index d37aa3fe..a4deae13 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -6,7 +6,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. - ModuleVersion = '7.8.3' + ModuleVersion = '7.8.4' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' diff --git a/Public/Add-ConditionalFormatting.ps1 b/Public/Add-ConditionalFormatting.ps1 index e507c86f..bab408c5 100644 --- a/Public/Add-ConditionalFormatting.ps1 +++ b/Public/Add-ConditionalFormatting.ps1 @@ -23,6 +23,7 @@ [Parameter(ParameterSetName = "FourIconSet")] [Parameter(ParameterSetName = "FiveIconSet")] [switch]$Reverse, + [switch]$ShowIconOnly, [Parameter(ParameterSetName = "NamedRule",Position = 2)] $ConditionValue, [Parameter(ParameterSetName = "NamedRule",Position = 3)] @@ -84,6 +85,9 @@ elseif ($PSBoundParameters.ContainsKey("FourIconsSet" ) ) {$rule = $Worksheet.ConditionalFormatting.AddFourIconSet( $Address , $FourIconsSet )} elseif ($PSBoundParameters.ContainsKey("FiveIconsSet" ) ) {$rule = $Worksheet.ConditionalFormatting.AddFiveIconSet( $Address , $FiveIconsSet )} else {$rule = ($Worksheet.ConditionalFormatting)."Add$RuleType"($Address ) } + If($ShowIconOnly) { + $rule.ShowValue = $false + } if ($Reverse) { if ($rule.type -match 'IconSet$' ) {$rule.reverse = $true} elseif ($rule.type -match 'ColorScale$') {$temp =$rule.LowValue.Color ; $rule.LowValue.Color = $rule.HighValue.Color; $rule.HighValue.Color = $temp} diff --git a/Public/Export-Excel.ps1 b/Public/Export-Excel.ps1 index 1a11d464..9300a58a 100644 --- a/Public/Export-Excel.ps1 +++ b/Public/Export-Excel.ps1 @@ -637,9 +637,9 @@ } elseif ($c.formatter) { switch ($c.formatter) { - "ThreeIconSet" { Add-ConditionalFormatting -Worksheet $ws -ThreeIconsSet $c.IconType -range $c.range -reverse:$c.reverse } - "FourIconSet" { Add-ConditionalFormatting -Worksheet $ws -FourIconsSet $c.IconType -range $c.range -reverse:$c.reverse } - "FiveIconSet" { Add-ConditionalFormatting -Worksheet $ws -FiveIconsSet $c.IconType -range $c.range -reverse:$c.reverse } + "ThreeIconSet" { Add-ConditionalFormatting -Worksheet $ws -ThreeIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly} + "FourIconSet" { Add-ConditionalFormatting -Worksheet $ws -FourIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly} + "FiveIconSet" { Add-ConditionalFormatting -Worksheet $ws -FiveIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly} } Write-Verbose -Message "Added conditional formatting to range $($c.range)" } diff --git a/Public/New-ConditionalFormattingIconSet.ps1 b/Public/New-ConditionalFormattingIconSet.ps1 index 18ff937a..26fde957 100644 --- a/Public/New-ConditionalFormattingIconSet.ps1 +++ b/Public/New-ConditionalFormattingIconSet.ps1 @@ -1,11 +1,12 @@ function New-ConditionalFormattingIconSet { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '',Justification='Does not change system State')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Does not change system State')] param( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] $Range, - [ValidateSet("ThreeIconSet","FourIconSet","FiveIconSet")] + [ValidateSet("ThreeIconSet", "FourIconSet", "FiveIconSet")] $ConditionalFormat, - [Switch]$Reverse + [Switch]$Reverse, + [Switch]$ShowIconOnly ) DynamicParam { @@ -40,13 +41,14 @@ function New-ConditionalFormattingIconSet { End { - $bp = @{}+$PSBoundParameters + $bp = @{} + $PSBoundParameters $obj = [PSCustomObject]@{ - Range = $Range - Formatter = $ConditionalFormat - IconType = $bp.IconType - Reverse = $Reverse + Range = $Range + Formatter = $ConditionalFormat + IconType = $bp.IconType + Reverse = $Reverse + ShowIconOnly = $ShowIconOnly } $obj.pstypenames.Clear() diff --git a/__tests__/New-ConditionalFormattingIconSet.tests.ps1 b/__tests__/New-ConditionalFormattingIconSet.tests.ps1 new file mode 100644 index 00000000..685f20bd --- /dev/null +++ b/__tests__/New-ConditionalFormattingIconSet.tests.ps1 @@ -0,0 +1,66 @@ +if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { + Import-Module $PSScriptRoot\..\ImportExcel.psd1 +} + +Describe "Test New Conditional Formatting IconSet" -Tag ConditionalFormattingIconSet { + BeforeEach { + $xlFilename = "TestDrive:\ConditionalFormattingIconSet.xlsx" + Remove-Item $xlFilename -ErrorAction SilentlyContinue + + $data = ConvertFrom-Csv @" +Region,State,Other,Units,Price,InStock +West,Texas,1,927,923.71,1 +North,Tennessee,3,466,770.67,0 +East,Florida,0,1520,458.68,1 +East,Maine,1,1828,661.24,0 +West,Virginia,1,465,053.58,1 +North,Missouri,1,436,235.67,1 +South,Kansas,0,214,992.47,1 +North,North Dakota,1,789,640.72,0 +South,Delaware,-1,712,508.55,1 +"@ + } + + It "Should set ThreeIconSet" { + # $cfi1 = New-ConditionalFormattingIconSet -Range C:C -ConditionalFormat ThreeIconSet -IconType Symbols -ShowIconOnly + $cfi1 = New-ConditionalFormattingIconSet -Range C:C -ConditionalFormat ThreeIconSet -IconType Symbols + + $data | Export-Excel $xlFilename -ConditionalFormat $cfi1 + $actual = Import-Excel $xlFilename + $actual.count | Should -Be 9 + + $xl = Open-ExcelPackage $xlFilename + $xl.Workbook.Worksheets.Count | Should -Be 1 + $targetSheet = $xl.Workbook.Worksheets[1] + + $targetSheet.Name | Should -Be "Sheet1" + $targetSheet.ConditionalFormatting.Count | Should -Be 1 + $targetSheet.ConditionalFormatting[0].Type | Should -Be "ThreeIconSet" + $targetSheet.ConditionalFormatting[0].IconSet | Should -Be "Symbols" + $targetSheet.ConditionalFormatting[0].Reverse | Should -BeFalse + $targetSheet.ConditionalFormatting[0].ShowValue | Should -BeTrue + + Close-ExcelPackage $xl -NoSave + } + + It "Should set ThreeIconSet with ShowOnlyIcon" { + $cfi1 = New-ConditionalFormattingIconSet -Range C:C -ConditionalFormat ThreeIconSet -IconType Symbols -ShowIconOnly + + $data | Export-Excel $xlFilename -ConditionalFormat $cfi1 + $actual = Import-Excel $xlFilename + $actual.count | Should -Be 9 + + $xl = Open-ExcelPackage $xlFilename + $xl.Workbook.Worksheets.Count | Should -Be 1 + $targetSheet = $xl.Workbook.Worksheets[1] + + $targetSheet.Name | Should -Be "Sheet1" + $targetSheet.ConditionalFormatting.Count | Should -Be 1 + $targetSheet.ConditionalFormatting[0].Type | Should -Be "ThreeIconSet" + $targetSheet.ConditionalFormatting[0].IconSet | Should -Be "Symbols" + $targetSheet.ConditionalFormatting[0].Reverse | Should -BeFalse + $targetSheet.ConditionalFormatting[0].ShowValue | Should -BeFalse + + Close-ExcelPackage $xl -NoSave + } +} \ No newline at end of file diff --git a/changelog.md b/changelog.md index 7ea009a2..2bb33722 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # 7.8.3 +- Add -ShowOnlyIcon to `New-ConditionalFormattingIconSet` does not show data in the cell, just the icon. Based on this discussion https://github.com/dfinke/ImportExcel/discussions/1340 +# 7.8.3 + Thanks [Thomas Hofkens](https://github.com/thkn-hofa) - Extended Export-Excel with parameter TableTotalSettings