Just need your help and advise. I am quite new in coding stuff. Recently, I have coded below stuff. I was expecting to fill in all the blanks on google form including users' email as long as the form has been submitted, it will be recorded to a google spreadsheet and generate a pdf then send to the email address that was typed in the google form. I have tested below scripts, now function afterFormSubmit and function createPDF both working the pdf file could be generated properly and all the information is populated into the google doc properly then convert to pdf formate however function sendEmail does not work. When I run function sendemail , it returns error message "Exception: Invalid argument: attachments (line 36, file "Code")" Could someone please help ?
function afterFormSubmit(e) {
const info = e.namedValues;
const pdfFile = createPDF(info);
const entryRow = e.range.getRow();
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
ws.getRange(entryRow, 7).setValue(pdfFile.getUrl());
ws.getRange(entryRow, 8).setValue(pdfFile.getName());
sendEmail(e.namedValues['Student Email'][0],pdfFile);
}
function sendEmail(currentEmail,pdfFile){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
for (var i = 2;i<=lr;i++){
var currentEmail = ss.getRange(i, 6).getValue();
var currentStudentName = ss.getRange(i, 2).getValue();
var currentName = ss.getRange(i, 2).getValue();
var messageBody = templateText.replace("{recipient}",currentName);
var subjectLine = "Congratulations on your merit!" + currentStudentName;
GmailApp.sendEmail(currentEmail, subjectLine, messageBody, {
attachments:[pdfFile],
name: 'St Clare Merits'});
}
}
function createPDF(info) {
const pdfFolder = DriveApp.getFolderById("1WIgfR7YESk6fcRPbzKa_Gsi_yp3-gemg");
const tempFolder = DriveApp.getFolderById("1QgnNLCbmm1z9RvPQCjA-vSnuw-P1Fw4X");
const templateDoc = DriveApp.getFileById("1fh5uRKQ9WZOY-ngXK0sOxH85UX_HHR1HYDefQhjfZVI");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{name}", info['Student name'][0]);
body.replaceText("{reason}", info['Reason for Merit'][0]);
body.replaceText("{Sname}", info['Staff Name'][0]);
body.replaceText("{Date}", info['Date'][0]);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Student name'][0]+ " " + new Date());
newTempFile.setTrashed(true);
return pdfFile;
}