I am trying to create a daily email that sends daily report in a table format. Unfortunately, I am facing 2 problems with the code:
- The table data is not aligned to center. Instead all the data are on the right side of the table.
- Column SLA % should be in % format.
Attached is the look of the current email notification.
Below is the code I am using. Really appreciate anyone's advise
function getData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = sheet.getSheetByName('Summary');
var datavalues = sheet1.getDataRange().getValues();
var html= "Dear All," + "\n\n" + "Please find the daily report as below. <br><br>" + '<table>';
if(datavalues.length>0){
for(var i=0; i<datavalues.length;i++){
html+= '<tr style = bgcolor = "Black">';
for(var j=0; j<datavalues[i].length;j++)
{
if(i==0)
{
html+= Utilities.formatString('<td bgcolor = "Orange";style = "border:2px solid black"<th>%s</th></td>' ,datavalues[i][j]);
}
else{
html+= Utilities.formatString('<td style = "border:2px solid black" > %s</td>',datavalues[i][j]);
}
}
}
html+= '<table>';
}
return html;
}
function sendEmail(){
MailApp.sendEmail({
to:"[email protected]",
subject: "Daily Report",
htmlBody: getData()
})
}
- Table data to be in "centered" alignment
- Column SLA % to be in % format
Suggestion
You would have to add a
text-align:centerHTML tag to center the table data. A simpleifstatement can then be used to display the values under the columnSLA %to % format.You can try modifying your existing
forloop with this:Sample data in Google Sheets
Email Output
Note that the code above only centers the table data. If you wish to center the table headers as well, you can replace
with
Contents of the
<th> </th>tags are centered by default.Reference
HTML Horizontal alignment