Creating an Auto Response using Google Forms - google-apps-script

I am trying to create an auto response back to the user after they have filled in a Google Form. So far i have the following code which i have adapted from another user
function emailconfirm(e) {
var userEmail = e.values[3]; //email from column D
var firstName = e.values[2]; //first name from column C
var StoreNo = e.values[4]; //last name from column E
var StoreName = e.values[5]; //test name from column F
MailApp.sendEmail(userEmail,
"Corporate Order Form Submitted",
"Thank you " +firstName + ", your Order Form for " +StoreNo +StoreName + "has been submitted. " +
"This has been forwarded to the Team for approval. " +
"Regards ");
I am getting to the following message when I debug the code:
TypeError: Cannot read property "values" from undefined. (line 2, file "Code")
Any ideas where I am going wrong?

When you're entering that function in the debugger, you've got no event parameter e to give it. The error message isn't lying to you.
See How can I test a trigger function in GAS?.

Related

Auto-direct emails to specific addresses on Google Form submit

This is my first post so apologies in advance if I am posting to the wrong place or asking a question that has been answered elsewhere - go easy on me!
In a nutshell, I have a Google Form and a connected Google Sheet. I need to automate it so that, when a new form is submitted, an email is sent to a specific colleague (the student's supervisor). I have had a good go myself but am now totally stuck!
I have created the form, linked it to a sheet, written the code, added the trigger and tested by submitting a form. Nothing happened! No error messages, just... nothing!
Any advice hugely appreciated. I am very new to this and still taking baby steps.
The code I have cobbled together (which is probably full of errors!) is as follows:
function wa132657(e) {
//setup the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get the range from OnFormSubmit
var range = e.range;
Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG
// get the data for the range
var response = row.getValues();
// get the supervisor name from the form submission
var supervisor = response[0][1];
Logger.log("DEBUG: Supervisor = "+supervisor);// DEBUG
// get the emails list
var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SupEmails");
// get ALL the data from this sheet
var emaildata = emailSheet.getDataRange().getValues();
// check how many rows of data
var emailLastRow = emailSheet.getLastRow();
// start the loop through the emails data
for (var i=1; i<emailLastRow; i++){
// if the supervisor is equal to SupervisorEmail
if (supervisor == emaildata[i][0]){
// there is a match
//Next, get the email address
var emailSupervisor = emaildata[i][1];
Logger.log("DEBUG: supervisor = "+emaildata[i][0]+", email address: "+emailSupervisor);// DEBUG
// Finally, send the Email.
var theirName = e.values[2];
var theirProgramme = e.values[3];
var theAbsenceReason = e.values[8];
var theAbsenceStart = e.values[5];
var theAbsenceEnd = e.values[6];
var subject = "Student Absence Report";
var message = "New Absence Report: " + theirName + " \n Programme: " + theirProgramme; + " \n\n
Reason for Absence: \n" + theAbsenceReason + " \n Start of Absence" + theAbsenceStart + "\n End of Absence:" + theAbsenceEnd;
MailApp.sendEmail(emailSupervisor, subject, message);
}
}
}

Custom Email based on Cell values in google sheets

I need help while building a Request Approval Flow in google Forms/Sheets
I have a data response sheet similar to like below, Column A to J headers are
"Timestamp" "EmailAddress" "Name" "Targets" "Account#" "Reason" "Access(Days)" "Approver" "Approved" "CaseID"
I have already setup a form submit email trigger to Approval body through formMule AddOn, Now I want to trigger email when Approval body approve or disapprove the request in Data response sheet.
Everytime when anyone select "Y" or "N" to column "I", script should suppose to trigger an email based on the data present in that row.
I am not an expert but tried to do it with following script and unfortunately not getting desired outcome, I set it up Current project trigger to OnEdit
function sendNotification1(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses 1");
if(e.range.getColumn() == 9 && e.value == "Y")
{
var name = e.range.offset(0,-6).getValue();
var comment = e.range.offset(0,-3).getValue();
var email = e.range.offset(0,-7).getValue();
var case1 = e.range.offset(0,1).getValue();
var approver1 = e.range.offset(0,-1).getValue();
var subject = "Request has been Approved";
var body = "Hi " + name + ", your Change Request number " + case1 + " has been approved by " + approver1 + " with following comments: " + "\n\r" + comment;
MailApp.sendEmail(email, subject, body);
}
if(e.range.getColumn() == 9 && e.value == "N")
{
var name = e.range.offset(0,-6).getValue();
var comment = e.range.offset(0,-3).getValue();
var email = e.range.offset(0,-7).getValue();
var case1 = e.range.offset(0,1).getValue();
var approver1 = e.range.offset(0,-1).getValue();
var subject = "Request has been Disapproved";
var body = "Hi " + name + ", your Change Request number " + case1 + " has been Dis-Approved by " + approver1 + " with following comments: " + "\n\r" + comment;
MailApp.sendEmail(email, subject, body);
}
}
StackDriver logging showed the following error, but couldn't identify where the issue exist while referring the cell addresses.
2018-09-14 08:19:06.336 PKT
The coordinates or dimensions of the range are invalid. at sendNotification1(Code:6)
I am able to trigger an email on any edit event with following code, but no luck with conditional edit event trigger.
function sendNotification1(e) {
MailApp.sendEmail("myemail#companygmail.com", "Sample subject", "Sample body");
}
While debugging the code, I can see the following error
TypeError: Cannot read property "range" from undefined. (line 6, file "Code")
Any help will be highly appreciated
You are likely running into the issue of the code not knowing what range you are referring to, because you never tell it where to look.
Add the following to the top of the function:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("YOUR SHEET NAME");
And it should work perfectly.
EDIT: Here's the email I sent to myself when I tested it.

Adding multiple attachments to email

I have a script which generates a document, saves as a PDF and attaches to an email. In addition to this attachment, I'm trying to add a second attachment which is an existing PDF saved on google drive (terms). Why is the following code not attaching the second document?
if (email_status == "YES" ) {
//send a pdf copy to customer
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var terms = DriveApp.getFileById('file ID');
var message = "Hi " + usernamefordoctitle + "!, please kindly find your invoice attached.\nMany Thanks!\nMe";
var emailAdd = sheet.getRange("D2").getValue()
var emailTo = emailAdd; // add customer email here
var subject = "Invoice for " + usernamefordoctitle + " from ME" + " - Invoice Number : " + newInvNumber;
var attach = {fileName:"INVOICE " + newInvNumber + " " + usernamefordoctitle + '.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach, terms.next()]});
ss.toast("70%: emailed customer");
Utilities.sleep(sleepINT);
}
You have
terms = DriveApp.getFileById('file ID');
followed by
terms.next()
This is incorrect, because getFileById gets you one specific file with the given Id. You were thinking of other methods like getFilesByName which return a file iterator. The name of the method is a clue to what it returns: getFile versus getFiles.
So, simply attaching
{attachments:[attach, terms]}
will work. You may also want to specify a MimeType like
terms.getAs(MimeType.PDF)
so that, e.g., you send a Google Doc as a PDF.

Sending a confirmation email after a google form is submitted

I have looked at many resources to figure out what is wrong with my script but nothing is working. I am trying to send a confirmation email after a google form has been submitted. It seems like my script is not calling the email address correctly from the form.
My original script came from http://acrl.ala.org/techconnect/?p=2343 and I modified it for my form.
This is what it looked like:
function swykemailconfirm(e) {
var userEmail = e.values[10]; //email from column K
var firstName = e.values[2]; //first name from column C
var lastName = e.values[1]; //last name from column B
var test = e.values[4]; //test name from column E
MailApp.sendEmail(userEmail,
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
I am getting an email with this error message after doing a test form submission:
Can not find method (class) sendEmail (string, string). (Line 6, file "Code")
After searching here Google Forms Confirmation Script for a way to edit my script, I used one of the suggestions and changed my code to the following:
function swykemailconfirm(e) {
var userEmail = e.values["E-mail"][0];
var firstName = e.values[2];
var lastName = e.values[1];
var test = e.values[4];
MailApp.sendEmail(userEmail,
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
I am getting an email with this error message after doing a test form submission:
TypeError: Can not read property "0" from undefined. (Line 2, file "Code")
Any help is much appreciated!
Your original function (top) will work properly with one small modification.
Google Docs is giving you the error message "Cannot find method (class) sendEmail (string, string). (Line 6, file "Code")" because the sendEmail method requires three arguments to passed: recipient (string), subject (string), body (string). Currently, you are only passing two arguments: recipient and body. See documentation here
Modify your function to the following and everything should work!
function swykemailconfirm(e) {
var userEmail = e.values[10]; //email from column K
var firstName = e.values[2]; //first name from column C
var lastName = e.values[1]; //last name from column B
var test = e.values[4]; //test name from column E
MailApp.sendEmail(userEmail,
"Registration Confirmation Subject Line",
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
Hope that helps!

Google App Script: Trying to setFormula for Spreadsheet cell

I am trying to set the formula for a Google Spreadsheet cell using the cell.setFormula function.
This is my code from the function. The line of code of importance is the last one where I try to setFormula.
//Create new empty project sheet
function copySheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var temp = ss.getSheetByName("Project_Template");
var name = Browser.inputBox("Create New Project", "Enter name for new project", Browser.Buttons.OK_CANCEL);
if (name != "cancel") {
ss.insertSheet(name,1, {template:temp})
//Add project name to project code cell
var newSheet = ss.getSheetByName(name);
var cell = newSheet.getRange(3, 2);
cell.setValue(name);
//Update formula in placemarkers sheet
var rowNum = ss.getNumSheets() - 3;
var formulaSheet = ss.getSheetByName("Placemarkers");
var formulaCell = formulaSheet.getRange(rowNum, 1);
formulaCell.setFormula('=if(isna(filter('name'!AH3:AH3,'name'!AH3:AH3 <> ""))=true,"",filter('name'!AH3:AH3,'name'!AH3:AH3 <> ""))');
}
}
When I try to save the script, I get the following error message:
Missing ) after argument list. (line 103)
I am sure it has to do with the combination of quotation and double quotation marks. I have not been able to get it working without an error message. Any help would be appreciated.
The + operator is not inserted to join string and the name variable. The following code part fixes the problem
formulaCell.setFormula('=if(isna(filter(' + name + '!AH3:AH3,' + name + '!AH3:AH3 <> ""))=true,"",filter(' + name + '!AH3:AH3,' + name + '!AH3:AH3 <> ""))');
.. it is not working to me.
cell.setFormula("=COUNTIF('name'!B14:B71,"G:*")");
cell.setFormula("=COUNTIF(' + name + '!B14:B71,"G:*")");
I guess that problem is with " in the setFormula's input.
It takes the " in the content of formula as a terminator of its parameter field and awaits ) symbol to enclose method call.
Solution:
Insert \ before " in the setFormula string:
cell.setFormula("=COUNTIF('name'!B14:B71,\"G:*\")");
_