Skip to content

Commit

Permalink
Merge pull request #1341 from dfinke/DoNotShowValues-for-Conditional
Browse files Browse the repository at this point in the history
Add -ShowOnlyIcon to New-ConditionalFormattingIconSet. Base on https:…
  • Loading branch information
dfinke authored Dec 11, 2022
2 parents c848497 + 3c23a06 commit 15679a0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ImportExcel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions Public/Add-ConditionalFormatting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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}
Expand Down
6 changes: 3 additions & 3 deletions Public/Export-Excel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down
20 changes: 11 additions & 9 deletions Public/New-ConditionalFormattingIconSet.ps1
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
66 changes: 66 additions & 0 deletions __tests__/New-ConditionalFormattingIconSet.tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 15679a0

Please sign in to comment.