Search-CellValue.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function Search-CellValue {
    <#
    .SYNOPSIS
        Find a value in a spreadsheet
 
    .DESCRIPTION
        Find a value in a spreadsheet
 
        Specify an xlsx path, an ExcelPackage object, or a WorkSheet to search, and a ScriptBlock you want to run the cell values against
 
    .PARAMETER Path
        Path to an xlsx file to search
 
    .PARAMETER Excel
        An ExcelPackage to search
 
    .PARAMETER WorkSheet
        An Excel WorkSheet to search
 
    .PARAMETER WorksheetName
        Optional name of Worksheet to search
 
    .PARAMETER FilterScript
        Scriptblock that we call with Where-Object against every cell value
 
        $_ would refer to the value of the cell.
 
    .PARAMETER As
        How the data should be returned
         
            Default WorksheetName, Row, Column, Match data
            Raw Return the value only
            Passthru Return the cell item
 
    .EXAMPLE
        Search-CellValue -Path C:\Temp\Demo.xlsx {$_ -like 'jsmith*'}
 
        #Find any cell values like jsmith*
 
    .EXAMPLE
        Search-CellValue = Get-ExcelValue -Path C:\Temp\Demo.xlsx {$_ -like 'jsmith*'} -As Passthru
 
        #Returns an OfficeOpenXml.ExcelRangeBase that you can process (e.g. add formatting to manually)
 
    .NOTES
        Thanks to Doug Finke for his example:
            https://github.com/dfinke/ImportExcel/blob/master/ImportExcel.psm1
 
        Thanks to Philip Thompson for an expansive set of examples on working with EPPlus in PowerShell:
            https://excelpslib.codeplex.com/
 
    .LINK
        https://github.com/RamblingCookieMonster/PSExcel
 
    .FUNCTIONALITY
        Excel
    #>

    [cmdletbinding(DefaultParameterSetName = 'Excel')]
    param(
        [parameter( Position = 1,
                    ParameterSetName = 'Excel',
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true)]
        [OfficeOpenXml.ExcelPackage]$Excel,

        [parameter( Position = 1,
                    ParameterSetName = 'File',
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true)]
        [validatescript({Test-Path $_})]
        [string]$Path,

        [parameter( Position = 1,
                    ParameterSetName = 'Worksheet',
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true)]
        [OfficeOpenXml.ExcelWorksheet]$WorkSheet,

        [parameter( Mandatory = $True,
                    Position = 0)]
        [scriptblock]$FilterScript,
       
        $WorkSheetName,

        [validateset('Default','Raw','Passthru')]
        [string]$As = 'Default'
    )
    
    Process
    {
        Write-Verbose "PSBoundParameters: $($PSBoundParameters | Out-String)"    
        $WSParam = @{}
        if($PSBoundParameters.ContainsKey( 'WorkSheetName') )
        {
            $WSParam.Add('Name',$WorkSheetName)
        }
        Try
        {
            switch ($PSCmdlet.ParameterSetName)
            {
                'Excel'
                {
                    $WorkSheets = @( $Excel | Get-Worksheet @WSParam -ErrorAction Stop )
                }
                'File'
                {
                    $WorkSheets = @( New-Excel -Path $Path -ErrorAction Stop | Get-Worksheet @WSParam -ErrorAction Stop )
                }
                'Worksheet'
                {
                    $WorkSheets = @( $WorkSheet )
                }
            }
        }
        Catch
        {
            Throw "Could not get worksheets to search: $_"
        }

        If($WorkSheets.Count -eq 0)
        {
            Throw "Something went wrong, we didn't find a worksheet"
        }

        Foreach($Sheet in $WorkSheets)
        {
            $Dimension = $Sheet.Dimension
            if($IgnoreHeader)
            {
                $RowStart = 2
            }
            else
            {
                $RowStart = $Dimension.Start.Row
            }
            $ColumnStart = $Dimension.Start.Column
            $RowEnd = $Dimension.End.Row
            $ColumnEnd = $Dimension.End.Column
            
            Write-Verbose "Searching $($Sheet.Name) over coordinates $RowStart, $ColumnStart through $RowEnd, $ColumnEnd"

            for ($Row = $RowStart; $Row -le $RowEnd; $Row++)
            {
                for ($Column = $ColumnStart; $Column -le $ColumnEnd; $Column++)
                {
                    $Value = $Sheet.Cells.Item($Row, $Column).Value
                    
                    #Handle dates, they're too common to overlook... Could use help, not sure if this is the best regex to use?
                    $Format = $Sheet.Cells.Item($Row, $Column).style.numberformat.format
                    if($Format -match '\w{1,4}/\w{1,2}/\w{1,4}( \w{1,2}:\w{1,2})?')
                    {
                        Try
                        {
                            $Value = [datetime]::FromOADate($Value)
                        }
                        Catch
                        {
                            Write-Verbose "Error converting '$Value' to datetime"
                        }
                    }

                    $Data = $Null
                    $Data = $Value | Where-Object -FilterScript $FilterScript
                    if($Data -ne $Null)
                    {
                        Switch ($As)
                        {
                            'Raw'
                            {
                                $Data
                            }
                            'Default'
                            {
                                New-Object -TypeName PSObject -Property @{
                                    WorksheetName = $Sheet.Name
                                    Row = $Row
                                    Column = $Column
                                    Match = $Data
                                } | Select WorkSheetName, Row, Column, Match
                            }
                            'Passthru'
                            {
                                $Sheet.Cells.Item($Row, $Column)
                            }
                        }
                    }
                }
            }

            if($PSBoundParameters.ContainsKey('Path'))
            {
                $Sheet.Dispose()
            }
        }
    }
}