1

I have a simple script to generate a doc and PDF upon form submission. I checked all of my code and have submitted a test form to test it out but I kept getting the error of 'values' being undefined when the script is trying to read from the spreadsheet result from the form.

I also tried adding the script in spreadsheet that stores the form submissioon result instead of the form, but I got the same error.

Can anybody points out what have I done wrong?

I set a trigger to execute the script upon form submission: Trigger set

Here's my code:

function myFunction(e) {
  //e.values is an array of form values
  var timestamp = e.values[0];
  var firstName = e.values[1];
  var lastName = e.values[2];
  var title = e.values[3];
  Logger.log(e.values[0]);

  //file is the template file, and you get it by ID
  var file = DriveApp.getFileById('google-doc-template-file-id'); 

  //We can make a copy of the template, name it, and optionally tell it what folder to live in
  //file.makeCopy will return a Google Drive file object
  var folder = DriveApp.getFolderById('my-drive-folder-id')
  var copy = file.makeCopy(lastName + ',' + firstName, folder); 

  //Once we've got the new file created, we need to open it as a document by using its ID
  var doc = DocumentApp.openById(copy.getId());

  //Since everything we need to change is in the body, we need to get that
  var body = doc.getBody(); 

  //Then we call all of our replaceText methods
  body.replaceText('{{First name}}', firstName); 
  body.replaceText('{{Last name}}', lastName);  
  body.replaceText('{{Company}}', company); 

  doc.getAs(pdf);
  //Lastly we save and close the document to persist our changes
  doc.saveAndClose(); 
}

The error occured at line 3 - var timestamp = e.values[0];.

ross
  • 2,684
  • 2
  • 13
  • 22
unacorn
  • 827
  • 10
  • 27
  • 2
    If I misunderstood your question, I apologize. About `getting error of 'values' undefined when the script is trying to read from the spreadsheet result from the form`, where does this error occur in your script? – Tanaike Feb 18 '20 at 07:18
  • Apologies, I forgot to add the details of the log: it happened at line 3 "var timestamp = e.values[0];" – unacorn Feb 18 '20 at 07:27
  • 2
    Thank you for replying. Can I ask you about the method for executing the function of `myFunction`? Because when the function of `myFunction` is directly run at the script editor, such error occurs because that `e` of the event object is not given. Or when you run the function of `myFunction` by running other function, also please provide the other function in order to replicate the issue. How about this? – Tanaike Feb 18 '20 at 07:39
  • Thanks Tanaike, I actually just set a trigger to run the script when i submit a form. My trigger runs the function (I added on my question above because I can't attach an image on the comment) – unacorn Feb 18 '20 at 08:18
  • Thank you for replying and adding the information. I proposed a modified script. Could you please confirm it? If I misunderstood your situation and this was not the result you want, I apologize. – Tanaike Feb 18 '20 at 08:56
  • I think you just need to change the event source to from spreadsheet rather than from form. – Cooper Feb 18 '20 at 14:35

2 Answers2

4
  • In your situation, the container-bound script of Google Form is used.
  • The OnSubmit event trigger is installed to the function of myFunction.
  • You want to retrieve 3 response values of firstName, lastName, title from the 4 questions.
  • You want to also retrieve timestamp.

If my understanding is correct, how about this modification? Please think of this as just one of several possible answers.

Modification points:

  • From the image you provided, it is found that the script is the container-bound script of Google Form.
    • Unfortunately, the event object of the OnSubmit event trigger has no property of values for the container-bound script of Google Form. Ref
    • In this case, it is required to retrieve the response values with e.response.getItemResponses().
  • In the case that values is included in the event object, it is required to be the container-bound script of Spreadsheet. When the container-bound script of Spreadsheet is used, you can retrieve the response values using e.values from myFunction(e). Ref
  • And also, in order to retrieve the time stamp, getTimestamp() is used.

When above points are reflected to your script, it becomes as follows.

Modified script:

From:
//e.values is an array of form values
var timestamp = e.values[0];
var firstName = e.values[1];
var lastName = e.values[2];
var title = e.values[3];
Logger.log(e.values[0]);
To:
var response = e.response;
var timestamp = response.getTimestamp();
var [firstName, lastName, title] = response.getItemResponses().map(function(f) {return f.getResponse()});

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks Tanaike! I read through the resources and make changes accordingly. I tried 2 things: (1) From form submission itself, I changed the code to the modified script you suggested. However I faced the same issue. The error I get is similar (just that it became 'response' undefined now - "TypeError: Cannot read property 'response' of undefined" (2) Since you mentioned the reason was because "values" is from spreadsheet, I also tried the script directly from the spreadsheet where the form submissions are captured. However in the spreadsheet it also mentioned "values" undefined. – unacorn Feb 18 '20 at 10:38
  • @unacorn Thank you for replying. I apologize for the inconvenience. I could confirm that the modified script worked. So I would like to confirm your current situation. 1. Is your script the container-bound script of Google Form? 2. In this case, when the Google Form is submitted, the script is run when the OnSubmit event trigger is installed to the function. If you directly run the script with the script editor, such error occurs. Please be careful this. 3. Can you provide the detail flow for replicating your situation including the executing method? By this, I would like to confirm it. – Tanaike Feb 18 '20 at 12:17
  • @unacorn By the way, where do you see the error message of `"TypeError: Cannot read property 'response' of undefined"` and `"values" undefined`? – Tanaike Feb 18 '20 at 12:21
  • thanks! i figured out what has gone wrong. Thanks for your guide :) – unacorn Feb 19 '20 at 05:17
  • @unacorn Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Feb 19 '20 at 23:24
1

I'm pretty sure I know the issue with your original script, as it came from an article on my site. There are two things that need to be changed if you want the original script to work:

  • The script needs to be bound to the spreadsheet accepting form responses, not the form itself.
  • You need to change the 'Event Source' on the trigger to 'from Spreadsheet' instead of 'from Form'
J_Everhart383
  • 354
  • 1
  • 6
  • 1
    Yes your article guided me through the setup! Thanks :) I figured out the error from Tanaike's solution, yours is equally correct and helpful to resolve my issue too. thank you so much!!! – unacorn Feb 19 '20 at 05:16