Transposing HTML table in Email sender Snap
Hi All
Is there a way to transpose a HTML table in Email sender Snap? I am using a mapper Snap to pass key,value pairs to a Email sender snap and put them as a table in the email body. I have tried the sample code provided in below URL, however the table content is quite lengthy so it does not look good.
any help would be appreciated
snapping_turtl3 - try this:
- Change the "Email type" property to "HTML text"
- Switch the Template Body to enable expression (depress the equals sign) and use the following Template Body:
'<!DOCTYPE html> <html> <head> <title>Email Snap HTML Table</title> <style type="text/css"> table.Snap {background-color:transparent;border-collapse:collapse;width:100%;} table.Snap th, table.Snap td {text-align:center;border:1px solid black;padding:5px;} table.Snap th {background-color:AntiqueWhite;} </style> </head> <body> <p>Dear SnapLogic Users:</p> <p>This is a sample Snap email with HTML Table email type.</p> <table class="Snap"> ' + $.entries().map(x=> '<tr><th>' + x[0] + '</th><td>' + x[1] + '</td></tr>').join('\n') + ' </table> <p></p> <p>Regards,</p> <p>SnapLogic Staff</p> </body> </html> '
Make sure you keep the begin and end quote character since most of this is a static string. The only real difference here is the line that builds the HTML Table data explicitly rather than relying on the internal engine to do it.
' + $.entries().map(x=> '<tr><th>' + x[0] + '</th><td>' + x[1] + '</td></tr>').join('\n') + '
Basically, this is using the Object.entries() method to get an array of [key,value] pairs from the input document. Then the Array.map() method to create one static string of "<tr><th>[key]</th><td>[value]</td></tr>" for each key/value pair. Finally the Array.join() method is used to reduce the whole array to a single string with all table rows.
Hope this helps!