-
Notifications
You must be signed in to change notification settings - Fork 29
/
Format-HtmlDataTable.ps1
77 lines (67 loc) · 2.97 KB
/
Format-HtmlDataTable.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
<#
.SYNOPSIS
Right-aligns numeric data in an HTML table for emailing, and optionally zebra-stripes &c.
.INPUTS
System.String containing an HTML table, as produced by ConvertTo-Html.
.OUTPUTS
System.String containing the data-formatted HTML table.
.NOTES
Assumes only one <tr> element per string piped in, as produced by ConvertTo-Html.
.LINK
ConvertTo-Html
.LINK
https://www.w3.org/Bugs/Public/show_bug.cgi?id=18026
.EXAMPLE
Invoke-Sqlcmd "..." |ConvertFrom-DataRow.ps1 |ConvertTo-Html |Format-HtmlDataTable.ps1
Runs the query, parses each row into an HTML row, then fixes the alignment of numeric cells.
.EXAMPLE
$rows |ConvertTo-Html -Fragment |Format-HtmlDataTable.ps1 'Products' '#F99' '#FFF'
Renders DataRows as an HTML table, right-aligns numeric cells, then adds a caption ("Products"),
and alternates the rows between pale yellow and white.
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
# The HTML table caption, a label for the table.
[Parameter(Position=0)][string]$Caption,
# The background CSS value for odd rows.
[Parameter(Position=1)][string]$OddRowBackground,
# The background CSS value for even rows.
[Parameter(Position=2)][string]$EvenRowBackground,
# Any table attributes desired (cellpadding, cellspacing, style, &c.).
[Alias('TableAtts')][string]$TableAttributes = 'cellpadding="2" cellspacing="0" style="font:x-small ''Lucida Console'',monospace"',
# HTML attributes for the table caption.
[Alias('CaptionAtts','CapAtts')][string]$CaptionAttributes = 'style="font:bold small serif;border:1px inset #DDD;padding:1ex 0;background:#FFF"',
# Applies a standard .NET formatting pattern to numbers, such as N or '#,##0.000;(#,##0.000);zero'.
[string]$NumericFormat,
# The HTML table data to be piped in.
[Parameter(ValueFromPipeline=$true)][string]$Html
)
Begin
{
$odd = $false
if($OddRowBackground) {$OddRowBackground = [Security.SecurityElement]::Escape($OddRowBackground)}
if($EvenRowBackground) {$EvenRowBackground = [Security.SecurityElement]::Escape($EvenRowBackground)}
}
Process
{
$odd = !$odd
$Html =
if($NumericFormat)
{
[regex]::Replace($Html,'<td>([-$]?)(\d+(?:,\d{3})*(?:\.\d+)?)</td>',
{
Param($match)
'<td align="right">',$match.Groups[1].Value,
([decimal]$match.Groups[2].Value).ToString($NumericFormat),'</td>' -join ''
})
}
else {$Html -replace '<td>([-$]?\d+(?:,\d{3})*(?:\.\d+)?)</td>','<td align="right">$1</td>'}
if($Html -like '*<table>*')
{
if($Caption) {$Html = $Html -replace '<table>',"<table><caption $CaptionAttributes>$([Security.SecurityElement]::Escape($Caption))</caption>"}
if($TableAttributes) {$Html = $Html -replace '<table>',"<table $TableAttributes>"}
}
if($odd -and $OddRowBackground) {$Html -replace '^<tr>',"<tr style=`"background:$OddRowBackground`">"}
elseif(!$odd -and $EvenRowBackground) {$Html -replace '^<tr>',"<tr style=`"background:$EvenRowBackground`">"}
else {$Html}
}