使用应用脚本通过Google电子表格向电子邮件发送图表 [英] Emailing a chart from a Google spreadsheet with apps script

查看:122
本文介绍了使用应用脚本通过Google电子表格向电子邮件发送图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了高和低的答案,但没有运气。有一个Google电子表格,它使用应用程序脚本连接到数据库,将一些原始数据引入电子表格。然后,我使用各种电子表格公式来操作这些数据,然后最终创建一个图表。



我的下一个挑战是我希望能够通过电子邮件将该图表嵌入到电子邮件中应用程序脚本并将其作为HTML电子邮件发送。



这是否可能,或者我应该开始寻找其他解决方案?



谢谢!

Mikael

解决方案

是我用来给图表发送电子邮件的代码。
请注意,您需要为每个人提供对电子表格的链接访问权限。如果没有,你会在电子邮件中看到一个图片,说明用户没有登录。(非常烦人,没有解决方法)

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

if(charts.length == 0){
MailApp.sendEmail({
to:emails,
subject:ERROR:+ emailSubject,
htmlBody:电子表格中没有图表});
return;
}

var chartBlobs = new Array(charts.length);
var emailBody =图表< 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} );

}


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

解决方案

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});

}

这篇关于使用应用脚本通过Google电子表格向电子邮件发送图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆