Question

I'm hoping that someone can help me, What i want to do is to make a Google form create a new document and send that pdf via email when the form is edited.

I understand that when you edit a form the script inside a spreadsheet does not run, So i have put the script into a form and that is where i am having problems. I can not get the script to read the values that are been resubmitted on the form, it just keeps on coming up as "undefined" on the return

This is the script that i have got so far.

function Test(e) {

  var form = FormApp.openById('****Form Key****'); //Enter Form ID here

var docTemplate = "***doc to be used***";  
   var docName      = "***Name of document***";

   var formResponse = form.getResponses();

  var one =  e.response;  //I have tried e.value but does not pull through
  var two =  e.response;  // Column 2

  var copyId = DocsList.getFileById(docTemplate)
                .makeCopy(docName)
                .getId();
// Open the temporary document
   var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
   var copyBody = copyDoc.getActiveSection(); 

  copyBody.replaceText('one', one);
  copyBody.replaceText('two', two);

 // Save and close the temporary document
   copyDoc.saveAndClose();

// Convert temporary document to PDF by using the getAs blob conversion
   var pdf = DocsList.getFileById(copyId).getAs("application/pdf");  

//Sends the email
  var email_Address =  "***Email Address***"
     var subject = "***Subject";
  var body    = "**Body***"; 
  MailApp.sendEmail(email_Address,  subject, body, {attachments: pdf});  

}}

Any help will be greatly appreciated as i have been stuck on this for a while. thank you.

Was it helpful?

Solution

In a Form Response Trigger function, the event is an instance of Class FormResponse. Because of that, you don't need to open the form, or get responses... you've just had one handed to you, which you access via e.response. (See Form Response Events in Understanding Events.)

// Handle form response event
// This function must be a Form Response Trigger, attached to
// a Form (not a Spreadsheet).
function rightTest( e ) {
  var formResponses = e.response.getItemResponses(); // array of responses
  var one = formResponses[0];
  var two = formResponses[1];
  ...
}

OTHER TIPS

As of April 2015, DocsList is not supported, simply replace DocsList. with DriveApp. How to update DocsList to DriveApp in my code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top