Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emailing a chart from a Google spreadsheet with apps script

I've search high and low for an answer but without an y luck. Got a Google spreadsheet which uses apps script to connect to a database to pull in some raw data into a spreadsheet. I then use various spreadsheet formulas to manipulate that data and then final create a chart.

My next challenge is that I want to be able to embed that chart into an email via apps script and send it as an HTML email..

Is this at all possible or should I start looking for some other solution?

Thanks!

Mikael

like image 897
The Ginger Fox Avatar asked Nov 15 '13 10:11

The Ginger Fox


People also ask

How do you put a chart in the body of an email?

Subject = "Add Chart in outlook mail body" , Sheet1 is the sheet which contains the chart you want to send, please change it to your own. 3. Press the F5 key to run the code. In the opening Kutools for Excel dialog box, enter the name of the chart you will attach in the email body, and then click the OK button.


1 Answers

Here is the code I used to email charts. Please note that you need to give everyone with the link access to the spreadsheet. If not, you'll get an image in the email stating user is not signed in. (very annoying that there's no workaround to this)

function emailCharts(sheet,emails,emailSubject){
  var charts = sheet.getCharts();

  if(charts.length==0){
    MailApp.sendEmail({
      to: emails,
      subject: "ERROR:"+emailSubject,
      htmlBody: "No charts in the spreadsheet"});    
    return;
  }

  var chartBlobs=new Array(charts.length); 
  var emailBody="Charts<br>";
  var emailImages={};
  for(var i=0;i<charts.length;i++){
    chartBlobs[i]= charts[i].getAs("image/png").setName("chartBlob"+i);
    emailBody= emailBody + "<img src='cid:chart"+i+"'><br>";
    emailImages["chart"+i]= chartBlobs[i];
  }

  MailApp.sendEmail({
    to: emails,
    subject: emailSubject,
    htmlBody: emailBody,
    inlineImages:emailImages});

}
like image 163
dparnas Avatar answered Nov 15 '22 06:11

dparnas