-1

Please help me out with below error.

TypeError: Cannot read property 'namedValues' of undefined

As I am new in Google Apps Script. i want to generate PDF and send to requester on his email id. Please review my script below and suggest.

function afterformsubmit(e){

const info = e.namedValues;
createPDF(info);
}

function createPDF(info){

const pdfFolder = DriveApp.getFolderById("1nV1zSSJfBND0ao9NufmIOEa_ocO5DhQf");
const tempFolder = DriveApp.getFolderById("1u3vbM1hxqKThVMsnjLeLWqLn3CWdTMDO");
const templateDoc   = DriveApp.getFileById("1qBtaAfcsHmxU9Y4wp6uQRdFkR95XhVNqpr6tDQ2xH7Q");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{dt}", info['PO Date'][0]);
body.replaceText("{dpt}", info['Department'][0]);
body.replaceText("{rname}", info['Requestor Name'][0]);
body.replaceText("{item}", info['Item 1'][0]);
body.replaceText("{qty}", info['Qty1'][0]);
body.replaceText("{price}", info['Unit price1'][0]);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Requestor Name'][0] + " " + info['Department'][0]);
tempFolder.removeFile(newTempFile);
return pdfFile;
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Pls also post the code which calls the function `afterformsubmit`. I guess you have passed the wrong `event object` or the property `namedValues` does really not exist like the error shows. – Long Luong Dec 22 '20 at 13:27

1 Answers1

1

You need to setup the necessary trigger on your project in order to get and use the event object e on your code. To setup the "on form submit" trigger, please follow the steps below:

1.) In the script editor, go to Edit -> Current project's triggers.

2.) On the new tab that appeared, you will see the list of triggers that are manually added by you(if any). To add the "on form submit" trigger, click on "+ Add Trigger" button.

3.) On the pop up, select the function that the trigger will run, fill up the other fields, and select "On form submit" as event type. Click Save.

This should setup the installable trigger "On form submit" on your project and should allow you to use namedValues on your script.

Reference

Event Objects and Triggers

Installable Triggers

Jason E.
  • 1,201
  • 1
  • 3
  • 10