Execute Chart Library via Google App Script?

25 views
Skip to first unread message

Kevin Beattie

unread,
Mar 13, 2017, 6:59:52 PM3/13/17
to Google Visualization API
Hi,

Has anyone experimented with generating chart data on the fly for use with Google App Script?  I've been able to successfully generate the necessary html and javascript as var html, then pass that along to which uses MailApp.sendEmail in Google App script.  The problem with this approach is that the html and javascript isn't executed and is ignored.  If I look at the original email (with the headers), I see the HTML content, but the email is otherwise blank and no chart is visible.

Here's a sample of code:
// Generate HTML Content
function genHTML (val1, val2){
 
var htmlbody = "<!DOCTYPE html> <html> <head>";
  htmlbody
= htmlbody + "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>";
  htmlbody
= htmlbody + "<script type=\"text/javascript\">";
  htmlbody
= htmlbody + "google.charts.load(\'current\', {\'packages\':[\'corechart\']});";
  htmlbody
= htmlbody + "google.charts.setOnLoadCallback(drawChart);";
  htmlbody
= htmlbody + "function drawChart() { ";
  htmlbody
= htmlbody + "var data = google.visualization.arrayToDataTable([";
  htmlbody
= htmlbody + "[\'2-Factor Status\', \'Total\'],";
  htmlbody
= htmlbody + "[\'Enabled\', " + val1 + "],";
  htmlbody
= htmlbody + "[\'Disabled\'," + val2 + "]]);";
  htmlbody
= htmlbody + "      var options = { ";
  htmlbody
= htmlbody + "        title: \'Google 2-Factor Status\'};";
  htmlbody
= htmlbody + "      var chart = new google.visualization.PieChart(document.getElementById(\'piechart\'));";
  htmlbody
= htmlbody + "        chart.draw(data, options);}";
  htmlbody
= htmlbody + "    </script> </head> <body> <div id=\"piechart\" style=\"width: 900px; height: 500px;\"></div> </body> </html>";
 
Logger.log(htmlbody);
 
return htmlbody;
}

Thanks in advance!

-Kevin

Phil Tyskewicz

unread,
Sep 9, 2020, 10:13:42 AM9/9/20
to Google Visualization API
Did you ever get this resolved? I am having a similar issue where I am trying to use htmlservice to render a google visualization, but it doesn't show in the email.

Thanks,
Phil

Reply all
Reply to author
Forward
0 new messages