Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Examples/Example-ConvertFromHtmlTable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$html = (Invoke-WebRequest -Uri 'https://en.wikipedia.org/wiki/PowerShell').Content
$tmp = ConvertFrom-HtmlTable -Content $html

# If converting multiple tables, the output will look funky
# since it is creating an array of different objects.
$tmp[0] | Format-Table -AutoSize
$tmp[1] | Format-Table -AutoSize
$tmp[2] | Format-Table -AutoSize

# ... etc
54 changes: 54 additions & 0 deletions Public/ConvertFrom-HtmlTable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Function ConvertFrom-HtmlTable {
[cmdletbinding()]
param (
[Parameter(
Mandatory = $true
)]
[string]$Content
)
Begin {
# Initialize the parser
$HTMLParser = [AngleSharp.Html.Parser.HtmlParser]::new()
}
Process {
# Load the html
$ParsedDocument = $HTMLParser.ParseDocument($content)

# Get all the tables
[Array] $Tables = $ParsedDocument.GetElementsByTagName('table')

# For each table
:table foreach ($table in $tables) {

# Get the headers / Where-Object is nessecary to get rid of empty values
$headers = $table.Rows[0].Cells.TextContent.Trim() | Where-Object { $_ }

# if headers have value
if ($null -ne $headers) {
[Array] $output = foreach ($row in $table.Rows | Select-Object -Skip 0) {

# If there aren't as many cells as headers, skip this table
if (@($row.Cells).count -ne $headers.count) {
Write-Verbose 'Unsupported table.'
Continue table
}
$obj = [ordered]@{ }

# add all the properties, one per row
for ($x = 0; $x -lt $headers.count; $x++) {
#$obj | Add-Member -MemberType NoteProperty -Name $headers[$x] -Value $row.Cells[$x].TextContent.Trim()
$obj["$($headers[$x])"] = $row.Cells[$x].TextContent.Trim()
}
[PSCustomObject] $obj
}
# if there are any rows, output
if ($output.count -ge 1) {
@(, $output)
} else {
Write-Verbose 'Table has no rows'
}
}
}
}
End { }
}