Google-apps-script – not working form submit Event Object

google docsgoogle-apps-scriptgoogle-forms

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

Best Answer

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.