I have a survey form that we use for our clients. It is a series of multiple choices questions, and a few text field entries. At the end, you click "submit" and an email is sent containing all the responses to our Quality Manager. Recently, the form stopped working, and I found many people who successfully fixed the issue by changing docslist to DriveApp. However, now I am getting the error:
TypeError: Cannot read property "response" from undefined. (line 10, file "")
The entire code reads as follows:
/// Get template from Google Docs and name it
var docTemplate = "1vCv-NR9c2G5q6f6EJ2CL4zAEQe_37OE7MgsSLTIKheY"; // *** replace with your template ID ***
var docName = "ClientSurvey";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
Logger.log(Utilities.jsonStringify(e));
var formResponses = e.response.getItemResponses();
Logger.log(formResponses[0].toString());
var service_type = formResponses[0].getResponse().toString();
var email_address = "notrealaddress#ntainc.com, notrealaddress#ntainc.com"
//var service_type = e.values[0];
var d = new Date();
var timeStamp = d.getTime();
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+timeStamp)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyServiceType', service_type);
for (var i = 0; i < formResponses.length; i++) {
copyBody.appendParagraph(formResponses[i].getItem().getTitle().toString());
copyBody.appendParagraph(formResponses[i].getResponse().toString());
copyBody.appendParagraph(" ");
}
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Customer Survey";
var body = "Here is a New Customer Survey";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
Please help!
This is more of a comment than an answer, but I dont have enough rep to comment yet.
Check your trigger, there is two types of FormSubmit triggers, one for sheets and one for forms, if you are using the wrong one that could be what is causing your issues.
Related
I have been receiving 2 main issues back upon a google form submission- the first being that the e.values I have set up cannot be read and the second that the Doclist is not defined. I have already set the triggers for the e.values and tried using e.namedvalues as well. I would like to eliminate the second issue by sending a doc and not a pdf to the form submitter, however it is most important that I get the first issue addressed first.
I am using framework that I have modified to fit my own documents, so that may be where I am encountering issues.
// G. Hopper
// Subcontract Generator
// Get template from Google00 Docs and name it
var docTemplate = "16bdl2myh7f0ywlxmf3iimq4Nz6-j8VTs7hkMbshRjp0";
var docName = "Subcontract";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var sub_name = e.namedValues["Subcontractor Name"].toString();
var sub_address = e.namedValues["Subcontractor address"].toString();
var proj_name = e.namedValues["Project Name"].toString();
var own_name = e.namedValues["Owner Name"].toString();
var own_address = e.namedValues["Owner Address"].toString();
var designer = e.namedValues["Design Firm"].toString();
var design_address = e.namedValues["Design Firm Address"].toString();
var proj_address = e.namedValues["Project Address"].toString();
var scope = e.namedValues["Scope of work"].toString();
var date_bid = e.namedValues["Date on bid"].toString();
var sub_value = e.namedValues["Subcontract Value"].toString();
var date_pay = e.namedValues["Date of the month pay applications are due"].toString();
var email = e.namedValues["Your email address"].toString();
var day = e.namedValues["Day"].toString();
var month_year = e.namedValues["Month and Year"].toString();
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy('Subcontract for '+sub_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keySubName', sub_name);
copyBody.replaceText('keySubAddress', sub_address);
copyBody.replaceText('keyProject', proj_name);
copyBody.replaceText('keyOwner', own_name);
copyBody.replaceText('keyOwnAddress', own_address);
copyBody.replaceText('keyDesigner',designer);
copyBody.replaceText('keyDesignAddress', design_address);
copyBody.replaceText('keyProjAdd', proj_address);
copyBody.replaceText('keyScope', scope);
copyBody.replaceText('keyBidDate', date_bid);
copyBody.replaceText('KeySubValue', sub_value);
copyBody.replaceText('keyPayApp', date_pay);
copyBody.replaceText('keyDay', day);
copyBody.replaceText('keyMonthDay', month_year);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = 'Subcontract'
var body = "Here is your subcontract for " + sub_name + "";
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
I am using Google Apps script to set up a spreadsheet merge to a from letter that will be emailed. I copied a similar script and added my info.
Everytime I try to save it I get the "We're sorry a server error occurred. Please wait a bit and try again."
// dixie#yellowdogknitting.com
// Job Offer Letter
// Get template from Google Docs and name it
var docTemplate = "1Y5ohbBWPeLSvNijge6I3ueQpulFzeZTky7853sKGSj8";
var docName = "HR_2_Job_Offer_form_letter";
// When Form Gets Submitted
function onFormSubmit(e) {
// Get information from form and set our variables
var email_address = e.values[10];
var date = e.values[1];
var first_name = e.values[2];
var last_name = e.values[3];
var job_title = e.values[11];
var status = e.values[13];
var hourly_wage = e.values[14];
var start_date = e.values[15];
// Get document template, copy it as a new temp doc, and save the doc's id
var copyId = DocList.getFileById(docTemplate)
.makeCopy(docName+' for '+first_name last_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document's body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys, in our google doc template
copyBody.replaceText('keyTodaysDate', date);
copyBody.replaceText('keyFirstName', first_name);
copyBody.replaceText('keyLastName', last_name);
copyBody.replaceText('keyJobTitle', job_title);
copyBody.replaceText('keyStatus', status);
copyBody.replaceText('keyHourlyWage', hourly_wage);
copyBody.replaceText('keyStartDate', start_date);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert document to PDF
var pdf = DocList.getFileById(copyId).getAs("application/pdf")'
// Attach PDF and send the email
var subject = "Job Offer";
var body = "Here is the Job Offer for " + first_name last_name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
This happens when there's some kind of Javascript error thrown behind the scenes.
In order to work out which line has the error, put a breakpoint at the first line of code (you do this by clicking just to the left of the line number.)
Then step through the code and see when the server error pops up. Then we can help you work out what's wrong with that particular line.
I'm hoping that someone can help me, What i want to do is to make a Google form create a new document and send that pdf via email when the form is edited.
I understand that when you edit a form the script inside a spreadsheet does not run, So i have put the script into a form and that is where i am having problems. I can not get the script to read the values that are been resubmitted on the form, it just keeps on coming up as "undefined" on the return
This is the script that i have got so far.
function Test(e) {
var form = FormApp.openById('****Form Key****'); //Enter Form ID here
var docTemplate = "***doc to be used***";
var docName = "***Name of document***";
var formResponse = form.getResponses();
var one = e.response; //I have tried e.value but does not pull through
var two = e.response; // Column 2
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
copyBody.replaceText('one', one);
copyBody.replaceText('two', two);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
//Sends the email
var email_Address = "***Email Address***"
var subject = "***Subject";
var body = "**Body***";
MailApp.sendEmail(email_Address, subject, body, {attachments: pdf});
}}
Any help will be greatly appreciated as i have been stuck on this for a while. thank you.
In a Form Response Trigger function, the event is an instance of Class FormResponse. Because of that, you don't need to open the form, or get responses... you've just had one handed to you, which you access via e.response. (See Form Response Events in Understanding Events.)
// Handle form response event
// This function must be a Form Response Trigger, attached to
// a Form (not a Spreadsheet).
function rightTest( e ) {
var formResponses = e.response.getItemResponses(); // array of responses
var one = formResponses[0];
var two = formResponses[1];
...
}
As of April 2015, DocsList is not supported, simply replace DocsList. with DriveApp. How to update DocsList to DriveApp in my code
I used a Google Forms tutorial to tweak Form data to merge into a PDF and then send to an email. I am getting the following error message when I try to run the script:
TypeError: Cannot read property "values" from undefined. (line 11, file "Code")
I do not know how to fix the problem. I have searched the web for an answer. Here is a copy of the script. I marked the 2 lines where the script is giving an error:
var docTemplate = "1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc";
var docName = "TestCertificate";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set our variables
var email_address = "EMAIL#example.com";
//**(THIS IS WHERE THE ERROR IS OCCURRING ON THESE 2 LINES BELOW!)**
var full_name = e.values[2];
var Activity = e.values[3];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+full_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyFullName', full_name);
copyBody.replaceText('keyActivity', Activity);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Report";
var body = "Here is the form for " + full_name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
Here are links to the form and certificate I was testing.
Form/Spreadsheet
Document Template
The error you're seeing is because you're running a trigger function in the Script Editor. When you do this, the Event Parameter e is not defined - that's what the error message is saying.
For more background, see How can I test a trigger function in GAS?
Here's a test function that will run your onFormSubmit() function multiple times, with the data that's already in your spreadsheet. It reads each row of the sheet, generates an object to simulate the Event you would get when a form was submitted, then calls the trigger function. If you place breakpoints inside onFormSubmit(), or rely on Logger.log(), this technique will allow you to test your trigger function.
function test_onFormSubmit() {
var dataRange = SpreadsheetApp.getActiveSheet().getDataRange()
var data = dataRange.getValues();
var headers = data[0];
// Start at row 1, skipping headers in row 0
for (var row=1; row < data.length; row++) {
var e = {};
e.values = data[row];
e.range = dataRange.offset(row,0,1,data[0].length);
e.namedValues = {};
// Loop through headers to create namedValues object
for (var col=0; col<headers.length; col++) {
e.namedValues[headers[col]] = e.values[col];
}
// Pass the simulated event to onFormSubmit
onFormSubmit(e);
}
}
I've done no other debugging of your original function... but this gets rid of that error message, so you can continue testing.
I use the below code to test my triggers
//
//
function onFormSumbit(e) {
//Timestamp 1 Status Remarks 3 Expected Completion Date 4 TRB Number
var resp = e.source.getActiveSheet().getRange(e.range.rowStart,1, e.range.rowStart,5 ).getValues();
/*
function test(){
var ss=SpreadsheetApp.getActive();
var respsht=ss.getSheetByName("Form responses 1");
var resp = respsht.getRange("A119:P119").getValues();
*/
I will comment out the onformsubmit portion and run the test portion.
Once the function is working, I will comment out the test function and activate the onformsubmit function.
Simple solution :)
My company recently switched to Google Apps and I am trying to covert Word and Excel docs to Google Docs. I am completely new to Google App Script and am stuck. I have been searching for a few days a have not found what I am looking for.
I have a Google form that is emailed to a sales person. After it is completed the Google spreadsheet is updated and then sends a form letter to the sales person summarizing their input. Some of the fields on the form are optional. How do I create a template that does not include some of the form letter text if the question was left unanswered?
For example, one of the optional fields is for a customer website address, if the customer does not have a website I don’t want the words website to show up on the form.
If anyone has any “For Dummies” instructions on how to do this I would really appreciate it.
Thanks.
Here is what I have so far:
// Business Review Report
// Get template from Google Docs and name it
var docTemplate = "1gRk2irahyW2Sf4pQMTDykXlMQqYgbe1Ba1vkh20KDYo";
var docName = "Business Review";
// When Form Gets submitted
function onFormSubmit (e) {
//Get information from form and set as variables
var variablename = "static entry or form value"
var email_address = e.values[1];
var Business_Name = e.values[2];
var Business_Address = e.values[3];
var Phone_Number = e.values[4]
var Email_address = e.values[5];
var Website = e.values[6];
var Participants = e.values[7];
var Agenda = e.values[8];
var Updates_and_Progress1 = e.values[9];
var Updates_and_Progress2 = e.values[10];
var Updates_and_Progress3 = e.values[11];
var Current_Issues1 = e.values[12];
var Current_Issues2 = e.values[13];
var Current_Issues3 = e.values[14];
var Next_Steps = e.values[15];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+ Business_Name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyBusinessName', Business_Name);
copyBody.replaceText('keyBusinessAddress', Business_Address);
copyBody.replaceText('keyPhoneNumber', Phone_Number);
copyBody.replaceText('keyEmailaddress', email_address);
copyBody.replaceText('keywebsite', Website);
copyBody.replaceText('keyParticipants', Participants);
copyBody.replaceText('keyAgenda', Agenda);
copyBody.replaceText('keyUpdatesAndProjects1', Updates_and_Progress1);
copyBody.replaceText('keyUpdatesAndProjects2', Updates_and_Progress2);
copyBody.replaceText('keyUpdatesAndProjects3', Updates_and_Progress3);
copyBody.replaceText('keyCurrentIssues1',Current_Issues1);
copyBody.replaceText('keyCurrentIssues2', Current_Issues2);
copyBody.replaceText('keyCurrentIssues3', Current_Issues3);
copyBody.replaceText('keyNextSteps', Next_Steps);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Business Review";
var body = "Business Review for " + Business_Name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
It works, but the resulting PDF also includes text for the unfilled fields like CurrentIssues2.
I am not sure if I am explaining this clearly so please respond with any questions and I will try to be more clear.
Thanks again,
Lisa
Your form questions apparently have default values that you don't want in your form letter. You can check for those defaults and replace them with something more suitable, such as a blank string.
...
// If website question was not answered, leave blank
var Website = e.values[6];
if (Website == 'website') Website = '';
...
This alternative uses the Ternary or Conditional Operation, and is more compact:
...
// If website question was not answered, leave blank
var Website = (e.values[6] == 'website') ? '' : e.values[6];
...