howto add a googlechart on my draft email by appscript

Viewed 44

I need your help.

Context

I 'm generating a draft email with variable depending on Gsheet cells I have used this good youtube video as a starter https://www.youtube.com/watch?v=h2z13YE3kJg

The Code

I have create this code :

// filename code.gs

function emailbrouillon() {
// variable pour la creation du fichier HTML
var emailTemp = HtmlService.createTemplateFromFile("EMAIL");
//variable pour appeller le "sheet" du fichier actif  
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("emailSI");
//  la variable d'appel pour l'objet dans l'onglet final
var wsSettings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("emailSI");

// Sauver les lignes dans les variables Emails addresse et l'objet généré en colonne B
var nom = wsSettings.getRange("$B$1").getValue();
var sujet = wsSettings.getRange("$B$2").getValue();
var destinataire = wsSettings.getRange("$B$3").getValue();
var cc = wsSettings.getRange("$B$4").getValue();
var from = wsSettings.getRange("$B$6").getValue();

var corps32 = wsSettings.getRange("$B$41").getValue();


emailTemp.nom = nom;
emailTemp.sujet = sujet;
emailTemp.destinataire = destinataire;
emailTemp.cc = cc;
emailTemp.from = from;

// Graphique
emailTemp.corps32 = corps32;

  var htmlMessage = emailTemp.evaluate().getContent();
      GmailApp.createDraft
      (
      mail, 
      sujet,
       
      "Votre messagerie ne support pas HTML",
//envoyé le message créé htmlbody et le htmlmessage créé.
         {name: nom, htmlBody: htmlMessage,cc: cc, from: from}
      );
}




Where corps32 is the publish chart url https://docs.google.com/spreadsheets/d/e/sheetID/pubchart?oid=9911774433&format=image>> But this way I have the url generated by the html file below not the picture itself

//filename EMAIL.html

<p style="margin: 0cm 0cm 6pt; line-height: 110%; font-size: 10pt; font-family: Calibri, sans-serif;"><span style="font-family: verdana, geneva, sans-serif;">Introduction</span></p>


<p style="margin: 0cm 0cm 6pt; line-height: 110%; font-size: 10pt; font-family: Calibri, sans-serif;"><span style="font-family: verdana, geneva, sans-serif;"><br />&lt;&lt;<?= corps32 ?>&gt;&gt;</span></p>

how can i do to get it and include it in my html file ?
If the way to get the chart image is not good Can you provide me a sheet exemple for the same ?

Thank you

1 Answers

Adding Chart to Email Draft

function addingcharttoemaildraft() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const c = sh.getCharts()[0];
  const img = c.getAs("image/png").setName("myimage");
  const f = DriveApp.getFolderById(gobj.globals.testfolderid).createFile(img);
  let htmltemp = HtmlService.createTemplateFromFile('ah2');
  htmltemp.filename = "myimage";
  let imgObj = {img0:f.getBlob()};
   let html = htmltemp.evaluate().getContent();
  GmailApp.createDraft(gobj.globals.emailpub, "Test File", '', { htmlBody: html, inlineImages: imgObj});
}

html:

<html>
  <head>
    <title>Email Test</title>
  </head>
  <body>
    <p>This is the image from my chart</p>

     <img src="cid:img0" style="margin:2px 5px 2px 0;" /><?= filename ?><br />
  </body>
</html>

Image:

enter image description here

Related