Below is my script. Each time the script runs it creates two copies of the pdf in the destination folder. I haven't been able to figure out why (at this point I can't see the forest through the trees). Any insight would be greatly appreciated. This script is triggered on form submission. Thanks.
//Global
var InspectionLog = "Log" //Log name in sheet
var pdfFolder = DriveApp.getFolderById("604F7h"); //Store finalized PDFs
var tempFolder = DriveApp.getFolderById("3W51X"); //Folder to make temp doc
var templateDoc = DriveApp.getFileById("2O3Ea"); //Template doc
var emailAddresses = "
kni...@kitt.com" //List of emails to receive confirmation PDF
var siteName = "Testing" //Name of site
function afterFormSubmit(e) {
const info = e.namedValues;
createPDF(info);
const pdfFile = createPDF(info);
const entryRow = e.range.getRow();
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(InspectionLog);
ws.getRange(entryRow + 1,1).setValue(pdfFile.getUrl());
sendEmail(emailAddresses,pdfFile);
}
function sendEmail(email,pdfFile){
const dataSheet3 = SpreadsheetApp.getActiveSpreadsheet()
const sheet3 = dataSheet3.getSheetByName(InspectionLog);
let cell5 = sheet3.getRange(3, 29,1,1).getValue();
let cell6 = sheet3.getRange(cell5,5,1,1) .getValue();
GmailApp.sendEmail(email, "Thank you for submitting your request", "Your PDF confirmation is attached.", {
attachments: [pdfFile],
name: 'Mark'
});
}
//Request Confirmation
function createPDF(info){
const dataSheet2 = SpreadsheetApp.getActiveSpreadsheet()
const sheet1 = dataSheet2.getSheetByName(InspectionLog);
let cell3 = sheet1.getRange(3, 29,1,1).getValue();
let cell4 = sheet1.getRange(cell3,3,1,1) .getValue();
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{Timestamp}", info['Timestamp'][0]);
body.replaceText("{By}", info['Requested by:'][0]);
body.replaceText("{Sub}", info['Sub:'][0]);
body.replaceText("{Date}", info['Requested Date:'][0]);
body.replaceText("{Time}", info['Requested Time:'][0]);
body.replaceText("{Description}", info['Description:'][0]);
body.replaceText("{Location}", info['Location:'][0]);
body.replaceText("{CNA}", info['CNA:'][0]);
body.replaceText("{Detail}", info['Detail:'][0]);
body.replaceText("{Section}", info['Section:'][0]);
body.replaceText("{Number}", info['Number:'][0]);
body.replaceText("{SI}", info['Special Required:'][0]);
body.replaceText("{QC}", info['Quality Control'][0]);
body.replaceText("{SI_Details}", info['Details:'][0]);
body.replaceText("{IR}", cell4);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(siteName + " IRC" + " " + "IR#" + " " + cell4);
newTempFile.setTrashed(true);
return pdfFile;
}