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
function New-ConditionalFormattingIconSet {
    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
    }
}