2

I am trying to insert 3D charts from google sheet into email using the below code but the image in the email is getting converted to 2D. Can anyone help me to pull the charts from google sheet into an email with all its properties intact?

The Original Chart

The Chart in Email

function email(sheet, toMail, ccMail, msg){
  var charts = sheet.getCharts();
  var emailImages={};
  var chartBlobs=new Array(charts.length); 
  for(var i=0;i<charts.length;i++){    
    chartBlobs[i]= charts[i].getAs("image/png").setName("chartBlob"+i);
    emailBody= emailBody + "<p align='left'><img src='cid:chart"+i+"'></p>";
    emailImages["chart"+i]= chartBlobs[i];
  }
  MailApp.sendEmail({
    to: toMail,    
    cc: ccMail,
    subject: "ABC",   
    htmlBody: emailBody,
    inlineImages:emailImages});

}
5
  • Have you tried 'image/bmp', 'image/gif', or 'image/jpeg'?
    – TheMaster
    Apr 28, 2019 at 14:48
  • thanks for responding @TheMaster.I had tried 'image/bmp', 'image/gif', and 'image/jpeg' but all gave me 2D Charts
    – Sree
    Apr 28, 2019 at 15:07
  • 1
    @TheMaster I think that this might be a bug. Because I confirmed 2 same situations except for my environment. So I would like to report this situation to the issue tracker. So I proposed a current workaround.
    – Tanaike
    Apr 28, 2019 at 23:07
  • 1
    @Tanaike Good. If you do open a issue, Link it here, so that people might star it.
    – TheMaster
    Apr 29, 2019 at 8:44
  • 1
    @TheMaster Thank you for replying. I reported this situation to the issue tracker. I hope this issue is modified in the future.
    – Tanaike
    Apr 29, 2019 at 12:20

1 Answer 1

1
  • You want to send email including the 3D chart on Spreadsheet.

If my understanding is correct, how about this workaround? I experienced the same issue with you. When the chart on Spreadsheet are retrieved as a blob, the chart of the blob is different from the original chart. I think that this might be a bug. In this case, as one of several workarounds, I used the following flow.

Flow of modified script:

  1. Retrieve charts from Spreadsheet.
  2. Create Google Slides as a temporal.
  3. Insert the charts to Slides as images.
  4. Retrieve the inserted charts as blobs.
  5. Send email including the retrieved blob.
  6. Remove the temporal Google Slides.

Modified script:

In this modification, I modified your script. Please modify your script as follows.

function email(sheet, toMail, ccMail, msg){
  var charts = sheet.getCharts();

  var slides = SlidesApp.create("sample"); // Added
  var slide = slides.getSlides()[0]; // Added

  var emailImages={};
  var chartBlobs=new Array(charts.length);
  var emailBody = ""; // Added
  for(var i=0;i<charts.length;i++){
    var image = slide.insertSheetsChartAsImage(charts[i]); // Added
    chartBlobs[i]= image.getAs("image/png").setName("chartBlob"+i); // Modified
    emailBody= emailBody + "<p align='left'><img src='cid:chart"+i+"'></p>";
    emailImages["chart"+i]= chartBlobs[i];
  }
  MailApp.sendEmail({
    to: toMail,
    cc: ccMail,
    subject: "ABC",
    htmlBody: emailBody,
    inlineImages:emailImages});

  DriveApp.getFileById(slides.getId()).setTrashed(true); // Added
}

Note:

  • In this modified script, the temporal Google Slides is moved to the trash box.
  • I reported this issue to the issue tracker.

References:

If I misunderstood your question and this workaround was not what you want, I apologize.

2
  • Thanks a lot Tanaike.Your work around worked so perfectly.Now I am getting the same chart as in the Google Sheet.Hats off to you.
    – Sree
    Apr 29, 2019 at 2:23
  • @Sree Thank you for replying. I'm glad your issue was resolved. And I reported this situation to the issue tracker. I hope this issue is modified in the future.
    – Tanaike
    Apr 29, 2019 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.