0

I have a Google form which I use to populate a Google docs from a certain template whenever a new response is submitted. In order to achieve this, I have written this script, but it is not working and showing this error TypeError: Cannot read property 'values' of undefined (line 3, file "Code"):

function createdocs(e) {

  var Contracttype = e.values[1];
  var Partnername = e.values[2];
  var Bankname = e.values[3];
  var Accountnumber = e.values[4];
  var TIN = e.values[5];
  var Bankcode = e.values[6];
  var Businesscode = e.values[7];
  var Phone = e.values[8];
  var Director = e.values[9];
  var Contractstartingdate = e.values[10];
  var Contractendingdate = e.values[11];
  var EmailAddress = e.values[12];

  var file = DriveApp.getFileById('10lZQhmbocTOmvGwklYnHNvXbdGXDmpbgFbvcnu67G7c'); 
  var folder = DriveApp.getFolderById('1O6QJZ5gRRoSUeolcVvKlMe65Wdb4rz9z')
  var copy = file.makeCopy(Partnername + ',' + Contractstartingdate, folder); 
  var doc = DocumentApp.openById(copy.getId()); 
  var body = doc.getBody(); 

  body.replaceText('{{Partner name}}', Partnername);  
  body.replaceText('{{Bank name}}', Bankname);
  body.replaceText('{{Account number}}', Accountnumber);
  body.replaceText('{{TIN}}', TIN);
  body.replaceText('{{Bank code}}', Bankcode);
  body.replaceText('{{Business code}}', Businesscode);
  body.replaceText('{{Phone}}', Phone);
  body.replaceText('{{Director}}', Director);
  body.replaceText('{{Contract starting date}}', Contractstartingdate);
  body.replaceText('{{Contract ending date}}', Contractendingdate);

  doc.saveAndClose(); 
}

I have used a youtube tutorial for this purpose and followed every step, But I am failing, how to define values in the script, I have installed the onformSubmit trigger. But there is something going wrong Please help me with this out

enter image description here

3
  • 1
    Does this answer your question? TypeError: Cannot read property 'range' of undefined at onformsubmit(Code:10:17) – Rubén Apr 15 '20 at 20:04
  • The point I wanted to state is that onformsubmit trigger is not working when I install it manually. The problem is going with event object, I suppose. Of course, what you suggested is working perfectly, but what is interesting me is why it is not working when installed manually in my case?! – Aroon Advisory Group Apr 17 '20 at 23:40
  • 1
    @AroonAdvisoryGroup You are having this problem because you are running the script from the Script Editor. The property "values" only has a context when no context and that is why is is When you do this, "e.values" – Tedinoz Jul 12 '20 at 10:04
0

You are having this problem because you are running the script from the Script Editor. When you do this, the event objects aren't created - this is why "values" is described as "undefined".

You are using an onformsubmit trigger and event objects are created ONLY when a form is submitted.

Your spreadsheet must contain the Response sheet for a form. When a form is submitted, the Response sheet is automatically updated and the script is triggered. At this point, the event objects (includes "values") can be accessed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.