Getting TypeError in onFormSubmit trigger?

I used the Google Forms tutorial to set up form data to be combined into a PDF file and then sent by email. When trying to run the script:

The following error message appears:

TypeError: Unable to read property values ​​from undefined. (line 11, file "Code")

I do not know how to fix this problem. I searched the Internet for an answer. Here is a copy of the script. I noted two lines where the script gives an error:

var docTemplate = "1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc"; var docName = "TestCertificate"; // When Form Gets submitted function onFormSubmit(e) { //Get information from form and set our variables var email_address = "EMAIL@example.com"; //**(THIS IS WHERE THE ERROR IS OCCURRING ON THESE 2 LINES BELOW!)** var full_name = e.values[2]; var Activity = e.values[3]; // Get document template, copy it as a new temp doc, and save the Doc's id var copyId = DocsList.getFileById(docTemplate) .makeCopy(docName+' for '+full_name) .getId(); // Open the temporary document var copyDoc = DocumentApp.openById(copyId); // Get the document's body section var copyBody = copyDoc.getActiveSection(); // Replace place holder keys,in our google doc template copyBody.replaceText('keyFullName', full_name); copyBody.replaceText('keyActivity', Activity); // Save and close the temporary document copyDoc.saveAndClose(); // Convert document to PDF var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); // Attach PDF and send the email var subject = "Report"; var body = "Here is the form for " + full_name + ""; MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf}); // Delete temp file DocsList.getFileById(copyId).setTrashed(true); } 

Here are the links to the form and certificate that I tested.

+4
google-spreadsheet google-apps-script
Aug 01 '13 at 2:25
source share
1 answer

The error you see is due to the fact that you are launching the trigger function in the Script editor. When you do this, the event parameter e not defined - this is what the error message says.

For more information, see How do I test the trigger function in GAS?

This uses a test function that will run your onFormSubmit() function several times, with data that is already in your spreadsheet. It reads each row of the sheet, generates an object to simulate the event that you receive when you submit the form, then calls the trigger function. If you place breakpoints inside onFormSubmit() or rely on Logger.log() , this method will allow you to test your trigger function.

 function test_onFormSubmit() { var dataRange = SpreadsheetApp.getActiveSheet().getDataRange() var data = dataRange.getValues(); var headers = data[0]; // Start at row 1, skipping headers in row 0 for (var row=1; row < data.length; row++) { var e = {}; e.values = data[row]; e.range = dataRange.offset(row,0,1,data[0].length); e.namedValues = {}; // Loop through headers to create namedValues object for (var col=0; col<headers.length; col++) { e.namedValues[headers[col]] = e.values[col]; } // Pass the simulated event to onFormSubmit onFormSubmit(e); } } 

I did not do any other debugging of your original function ... but this eliminates this error message, so you can continue testing.

+5
Aug 01 '13 at 3:16
source share



All Articles