[英]Create and email google doc from form submission
I'm trying to create a Google doc from a form submission and send the doc via email.我正在尝试从表单提交创建 Google 文档并通过电子邮件发送文档。 THe data for the doc comes from a second sheet (not the response sheet) that looks up values based on the submission (a student id that looks up student, parent, class, and teacher data placed in a 3rd sheet).
文档的数据来自第二张表(不是响应表),该表根据提交(查找学生、家长、班级和教师数据的学生 id 放在第三张表中)查找值。 For some reason, the email and doc that is created and sent is the previous submission, but not the latest submission.
由于某种原因,创建和发送的电子邮件和文档是以前的提交,而不是最新的提交。 My scripting knowledge is insufficient to figure this one out.
我的脚本知识不足以解决这个问题。 Any help is much appreciated.
任何帮助深表感谢。 My document
我的文件
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1Q5s3doVHDDNygeLFNLqm90x9JoVZTIFW77Xix7VckVs');
var cache = CacheService.getScriptCache();
const destinationFolder = DriveApp.getFolderById('1rE8vvX9omreAUy7gnrhxpYcfM6uK3v4r')
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data')
const rows = sheet.getRange("Data!A2:S2").getValues();
const friendlyDate = new Date(rows[4]).toLocaleDateString("dd/mm/yyyy");
const sheetR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses')
rows.forEach(function(row, index){
const copy = googleDocTemplate.makeCopy(`${row[12]}, ${row[13]}` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[4]).toDateString();
body.replaceText('{{timestamps}}', row[0]);
body.replaceText('{{emailSG}}', row[1]);
body.replaceText('{{rutalumnoa}}', row[2]);
body.replaceText('{{cOMMENTS}}', row[3]);
body.replaceText('{{fecha}}', friendlyDate);
body.replaceText('{{solicitadapor}}', row[5]);
body.replaceText('{{asistentes}}', row[6]);
body.replaceText('{{objetivo}}', row[7]);
body.replaceText('{{familia}}', row[8]);
body.replaceText('{{colegio}}', row[9]);
body.replaceText('{{conclusionyacuerdos}}', row[10]);
body.replaceText('{{sTUDENTnAME}}', row[12]);
body.replaceText('{{cLASS}}', row[13]);
body.replaceText('{{tEACHERNAME}}', row[14]);
body.replaceText('{{motherName}}', row[15]);
body.replaceText('{{fatherName}}', row[16]);
body.replaceText('{{motherRut}}', row[17]);
body.replaceText('{{fatherRut}}', row[18]);
doc.saveAndClose();
const url = doc.getUrl();
const docname = doc.getName();
const StudentName = sheet.getRange("M2").getValue();
const Class = sheet.getRange("N2").getValue();
sheet.getRange("U2").setValue(url);
const GDoc = sheet.getRange("U2").getValue();
const emailAddress = sheet.getRange("B2").getValue();
const message = 'Entrevista: Alumno/a ' + StudentName +', Curso: ' + Class +', Archivo: ' + docname +', ' + GDoc;
const subject = 'Entrevista: ' + StudentName +', ' + Class;
MailApp.sendEmail(emailAddress, subject, message);
sheetR.deleteRow(2);
})
}
In the end the error was in the spreadsheet;最后错误出现在电子表格中; the range from the response sheet that I was using was obtained by using the Importrange method.
我使用的响应表中的范围是通过使用 Importrange 方法获得的。 I changed the method to an Index and Counta method (Eg. =index(Responses!A2:K,Counta(Responses!A2:A))).
我将该方法更改为 Index 和 Counta 方法(例如 =index(Responses!A2:K,Counta(Responses!A2:A)))。 This did the trick.
这成功了。 No need to delete the responses in the script.
无需删除脚本中的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.