MS Flow - Approval Email - Markdown Table
-
19-02-2021 - |
Domanda
I am new to MS Flow, I am creating an approval process where I have to send data in a table.
I have an array, which is
[
{
"Header": [
"Username",
"Hname",
"RE"
],
"Data": [
"aa",
"bb",
"cc"
]
},
{
"Header": [
"Username",
"Hname",
"RE"
],
"Data": [
"aa11",
"bb11",
"cc33"
]
}
]
How can I create a Markdown table from the above array to get the below table?
|Header |Data |
|Username|aa |
|Hname |bb |
|RE |cc |
|Username|aa1 |
|Hname |bb2 |
|RE |cc3 |
I have passed the above array from the select output ie. body('select') to foreach action, and foreach output ie. items(apply_to_each) to parse_JSON. Below is the output from Parse_JSON.
{
"Header": [
"Username",
"Hostname",
"RE"
],
"Data": [
"aa",
"bb",
"cc"
]
}
Soluzione
Since this a complex array, I used a combination of [Parse JSON] and [Apply to each] actions, and string functions to generate the HTML table to get the output as requested.
See the screenshots and explanation below.
varArray - a string variable, contains the array string that has been provided.
varHTMLTable - a string variable, initialized with a Table layout with some styles. See Expression 1.
Expression 1
<!DOCTYPE html>
<html>
<head>
<style>
th, td {
border-left: 1px solid black;
padding: 5px;
width:200px;
}
th {text-align:left; }
</style>
</head>
<body>
<table style="width:50%">
<tr>
<th>Header</th>
<th>Data</th>
<th></th>
</tr>
<!--add a blank row between [Header] and [Data] Rows -->
<tr>
<td style="border:0px;"> </td>
<td style="border:0px;"> </td>
<td style="border:0px;"> </td>
</tr>
Detail 1
Expression 2
concat('<tr><td>', items('Apply_to_each')['Header']?[0], '</td><td>', items('Apply_to_each')['Data']?[0], '</td><td> </td></tr>',
'<tr><td>', items('Apply_to_each')['Header']?[1], '</td><td>', items('Apply_to_each')['Data']?[1], '</td><td> </td></tr>',
'<tr><td>', items('Apply_to_each')['Header']?[2], '</td><td>', items('Apply_to_each')['Data']?[2], '</td><td> </td></tr>'
)
Detail 2
The Final output seen in the email body
Note: Table styles can be adjusted as needed.
Updated for a Markdown Table
The process is the same to generate a Markdown Table.
However, for adding a line break, only </br>
worked for me.
Initialize a string variable, varMarkdownTable, with the table header
concat('## Markdown Table </br>', '| Header | Data |</br>', '| :---------: | :---------: |</br>', '| | |</br>')
Within Apply to each, using [Append to string variable], append the table body string to varMarkdownTable variable
concat('| ', items('Apply_to_each')['Header']?[0], ' | ', items('Apply_to_each')['Data']?[0], ' |</br>', '| ', items('Apply_to_each')['Header']?[1], ' | ', items('Apply_to_each')['Data']?[1], ' |</br>', '| ', items('Apply_to_each')['Header']?[2], ' | ', items('Apply_to_each')['Data']?[2], '|</br>')
Generated Markdown Table as seen in outlook email
Copy the the output and verify using Visual Studio Code or MarkdownLivePreview
Altri suggerimenti
You can use Parse JSON and then Select action to selecting specific properties you want to add to table.
Then you need to use Create HTML Table action in Power Automate to generate table from JSON data.
Check below references for detailed information: