79397845

Date: 2025-01-29 18:48:40
Score: 0.5
Natty:
Report link

@doubleunary said it correctly. In addition, to improve the code, I suggest using this code to combine all functions into one. In the last part, I changed tempFolder.removeFile(newTempFile); to tempFolder.getFilesByName(newTempFile).next().setTrashed(true); because the removeFile method is deprecated.

Code:

function onFormSubmit(e) {
  const info = e.namedValues;

  const pdfFolder = DriveApp.getFolderById("1g58GUQLPjPonsHtxj5LlxyoDgXs5wj2R");
  const tempFolder = DriveApp.getFolderById("1Fkzf0xeZcedfq7BF2k3V4mn4Pz_LsXsv");
  const templateDoc = DriveApp.getFileById("1eOqom8SqhuDUpIqYEVum-EvQ09cVz2d_XCLcRNAz8jE");

  const newTempFile = templateDoc.makeCopy(tempFolder);

  const openDoc = DocumentApp.openById(newTempFile.getId());
  const body = openDoc.getBody();
  body.replaceText("{fn}", info['First Name'][0]);
  body.replaceText("{ln}", info['Last Name'][0]);
  body.replaceText("{bd}", info['Birthday'][0]);
  body.replaceText("{em}", info['Email'][0]);
  body.replaceText("{pn}", info['Phone Number'][0]);
  body.replaceText("{pv}", info['Province'][0]);
  body.replaceText("{cm}", info['Contact Method'][0]);
  body.replaceText("{lg}", info['Language'][0]);
  body.replaceText("{ts}", info['Type Of Service'][0]);
  body.replaceText("{cn}", info['Child Name'][0]);
  body.replaceText("{cbd}", info['Child Birthday'][0]);
  body.replaceText("{sr}", info['Services Required'][0]);
  body.replaceText("{stf}", info['Staff Requested'][0]);
  body.replaceText("{pri}", info['Priority'][0]);
  body.replaceText("{ref}", info['Referral'][0]);
  body.replaceText("{jc}", info['Jane Consent'][0]);

  openDoc.saveAndClose();

  const blobPDF = newTempFile.getAs(MimeType.PDF);
  pdfFolder.createFile(blobPDF).setName(info['First Name'][0] + ' ' + (info['Last Name'][0]));
  // tempFolder.removeFile(newTempFile);
  tempFolder.getFilesByName(newTempFile).next().setTrashed(true);
}

References: setTrashed(trashed)

Reasons:
  • Blacklisted phrase (0.5): Contact Me
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @doubleunary
  • Low reputation (0.5):
Posted by: leylou