Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active March 22, 2019 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chirishman/b2cdf13fcc4beb6335acf3d6afb93e14 to your computer and use it in GitHub Desktop.
Save Chirishman/b2cdf13fcc4beb6335acf3d6afb93e14 to your computer and use it in GitHub Desktop.
Function to allow advanced table styling in HTML based emails opened in Outlook
Function Convertto-OutlookHTMLTable {
Param(
[Parameter(ParameterSetName='CSVInput')]
[string]$CSVFile,
[Parameter(ParameterSetName='CSVInput')]
[string]$Delimiter = ',',
[Parameter(ParameterSetName='InputObject')]
[object]$InputObject,
[string]$CSS,
[string]$CSSFile
)
#region CSSSetup
if ($CSS) {
$csssplit = $Css -split '\n|\r' | ?{$_}
} elseif ($CSSFile){
$csssplit = Get-Content $CSSFile | ?{$_}
} else {
Write-Error -Message 'No CSS Specified' -ErrorAction Stop
}
# Since css/html in outlook is bad (https://msdn.microsoft.com/en-us/library/aa338201(v=office.12).aspx)
# The below code takes the css for col(n) and "tbody tr:nth-child(odd/even)" and tries to combine them
# The priority is col > tr
If($csssplit -contains "tbody tr:nth-child(odd) {"){
$i = 0
$s = 0
$e = 0
$csssplit | ForEach-Object{
$i++
If($_.Trim() -eq "tbody tr:nth-child(odd) {"){
$s = $i
}
If(($e -lt $s) -and ($_.Trim() -eq "}")){
$e = $i-2
}
}
$oddstyle = "style=`"" + ($csssplit[$s..$e] -join '') + '"'
}
If($csssplit -contains "tbody tr:nth-child(even) {"){
$i = 0
$s = 0
$e = 0
$csssplit | ForEach-Object{
$i++
If($_.Trim() -eq "tbody tr:nth-child(even) {"){
$s = $i
}
If(($e -lt $s) -and ($_.Trim() -eq "}")){
$e = $i-2
}
}
$evenstyle = "style=`"" + ($csssplit[$s..$e] -join '') + '"'
}
#endregion CSSSetup
#region CSVSetup
if ($PSCmdlet.ParameterSetName -eq 'CSVInput'){
$InputObject = Import-Csv $CSVFile -Delimiter $Delimiter
}
$Columns = $InputObject[0].PSObject.Properties.Name
#Generate Column CSS (as col1, col2, col3 etc)
$colgroups = 1..($Columns.Count) | ForEach-Object{"<col class=`"col$_`" />"}
$colgroup = "<colgroup>$colgroups</colgroup>"
#Create table headers
$tableheaders = $Columns | ForEach-Object{"<th>$_</th>"}
$thead = "<thead><tr>$tableheaders</tr></thead>"
#endregion CSVSetup
#region BuildRows
$i = 0
$tablerows = $InputObject | ForEach-Object {
$i++
$c = 0
$ThisFormattedRow = $_.psobject.properties | ForEach-Object{
$c++
$ci = 0
$s = 0
$e = 0
$font = $null
$style = $null
If($csssplit -contains ".col$c {"){
$csssplit | ForEach-Object{
$ci++
If($_.Trim() -eq ".col$c {"){
$s = $ci
}
If(($e -lt $s) -and ($_.Trim() -eq "}")){
$e = $ci-2
}
}
$csssplit[$s..$e] | ForEach-Object{
If($_.Trim().Startswith("color:")){
$font = $_.Trim().replace(';','')
$font = $font.Split(':')[0].trim()+"=`""+$font.Split(':')[1].trim()+"`""
}
Else{$style+=$_.Trim()}
}
If($null -ne $font){
$fstyle = "<font $font>"
}
$cstyle = "style=`"$style`""
}
if($null -ne $font){$t = "$fstyle$($_.value)</font></td>"}
else{$t = "$($_.value)</td>"}
If($null -ne $style){$s = "<td $cstyle>"}
else{$s = "<td>"}
"$s$t"
}
If($i%2 -eq 1) {
"<tr $evenstyle>$ThisFormattedRow</tr>"
} else {
"<tr $oddstyle>$ThisFormattedRow</tr>"
}
}
#endregion BuildRows
$tbody = "<tbody>$tablerows</tbody>"
$table = -join('<table>',$colgroup,$thead,$tbody,'</table>')
Return $table
}
@Chirishman
Copy link
Author

Based on code found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment