Get-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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
function Get-CellValue { <# .SYNOPSIS Get cell data from Excel .DESCRIPTION Get cell data from Excel .PARAMETER Path Path to an xlsx file to get cells from .PARAMETER Excel An ExcelPackage to get cells from .PARAMETER WorkSheet An Excel WorkSheet to get cells from .PARAMETER WorksheetName Optional name of Worksheet to get cells from .PARAMETER Header Replacement headers. Must match order and count of your data's columns .PARAMETER Coordinates Excel style coordinates specifying starting cell and final cell (e.g. A1:B2) If not specified, we get the dimension for the worksheet and return everything .EXAMPLE Get-CellValue -Path C:\temp\Demo.xlsx -Coordinates A2:A2 #Get the value at column 1, row 2 .EXAMPLE Get-CellValue -Path C:\temp\Demo.xlsx -Coordinates A2:B3 -Header One, Two #Get the values from cells in column one, row two through column two, row three. Replace headers with One, Two .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()] 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, [validatescript({ if( $_ -match "^[a-zA-Z]+[0-9]+:[a-zA-Z]+[0-9]+$" ) { $True } else { Throw "'$_' is not a valid coordinate. See help for 'Coordinates' parameter" } })] [string]$Coordinates, $WorkSheetName, [string[]]$Header ) 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($Worksheet in $WorkSheets) { Write-Verbose "Working with worksheet $($Worksheet.Name)" if($PSBoundParameters.ContainsKey('Coordinates')) { Try { $CellRange = $WorkSheet.Cells.item($Coordinates) } Catch { Write-Error "Could not get cells from '$($WorkSheet.Name)' for coordinates '$Coordinates'" Continue } } else { $CellRange = $Worksheet.Cells $Coordinates = $WorkSheet.Dimension.Address } $ColumnStart = ($($Coordinates -split ":")[0] -replace "[0-9]", "").ToUpperInvariant() $ColumnEnd = ($($Coordinates -split ":")[1] -replace "[0-9]", "").ToUpperInvariant() [int]$RowStart = $($Coordinates -split ":")[0] -replace "[a-zA-Z]", "" [int]$RowEnd = $($Coordinates -split ":")[1] -replace "[a-zA-Z]", "" Function Get-ExcelColumnInt { # http://stackoverflow.com/questions/667802/what-is-the-algorithm-to-convert-an-excel-column-letter-into-its-number [cmdletbinding()] param($ColumnName) [int]$Sum = 0 for ($i = 0; $i -lt $ColumnName.Length; $i++) { $sum *= 26 $sum += ($ColumnName[$i] - 65 + 1) } $sum Write-Verbose "Translated $ColumnName to $Sum" } $ColumnStart = Get-ExcelColumnInt $ColumnStart $ColumnEnd = Get-ExcelColumnInt $ColumnEnd $Columns = $ColumnEnd - $ColumnStart + 1 if($Header -and $Header.count -gt 0) { if($Header.count -ne $Columns) { Write-Error "Found '$columns' columns, provided $($header.count) headers. You must provide a header for every column." } } else { $Header = @( foreach ($Column in $ColumnStart..$ColumnEnd) { $worksheet.Cells.Item(1,$Column).Value } ) } [string[]]$SelectedHeaders = @( $Header | select -Unique ) Write-Verbose "Found headers $Header" #Skip headers... if($RowStart -eq 1 -and $RowEnd -ne 1) { $RowStart += 1 } foreach($Row in ($RowStart)..$RowEnd) { $RowData = @{} $HeaderCol = 0 foreach($Column in $ColumnStart..$ColumnEnd) { $Name = $Header[$HeaderCol] $Value = $WorkSheet.Cells.Item($Row,$Column).Value $HeaderCol++ Write-Debug "Row: $Row, Column: $Column, HeaderCol: $HeaderCol, Name: $Name, Value = $Value" #Handle dates, they're too common to overlook... Could use help, not sure if this is the best regex to use? $Format = $WorkSheet.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" } } if($RowData.ContainsKey($Name) ) { Write-Warning "Duplicate header for '$Name' found, with value '$Value', in row $Row" } else { $RowData.Add($Name, $Value) } } New-Object -TypeName PSObject -Property $RowData | Select -Property $SelectedHeaders } } } } |