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:
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];
.