powershell - How can I use convertto-html to create an HTML table with dynamic columns? -
how can use convertto-html create html table dynamic columns?
data below (number of columns , names vary):
$columnheadings = $("col1","col2","col3") $tablerows = @(("a1","b1","c1"),("a2","b2","c2"),("a3","b3","c3"),("a4","b4","c4"))
i have tried things below:
$htmlrows = $tablerows | convertto-html $_ $tablerows | foreach-object {add-member -inputobject $_ -type noteproperty -name value -value $_; $_} | convertto-html -fragment -property value
please let me know if more info required give answer.
edit: found answer myself (below), leaving question&answer here in case forget, or else needs it.
i managed put bit of code solves issue, columns out of order in html-table.. wonder why..? edit: found it, adjusted code below ([ordered]).
also, can remove colgroup tags?
$columnheadings = $("col1","col2","col3") $tablerows = @(("a1","b1","c1"),("a2","b2","c2"),("a3","b3","c3"), ("a4","b4","c4")) $columns = $columnheadings.count $htmlrows = $tablerows | where-object{$_} | foreach-object { $row = [ordered]@{} for($rowindex = 0;$rowindex -lt $columns;$rowindex++) { $h = $columnheadings[$rowindex] $row.add($h,$_[$rowindex]) } [pscustomobject]$row } | convertto-html -fragment
gives neat table:
<table> <colgroup><col/><col/><col/></colgroup> <tr><th>col1</th><th>col2</th><th>col3</th></tr> <tr><td>a1</td><td>b1</td><td>c1</td></tr> <tr><td>a2</td><td>b2</td><td>c2</td></tr> <tr><td>a3</td><td>b3</td><td>c3</td></tr> <tr><td>a4</td><td>b4</td><td>c4</td></tr> </table>
Comments
Post a Comment