0

I'm trying to create a script that will populate a Google Sheet template based on a Google Form--basically, following this verbatim . My code looks like this:

function onFormSubmit(e) {
  //open the template document by ID
  //you can find the ID in the link of the document
  var templateDoc = DriveApp.getFileById('spreadsheetID');
  //create a copy of the template, we don't wanna mess up the template doc
  var newTempFile = templateDoc.makeCopy();
  
  //open the new template document for editing
  var openDoc = DocumentApp.openById(newTempFile.getId());
  var body = openDoc.getBody();
  
  //get the responses triggered by On Form Submit
  var items = e.response.getItemResponses();
  
  //find the text in the template doc and replace it with the Form response
  //items[0].getResponse() is the first response in the Form
  body.replaceText('{Var1}', items[1].getResponse());  
  body.replaceText('{Var2}', items[2].getResponse());
  body.replaceText('{Var3}', items[3].getResponse());
  body.replaceText('{Var4}', items[4].getResponse());
  body.replaceText('{Var5}', items[5].getResponse());

  
  //Save and Close the open document and set the name
  openDoc.saveAndClose();
  newTempFile.setName(items[0].getResponse() + '-' + items[1].getResponse()) }

However, I'm getting the following error:

TypeError: Cannot read property 'getItemResponses' of undefined
    at onFormSubmit(Code:13:26)

This is the exact same error someone in the comments of the tutorial got, and the author said that the error was a result of running the script from the Script Editor--however, I have enabled the on Form Submit trigger and am successfully getting a copy of my template, just without any of the variables filled in. Any advice would be greatly appreciated!

  • 1
    Welcome to Web Applications Is this a bounded script? if so, is it bounded to a spreadsheet or to a form? (please edit the question to add this details). – Rubén Sep 22 at 14:39
  • Welcome. 'getItemResponses' of undefined suggests that your script is not bound to your form and/or the tutorial has inadvertently directed you to create a Form submit trigger based on a spreadsheet. getItemResponses and getResponse are methods that are executed by a Form script. To paraphrase the question that @Rubén asked - did you build the script from the Form? and did you create the Form submit trigger “From form”? – Tedinoz Sep 27 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.