mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-25 20:53:43 +00:00
Added Import-Html support and examples
This commit is contained in:
38
Get-HtmlTable.ps1
Normal file
38
Get-HtmlTable.ps1
Normal file
@@ -0,0 +1,38 @@
|
||||
function Get-HtmlTable {
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
$url,
|
||||
$tableIndex=0
|
||||
)
|
||||
|
||||
$r = (Invoke-WebRequest $url)
|
||||
$table = $r.ParsedHtml.getElementsByTagName("table")[$tableIndex]
|
||||
$propertyNames = @()
|
||||
$totalRows=@($table.rows).count-1
|
||||
|
||||
for ($idx = 0; $idx -lt $totalRows; $idx++) {
|
||||
|
||||
$row = $table.rows[$idx]
|
||||
$cells = @($row.cells)
|
||||
|
||||
if(!$propertyNames) {
|
||||
if($cells[0].tagName -eq 'th') {
|
||||
$propertyNames = @($cells | foreach {$_.innertext -replace ' ',''})
|
||||
} else {
|
||||
$propertyNames = @(1..($cells.Count + 2) | % { "P$_" })
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
$result = [ordered]@{}
|
||||
|
||||
for($counter = 0; $counter -lt $cells.Count; $counter++) {
|
||||
$propertyName = $propertyNames[$counter]
|
||||
|
||||
if(!$propertyName) { $propertyName= '[missing]'}
|
||||
$result.$propertyName= $cells[$counter].InnerText
|
||||
}
|
||||
|
||||
[PSCustomObject]$result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user