[英]How to send email from google sheet with the sheet as a PDF attachment
I have a google sheet that has two sheets, Form Responses and report : see here我有一个谷歌表,有两张表,表单响应和报告:见这里
when a form submits report sheet change with the last form or last row of the responses sheet.当表单提交报告表时,会使用响应表的最后一个表单或最后一行更改。 I want to send an email to the person who submits the form and attach the report sheet as PDF after sending email write sent email in column e: see here
I want to send an email to the person who submits the form and attach the report sheet as PDF after sending email write sent email in column e: see here
email to: Form Responses column b subject: some text and Form Responses column a cc: ABC@yahoo.com body: some text and Form Responses column f attach: report sheet as PDF email 至:表单响应列 b 主题:一些文本和表单响应列 a 抄送:ABC@yahoo.com 正文:一些文本和表单响应列 f 附加:报告表为 ZBCD1B68617759B1DFCFF0403
i used this code but it doesn't work我使用了这段代码,但它不起作用
function onSubmit(e){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow();
var sa = sheet.getRange(lastRow, 1).getValue();
var sB = sheet.getRange(lastRow, 2).getValue();
var sf = sheet.getRange(lastRow, 6).getValue();
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetgId = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
var email = Session.getUser().getEmail();
var subject = SB;
var body = Sf;
var url = "https://docs.google.com/spreadsheets/d/e/..............................................................=0&single=true&output=pdf";
var result = UrlFetchApp.fetch(url)
var contents = result.getContent();
if (emailSent !== "EMAIL_SENT") {
MailApp.sendEmail(email,subject ,body, {attachments:[{fileName:SB+".pdf", content:contents, mimeType:"application//pdf"}]});
sheet.getRange().setValue("EMAIL_SENT");
SpreadsheetApp.flush();
}
}
I get the url from spreadsheet>file>publish to web>publish report tab as pdf我从电子表格>文件>发布到网络>发布报告选项卡中获得 url 作为 pdf
i rewrite the code but get error " TypeError: report.getAs is not a function (line 36, file "Code") "我重写代码但得到错误“ TypeError:report.getAs is not a function(第 36 行,文件“代码”) “
function onSubmit(e){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var calculate = ss.getSheets()[2];
var Responses = ss.getSheets()[0];
var report = ss.getSheets()[1];
var lastRow = Responses.getLastRow();
var sa = sheet.getRange(lastRow, 1).getValue();
var sB = sheet.getRange(lastRow, 2).getValue();
var se = sheet.getRange(lastRow, 5).getValue();
var sf = sheet.getRange(lastRow, 6).getValue();
var sh = sheet.getRange(lastRow, 8).getValue();
var cell = calculate.getRange("b2");
cell.setFormula(sh);
SpreadsheetApp.flush();
var email = sB;
var subject = "راید ریپورت "+sa;
var body = se;
var calculate = ss.getSheets()[2];
vafunction onSubmit(e){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var calculate = ss.getSheets()[2];
var Responses = ss.getSheets()[0];
var report = ss.getSheets()[1];
var lastRow = Responses.getLastRow();
var sa = sheet.getRange(lastRow, 1).getValue();
var sB = sheet.getRange(lastRow, 2).getValue();
var se = sheet.getRange(lastRow, 5).getValue();
var sf = sheet.getRange(lastRow, 6).getValue();
var sh = sheet.getRange(lastRow, 8).getValue();
var cell = calculate.getRange("b2");
cell.setFormula(sh);
SpreadsheetApp.flush();
var email = sB;
var subject = "راید ریپورت "+sa;
var body = se;
var calculate = ss.getSheets()[2];
var Responses = ss.getSheets()[0];
var report = ss.getSheets()[1];
var pdf = report.getAs('application/pdf');
MailApp.sendEmail(email,subject ,body, {attachments:[pdf]});
sf.setvalue("EMAIL_SENT");
SpreadsheetApp.flush();
}
r Responses = ss.getSheets()[0];
var report = ss.getSheets()[1];
var pdf = report.getAs('application/pdf');
MailApp.sendEmail(email,subject ,body, {attachments:[pdf]});
sf.setvalue("EMAIL_SENT");
SpreadsheetApp.flush();
}
I would recommend you to use Apps Script for this.我建议您为此使用 Apps 脚本。
You as the owner of the form and response sheet, can create a bound script and use the Gmail and Forms services to create your desired pdf, and sent it to your users.您作为表单和响应表的所有者,可以创建一个绑定脚本并使用 Gmail 和 Forms 服务来创建您想要的 pdf,并将其发送给您的用户。
You can also create triggers to check when a new response is submitted.您还可以创建触发器来检查何时提交新响应。
References:参考:
I dug out an old example and was using getAs() to send a document so if you only want one sheet you would just create a new spreadsheet with the info you do want to send.我挖出了一个旧示例并使用 getAs() 发送文档,因此如果您只想要一张表,您只需创建一个包含您想要发送的信息的新电子表格。
var newSheet = SpreadsheetApp.create(Title,y,x);
Set whatever values you want in it and then设置你想要的任何值,然后
var pdf = newSheet.getAs('application/pdf');
MailApp.sendEmail(email, subject, body, {attachments:[pdf]} );
I suggest creating a new spreadsheet because you cannot convert just one tab to PDF.我建议创建一个新的电子表格,因为您不能只将一个选项卡转换为 PDF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.