如何通过脚本编辑器邮寄googlesheet图表? [英] How to mail a googlesheet chart through script editor?

查看:87
本文介绍了如何通过脚本编辑器邮寄googlesheet图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的代码是直接将邮件(pdf)直接正确发送给收件人. 我想要的只是pdf部分中的图表部分或仅包含某些内容(数据)的范围. 此代码以pdf格式提取整张表格,甚至未使用的单元格也是如此. 如何在没有未使用的单元格的情况下获取工作表(pdf)?

The Code that I am using is sending mail (pdf) directly to recipient correctly. what I want is only the chart part to be there in the pdf or only the range that contain something (data). This code is fetching whole sheet as pdf, even the unused cells. How to fetch the sheet (pdf) without unused cells?

function onOpen()
{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Email').addItem('Send Emails', 'sendEmails').addToUi();
}  
function sendEmails(){ 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh4 = ss.getSheetByName("Sheet1");

  if(true){
  var emailBody="Dear Parent,<br>  <br>Please find the Program chart of your child.<br> To discuss it further, the Engagement  Manager would call you shortly.<br> ";
  var file= DriveApp.getFileById('<spreadsheet-id>');

  var currentEmail1 = sh4.getRange(1, 4).getValues();

      MailApp.sendEmail({
    to:'email-address',
    subject: "Wizklub Hots program",
    htmlBody: emailBody,
     attachments:[file.getAs(MimeType.PDF).setName('student report' + '.pdf')]
    });
} 
}

推荐答案

答案:

您需要先从工作表中收集图表,然后以电子邮件附件的形式发送正确的Mimetype枚举.

首先,您需要从工作表中收集图表:

Firstly you need to gather the charts from the sheets:

var chart = sh4.getCharts()[0];

然后将其作为附件与兼容的Mimetype一起发送-图像之一,因此JPEGPNG:

And then send this as an attachment with a compatible Mimetype - one of an image so either JPEG or PNG:

attachments:[chart.getAs(MimeType.PNG).setName('student report' + '.png')]

或:

attachments:[chart.getAs('image/png').setName('student report' + '.png')]

请记住,getCharts()将返回工作表中的所有图表,因此您需要通过引用相应的数组元素来发送正确的图表.

Remember that getCharts() will return all charts in the sheet so you need to send the correct one by referencing the corresponding array element.

希望对您有帮助!

  • Google Sheets API v4 - Charts
  • Google Apps Script - method getCharts()
  • Google Apps Script - Class EmbeddedChart
  • Google Apps Script - Enum MimeType

这篇关于如何通过脚本编辑器邮寄googlesheet图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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