{"id":1653,"date":"2013-07-05T09:40:53","date_gmt":"2013-07-05T09:40:53","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1653"},"modified":"2024-04-26T12:47:48","modified_gmt":"2024-04-26T12:47:48","slug":"generating-alternate-colors-in-an-email-html-table-generated-by-powershell-in-sharepoint","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/generating-alternate-colors-in-an-email-html-table-generated-by-powershell-in-sharepoint\/","title":{"rendered":"Generating alternate colors in an email HTML table generated by PowerShell in SharePoint"},"content":{"rendered":"<h2>Generating rich text emails with PowerShell<\/h2>\n<p>In this post:\u00a0<a href=\"https:\/\/reality-tech.com\/generating-automatic-emails-with-embedded-reports-and-link-to-uploaded-csv\/\" target=\"_blank\" rel=\"noopener\" aria-label=\"Generating automatic emails with embedded-reports and link to uploaded csv - open in a new tab\" data-uw-rm-ext-link=\"\">Generating automatic emails with embedded-reports and link to uploaded csv<\/a><br role=\"presentation\" data-uw-rm-sr=\"\" \/>we explored sending emails and attachments with HTML tables. Now let\u2019s have a look at alternating row colors in the HTML.<\/p>\n<p>This function will be used later to inject CSS reference to alternating colors into the HTML Table:<\/p>\n<pre lang=\"php\">\r\nFunction Set-AlternatingRows {\r\n     \r\n    [CmdletBinding()]\r\n    Param(\r\n        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]\r\n        [string]$Line,\r\n        \r\n        [Parameter(Mandatory=$True)]\r\n        [string]$CSSEvenClass,\r\n        \r\n        [Parameter(Mandatory=$True)]\r\n        [string]$CSSOddClass\r\n    )\r\n    Begin {\r\n        $ClassName = $CSSEvenClass\r\n    }\r\n    Process {\r\n        If ($Line.Contains(\"<tr>\"))\r\n        {   $Line = $Line.Replace(\"<tr>\",\"<tr class=\"\"$ClassName\"\">\")\r\n            If ($ClassName -eq $CSSEvenClass)\r\n            {   $ClassName = $CSSOddClass\r\n            }\r\n            Else\r\n            {   $ClassName = $CSSEvenClass\r\n            }\r\n        }\r\n        Return $Line\r\n    }\r\n}\r\n<\/pre>\n<p>Let\u2019s now define $a as the styles, including the style for alternating rows, which we will use when generating the HTML table:<\/p>\n<pre lang=\"php\">\r\n$a = \"<style>\"\r\n$a = $a + \"TABLE{border-width: 1px;border-style: solid;border-color:black;}\"\r\n$a = $a + \"Table{background-color:#EFFFFF;border-collapse: collapse;}\"\r\n$a = $a + \"TH{border-width:1px;padding:5px;border-style:solid;border-color:black;background-color:#DDDDDD}\"\r\n$a = $a + \"TD{border-width:1px;padding-left:5px;padding-right:3px;border-style:solid;border-color:black;}\"\r\n$a = $a + \".odd { background-color:#ccddee; }\"\r\n$a = $a + \".even { background-color:#eeeeff; }\"\r\n$a = $a + \"<\/style>\"\r\n<\/pre>\n<p>In generating the HTML table, note the reference to the above function in the Pipeline:<\/p>\n<pre lang=\"php\">\r\n$MyOutput = $table | ConvertTo-Html Title, Link, Author, Size, FileName, Modified -head $a -body \"<H1>$($rows.count) $($Header)<\/H1>\"  | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Convert-HTMLEscape\r\n<\/pre>\n<p>As a refresher, we use this function to handle HTML escaping so the HTML comes out delightfully formatted:<\/p>\n<pre lang=\"php\">\r\nFunction Convert-HTMLEscape {\r\n<#\r\nconvert &lt; and &gt; to < and > It is assumed that these will be in pairs\r\n#>\r\n \r\n[cmdletbinding()]\r\n \r\nParam (\r\n[Parameter(Position=0,ValueFromPipeline=$True)]\r\n[string[]]$Text\r\n)\r\n \r\nProcess {\r\nforeach ($item in $text) {\r\n    if ($item -match \"&lt;\") {\r\n         \r\n        (($item.Replace(\"&lt;\",\"<\")).Replace(\"&gt;\",\">\")).Replace(\"&quot;\",'\"')\r\n     }\r\n     else {\r\n        #otherwise just write the line to the pipeline\r\n        $item\r\n     }\r\n }\r\n} #close process\r\n \r\n} #close function\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Generating rich text emails with PowerShell In this post:\u00a0Generating automatic emails with embedded-reports and link to uploaded csvwe explored sending emails and attachments with HTML tables. Now let\u2019s have a look at alternating row colors in the HTML. This function will be used later to inject CSS reference to alternating colors into the HTML Table: [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1657,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-1653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":1,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":1658,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1653\/revisions\/1658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1657"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}