Convert Google Forms responses to PDFs

1,127 views
Skip to first unread message

Tamesh

unread,
Jul 14, 2021, 12:10:33 PM7/14/21
to Google Apps Script Community
Can anyone point me to a script or tutorial to create a script that would allow me to convert form responses to PDF? This would behave similarly to the google form notifications add-on by Digital Inspiration, but I don't need something that complex. I just want to make it so that when a form submission is made, the responses are placed into a PDF with each result on its own line. If it can be sent via email that would be great too.

Any help would be appreciated.
Thanks!

Alan Wells

unread,
Jul 14, 2021, 1:28:51 PM7/14/21
to Google Apps Script Community

Andrew Roberts

unread,
Jul 14, 2021, 1:37:54 PM7/14/21
to google-apps-sc...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/12884851-8184-4ffe-91f0-5653dbc2b095n%40googlegroups.com.

Tamesh

unread,
Jul 15, 2021, 8:38:12 AM7/15/21
to Google Apps Script Community
This may be exactly what I'm looking for. I'll give it a try and let you know. 

Thanks, Alan and Andrew.

Tamesh

unread,
Jul 16, 2021, 11:38:47 AM7/16/21
to Google Apps Script Community
Hi Guys,

I am getting the error below referring to the namedValues field. Any ideas? Below the error is my code.

TypeError: Cannot read property 'namedValues' of undefined
After_Submit
@ Code.gs:3




function After_Submit(e){
  
  const info = e.namedValues;
  const pdfFile = Create_PDF(info);  
  
  console.log(info);
  
  sendEmail(e.namedValues['Email Address'][0],pdfFile);  
}

function sendEmail(email,pdfFile){
  
  GmailApp.sendEmail(email, "ID CARD", "This is your ID Card.", {
    attachments: [pdfFile], 
    name: "HEllO"

  });
 
}
function Create_PDF(info) {
  
  const PDF_folder = DriveApp.getFolderById("1zx7rnI2M3p2U7RGTJugM_0G5aMINYTyh");
  const TEMP_FOLDER = DriveApp.getFolderById("1jO1BHwhwkKbGFcyT8DAJsew2v0gjCI4W");
  const PDF_Template = DriveApp.getFileById("1qHOMwuq2X_5LhUCfPLWcpUSh2n7pVRvHZ_kE-hsGmwg");
  
  const newTempFile = PDF_Template.makeCopy(TEMP_FOLDER);
  const  OpenDoc = DocumentApp.openById(newTempFile.getId());
  const body = OpenDoc.getBody();
  
  console.log(body);
  
   body.replaceText("{email}", info['Email Address'][0])
   body.replaceText("{name}", info['Clients First name'][0]);
   body.replaceText("{dob}", info['Date of Birth'][0]);
   body.replaceText("{phone}", info['Phone number '][0]);
   body.replaceText("{borough}", info['Borough '][0]);
   body.replaceText("{language}", info['language'][0]);

  OpenDoc.saveAndClose();
  

  const BLOBPDF = newTempFile.getAs(MimeType.PDF);
  const pdfFile =  PDF_folder.createFile(BLOBPDF).setName(info['Clients first name'][0] + " " + info['dob'][0]);
  console.log("The file has been created ");
  
  return pdfFile;

}



Alan Wells

unread,
Jul 16, 2021, 1:25:48 PM7/16/21
to Google Apps Script Community
"e" is undefined.  "e" is the event object.  If you are running the code from the code editor, then there is no event object.  The only way that an event object is passed to the function is if the function is triggered by the event.  You can either trigger the event by submitting the Form.  Or you could pass in your own object for testing from some other function.  Or you could check for the event object being undefined, and if it is, assume that it's for testing, and get the last entry in the Form.  Or get the last row in the Sheet.

Tamesh

unread,
Jul 19, 2021, 9:37:02 AM7/19/21
to Google Apps Script Community
Thanks for that. I closed the code editor and submitted another test entry but am still getting the error. The entry is showing up in the spreadsheet but no PDF/email is being generated and I get the error. Hmmm.

Alan Wells

unread,
Jul 19, 2021, 10:35:16 AM7/19/21
to Google Apps Script Community
Okay, that's weird.  Maybe trying deleting all existing triggers and create a new one?  I don't know.
Something is triggering that function to run, but not passing in a valid event object.

Reply all
Reply to author
Forward
0 new messages