New-ConditionalFormattingIconSet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function New-ConditionalFormattingIconSet {
    <#
      .SYNOPSIS
        Creates an object which describes a conditional formatting rule a for 3,4 or 5 icon set.
      .DESCRIPTION
        Export-Excel takes a -ConditionalFormat parameter which can hold one or more descriptions for conditional formats;
        this command builds the defintion of a Conditional formatting rule for an icon set.
      .PARAMETER Range
        The range of cells that the conditional format applies to.
      .PARAMETER ConditionalFormat
        The type of rule: one of "ThreeIconSet","FourIconSet" or "FiveIconSet"
      .PARAMETER IconType
        The name of an iconSet - different icons are available depending on whether 3,4 or 5 icon set is selected.
      .PARAMETER Reverse
        Use the icons in the reverse order.
      .Example
        $cfRange = [OfficeOpenXml.ExcelAddress]::new($topRow, $column, $lastDataRow, $column)
        $cfdef = New-ConditionalFormattingIconSet -Range $cfrange -ConditionalFormat ThreeIconSet -IconType Arrows
        Export-Excel -ExcelPackage $excel -ConditionalFormat $cfdef -show
 
        The first line creates a range - one column wide in the column $column, running
        from $topRow to $lastDataRow.
        The second line creates a definition object using this range
        and the third uses Export-Excel with an open package to apply the format and
        save and open the file.
      .Link
        Add-Add-ConditionalFormatting
        New-ConditionalText
    #>

    param(
        [Parameter(Mandatory=$true)]
        $Range,
        [ValidateSet("ThreeIconSet","FourIconSet","FiveIconSet")]
        $ConditionalFormat,
        [Switch]$Reverse
    )

    DynamicParam {
        $IconType = New-Object System.Management.Automation.ParameterAttribute
        $IconType.Position = 2
        $IconType.Mandatory = $true

        $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

        $attributeCollection.Add($IconType)

        switch ($ConditionalFormat) {
            "ThreeIconSet" {
                $IconTypeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('IconType', [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting3IconsSetType], $attributeCollection)
            }

            "FourIconSet" {
                $IconTypeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('IconType', [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting4IconsSetType], $attributeCollection)
            }

            "FiveIconSet" {
                $IconTypeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('IconType', [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting5IconsSetType], $attributeCollection)
            }
        }

        $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        $paramDictionary.Add('IconType', $IconTypeParam)

        return $paramDictionary
    }

    End {

        $bp = @{}+$PSBoundParameters

        $obj = [PSCustomObject]@{
            Range     = $Range
            Formatter = $ConditionalFormat
            IconType  = $bp.IconType
            Reverse   = $Reverse
        }

        $obj.pstypenames.Clear()
        $obj.pstypenames.Add("ConditionalFormatIconSet")

        $obj
    }
}