I have a Google Sheet with cells of both data and =sparkline()
in-cell charts, and want to send this data view in an email. I currently use Apps Script to make an HTML email, but the sparkline charts display as blank cells in the email's table.
The data as viewed on Google Sheets:
The data as viewed in the email:
This is my Apps Script code:
function drawTable() {
var ss_data = getData();
var data = ss_data[0];
var background = ss_data[1];
var fontColor = ss_data[2];
var fontStyles = ss_data[3];
var fontWeight = ss_data[4];
var fontSize = ss_data[5];
var html = "<table border='1'>";
for (var i = 0; i < data.length; i++) {
html += "<tr>"
for (var j = 0; j < data[i].length; j++) {
html += "<td style='height:20px;background:" + background[i][j] + ";color:" + fontColor[i][j] + ";font-style:" + fontStyles[i][j] + ";font-weight:" + fontWeight[i][j] + ";font-size:" + (fontSize[i][j] + 6) + "px;'>" + data[i][j] + "</td>";
}
html += "</tr>";
}
html + "</table>"
MailApp.sendEmail({
to: Session.getUser().getEmail(),
subject: "Spreadsheet Data",
htmlBody: html
});
}
function getData(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getDataRange();
var background = ss.getBackgrounds();
var val = ss.getDisplayValues();
var fontColor = ss.getFontColors();
var fontStyles = ss.getFontStyles();
var fontWeight = ss.getFontWeights();
var fontSize = ss.getFontSizes();
return [val, background, fontColor, fontStyles, fontWeight, fontSize];
}
Could anyone please suggest the code needed to display sparkline charts in the email, or any other work around?
How about this workaround? I think that there are several workaround in your situation. So please think of this as one of them.
Unfortunately, the chart created by
SPARKLINE()
cannot be directly imported to the email. So as a workaround, I used the following flow.SPARKLINE()
to the images using EmbeddedChart.Modified script :
Result :
Note :
SPARKLINE
.insertChart()
, because the blob directly retrieved fromnewChart()
cannot be used as an image. When the chart is created to the sheet, the blob retrieved from the created chart can be used as an image.setOption("width", 200)
andsetOption("height", 100)
.References :
If I misunderstand your question, I'm sorry.