5

我一直在寻找答案,但没有运气。得到了一个谷歌电子表格,它使用应用程序脚本连接到数据库以将一些原始数据拉入电子表格。然后我使用各种电子表格公式来处理这些数据,然后最终创建一个图表。

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

这完全有可能还是我应该开始寻找其他解决方案?

谢谢!

迈凯尔

4

3 回答 3

5

这是我用来通过电子邮件发送图表的代码。请注意,您需要向拥有电子表格链接访问权限的所有人授予权限。如果没有,您将在电子邮件中收到一张图片,说明用户未登录。(非常烦人,没有解决方法)

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

}
于 2014-03-05T14:13:57.070 回答
1

将电子表格设为公开并运行脚本。脚本的修改版本粘贴在下面并带有注释

enter code here function 
emailCharts(sheet,emails,emailSubject){
var targetspreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Active        spreadsheet of the key file
var sheet = targetspreadsheet.getSheetByName('Sheet1'); // Change the sheet name 
var emailSubject = 'test';
var emails = 'test@test.com'; // your email ID
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++){
var builder = charts[i].modify();
builder.setOption('vAxis.format', '#');
var newchart = builder.build();
chartBlobs[i]= newchart.getAs('image/png');
emailBody= emailBody + "<p align='center'><img src='cid:chart"+i+"'></p>";
emailImages["chart"+i]= chartBlobs[i];
}

MailApp.sendEmail({
to: emails,
subject: emailSubject,
htmlBody: emailBody,
inlineImages:emailImages});
}
于 2015-11-06T08:12:51.417 回答
0

由于Charts服务似乎只是服务中EmbeddedChart使用的部分实现,SpreadsheetApp并非所有ChartTypes和选项都已实现,这意味着图表的呈现方式可能与它们在 Google 表格中的呈现方式不同。以下先前答案的修改版本将意味着图表作为图像包含在电子邮件中,就像它们出现在 Google 表格中一样。由于此解决方案还使用脚本生成的身份验证令牌,因此不必更改 Google 表格上的查看权限。

// Based on https://stackoverflow.com/a/22200230/1027723
function emailChartUsingImageUrl(){
  const idt = SpreadsheetApp.getActive().getId();
  const sheet = SpreadsheetApp.getActiveSheet();
  const charts = sheet.getCharts();
  
  // setup some variables for our email
  const chartBlobs = new Array(); 
  const emailImages = {};
  let emailBody = "Charts<br>";
  
  // setup our call to fetch the chart image
  const token = ScriptApp.getOAuthToken(); // project requires https://www.googleapis.com/auth/spreadsheets scope
  const baseUrl = `https://docs.google.com/spreadsheets/d/${idt}/embed/oimg?access_token=${token}&disposition=ATTACHMENT&bo=false&filetype=png&oid=`;
  
  // for each chart fetch the download image as a blob and appended to our email body
  charts.forEach(function(chart, i){
    // NEW BIT
    const url = baseUrl + chart.getChartId();
    chartBlobs[i] = UrlFetchApp.fetch(url).getBlob();
    emailBody +=  "<p align='center'><img src='cid:chart"+i+"'></p>";
    emailImages["chart"+i]= chartBlobs[i];
  });
  
  // Send email with inline images
  MailApp.sendEmail({
    to: "me@example.com",
    subject: "Email Charts - get chart from image url",
    htmlBody: emailBody,
    inlineImages:emailImages});
}
于 2020-09-08T14:20:31.093 回答