Get Google Sheet values from form submit trigger

1,355 views
Skip to first unread message

Wildbear Me

unread,
Mar 14, 2021, 11:05:28 AM3/14/21
to Google Apps Script Community
I am very new to Apps Script.  Any help is appreciated.
I am trying to create PDF files from a Google form submission, which also links to a Google sheet.  My Apps Script is connected to this Google sheet.  I also created a trigger from "on spreadsheet on form submit" event.  I have read about other's same situation. It seems no one has helped him.

I follow this youtube video to create my script.  https://www.youtube.com/watch?v=EpZGvKIHmR8

This is an error that I received after I submit the form.  The form information was added to the Google Sheet. A temp Google Doc was created.  However, the Google sheet values didn't get returned due to event object didn't pass these values.  
Screen Shot 2021-03-14 at 10.54.34 AM.png

This is my code. I think the e.nameValues is the issue. But I don't know how to fix it. I read the documentation.  This nameValues object does exist and it should pass the values in an array.  I also tested the Info array using a hard coded value. It works. But when I added the afterFormSubmit function. This script created errors above.  Your help is much appreciated. Thank you!
---------------------------------------------------------------
function afterFormSubmit(e) {
const info=e.nameValues;
createPDF(info);
}

function createPDF(info) {

// const info={
// 'Today Date': ['2021/01/14'],
// 'Check-in Date': ['2021/01/14'],
// 'Check-out Date': ['2021/01/15']
// };

const pdfFolder=DriveApp.getFolderById("11i2mJ0L4V1XjSXmuep5VBN9LCtme1yI2");
const tempFolder=DriveApp.getFolderById("1j_rVmne5ISwzyEl9La6pLMF5GYW97ytn");
const templateDoc=DriveApp.getFileById("1rgsPepR8m10_9dEti9pi4QNfZPziPrQr4p6seDsnZSw");

const newTempFile=templateDoc.makeCopy(tempFolder);
const openDoc=DocumentApp.openById(newTempFile.getId());

const body=openDoc.getBody();
body.replaceText("{todayDate}",info['Today Date'][0]);
body.replaceText("{checkInDate}",info['Check-in Date'][0]);
body.replaceText("{checkOutDate}",info['Check-out Date'][0]);

openDoc.saveAndClose();

const blobPDF=newTempFile.getAs(MimeType.PDF);
// const PDFFile=pdfFolder.createFile(blobPDF).setName("Regisration-"+info['Guest 1 First Name'][0]+"-"+info['Guest 1 Last Name'][0]+"-"+info['Check-In Date'][0]);
const PDFFile=pdfFolder.createFile(blobPDF).setName("Regisration-"+info['Check-in Date']);
tempFolder.removeFile(newTempFile);
}

Alan Wells

unread,
Mar 14, 2021, 1:19:23 PM3/14/21
to Google Apps Script Community
The letter "d" is missing from the method name:
Should be:
namedValues 
 
Do some debugging and log the value of "info" from both the
afterFormSubmit 
and 
 createPDF
functions.

On Sunday, March 14, 2021 at 11:05:28 AM UTC-4 wildb...@gmail.com wrote:
I am very new to Apps Script.  Any help is appreciated.
I am trying to create PDF files from a Google form submission, which also links to a Google sheet.  My Apps Script is connected to this Google sheet.  I also created a trigger from "on spreadsheet on form submit" event.  I have read about other's same situation. It seems no one has helped him.

I follow this youtube video to create my script.  https://www.youtube.com/watch?v=EpZGvKIHmR8

This is an error that I received after I submit the form.  The form information was added to the Google Sheet. A temp Google Doc was created.  However, the Google sheet values didn't get returned due to event object didn't pass these values.  
Screen Shot 2021-03-14 at 10.54.34 AM.png

This is my code. I think the e.nameValues is the issue. But I don't know how to fix it. I read the documentation.  This nameValues object does exist and it should pass the values in an array.  I also tested the Info array using a hard coded value. It works. But when I added the afterFormSubmit function. This script created errors above.  Your help is much appreciated. Thank you!
---------------------------------------------------------------
function afterFormSubmit(e) {
const info=e.nameValues;
createPDF(info);
}

function createPDF(info) {

// const info={
// 'Today Date': ['2021/01/14'],
// 'Check-in Date': ['2021/01/14'],
// 'Check-out Date': ['2021/01/15']
// };

const pdfFolder=DriveApp.getFolderById("");
const tempFolder=DriveApp.getFolderById("");
const templateDoc=DriveApp.getFileById("");

Wildbear Me

unread,
Mar 14, 2021, 3:50:16 PM3/14/21
to Google Apps Script Community

You are a life saver, Alan!  It works now. Thank you so much!

Vincent
Reply all
Reply to author
Forward
0 new messages