I have a google form with the following columns:
Name | email | have_kids
where Name is just text input, email is just an email, and have_kids is a yes/no radio button
ex. dmkumar | myemail#gmail.com | Yes
when the user submits the form, the data is sent to a google spreadsheet. I would like to have it set up so that if the user selected "Yes" to have_kids, then an email is sent to that user. Here is the code I have right now. I have a trigger event to run the function, from the spreadsheet, on the form submit.
Here is the code I have now (that is not working):
function emailConfirmation(e) {
var userEmail = e.values[11]; //column k
var check = e.values[14]; //column n
/**
* Un-comment this to use it for debugging
*/
//for (var i in e.values) {
// Logger.log("" + i + ":" + e.values[i]);
//}
var subject = "Sending email from a spreadsheet";
var message = "Hello World";
if(check == "Yes")
{
MailApp.sendEmail(userEmail, subject, message);
}
}
I am new to google scripting, so simple explanations would be much appreciated! Thanks!
This is the answer I came up, because I'm sure people will stumble upon this page sarcasm
But it works great, none the less.
function emailConfirmation(e) {
var userEmail = e.namedValues["Email Address"].toString(); //Where "Email Address" is the name of a column
var kids = e.namedValues["Some of my guests are children who will attend the kids' camp."].toString();
var subject = "Sending email from a spreadsheet";
if(kids == "Yes")
{
MailApp.sendEmail(userEmail, subject, kids);
}
}
Related
I have created a Google Form for my Elem. School Principal for Teacher Observations. She would like to be able to send the completed form to the teacher when she submits the form.
my idea is to somehow add a textbox for the Email address at the end of the form, and when email address is filled, click Submit which will process the form, and send a copy to the recipient email address. I am tryingo to do this via Google's Script editor, but I am not versed in JavaScript. Any help would be appreciated.
If you want to send the filled form to the address of whoever filled it, you can use the "Collect email addresses" and "response receipts" functions.
However, if you want to send the email to other users based on a form input field you can use something like this:
function onFormSubmit(e) {
var form = e.source;
var response = e.response.getItemResponses();
var targetEmail = response[response.length].getResponse(); //Gets response for last question
var htmlResponse = HtmlOutput.createHtmlOutput("<h1>Form Submission:</h1>");
var questions = form.getItems();
for (var i=0; i<questions.length; i++) {
var answerOfQuestion = response[i].getResponse();
if (typeof(answerOfQuestion)=="object") {
var tempAnswer = "<ul>";
for (var j = 0; j<answerOfQuestion.length; j++) {
tempAnswer += "<li>"+answerOfQuestion[j].toString()+"</li>";
}
tempAnswer += "</ul>";
answerOfQuestion = tempAnswer;
}
htmlResponse.append("<p><b>"+questions[i].getTitle()+":</b> "+answerOfQuestion+"</p>");
}
GmailApp.createDraft(targetEmail, 'Form Submission', 'Here is the form results:' + htmlResponse.getContent() , {
htmlBody: htmlResponse.getContent(),
}).send();
}
Go to Settings (the gear icon) > General Tab
Select both "collect email addresses" and "response receipts"
No additional coding necessary.
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);
}
}
}
I am using a Google Form and collecting the responses on a Sheet. I require notification emails be sent out to different teams depending on one of the responses on the Form. For example the form has the question, which Province are you in, if its AB then I need the AB team to get an email notification, if its the BC team I need only that team to be notified.
I have tried to use the Sheet (Responses) and build scripts off there by using Importrange function to create seperate sheets for each Province but that has not worked out so far. I am currently trying to see if I can just create the scripts in the Form itself and not have to worry about the sheets and just use one sheet.
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
Logger.log('Response #%s to the question "%s" was "%s"',
(i + 1).toString(),
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
}
}
/**
* Sends emails when form updated by end user, split responses by Province, send emails to team specified for each Province only.
*/
function sendEmails() {
var emailAddress = "example#example.com"; // First column
var message = itemResponse; // Response
var subject = 'Survey Updated';
MailApp.sendEmail(emailAddress, subject, message);
}
Currently the code above will send me an email once the form is submitted, I cannot figure out how to filter by Province or email a specific team based on the Province response. I also would like the details from each response to be in the body of the email so the teams do not have to open the spreadsheet.
Edit your sendEmails() function to grab the latest form response and search the item responses for the province:
function onSubmit(e) {
var responses = FormApp.getActiveForm().getResponses();
var response = responses[responses.length - 1].getItemResponses();
var provinceQuestionNumber = 4 // which question is the province question?
var province = response[provinceQuestionNumber - 1].getResponse()
if (province == "Province1"){
MailApp.sendEmail('province1Team#domain.com', 'New form response!', "The form has a new response for someone in province " + province + ".");
}
else if (province == "Province2"){
MailApp.sendEmail('province2Team#domain.com', 'New form response!', "The form has a new response for someone in province " + province + ".");
}
else if (province == "Province3"){
MailApp.sendEmail('province3Team#domain.com', 'New form response!', "The form has a new response for someone in province " + province + ".");
}
else if (province == "Province4"){
MailApp.sendEmail('province4Team#domain.com', 'New form response!', "The form has a new response for someone in province " + province + ".");
}
/*
...
*/
}
Then go to Edit -> Current Project's Triggers and set up a new installable trigger with the following settings:
Choose which function to run: sendEmails
Choose which deployment should run: Head
Select event source: From form
Select event type On form submit
Also don't forget to change the names of the provinces and extend the conditional statement if you need, and set which question number the province question is by changing the value of provinceQuestionNumber.
Try using the onFormSubmit trigger and write your script in the Spreadsheet with the Linked Sheet. You will be able to get the information you need from the event object on every onFormSubmit() and you should be able to direct your emails as needed with that information.
If you have any problems please return with additional questions.
The information you need (the response's province name) should be available in a field of the itemResponse object. So, what you nerd to do is access that information and make a switch case with it's value.
There is a link for the documentation of the itemResponse.getResponse() method:
https://developers.google.com/apps-script/reference/forms/item-response#getResponse()
I am using this code to send emails (composing email content getting text from the sheet named ranges:
//compose issue emails to student and admin
function composeIssueEmail() {
//student's name, last name and email
var email = ss.getRangeByName("CourseProgressEmail").getValue()
var name = ss.getRangeByName("CourseProgressName").getValue()
var lastName = ss.getRangeByName("CourseProgressStudentLastName").getValue()
var subj = ss.getRangeByName("SetUpIssueTitle").getValue()
var subject = subj.replace("*imya*", name)
var bodyText = ss.getRangeByName("SetUpIssueBody").getValue()
var body = bodyText.replace("*imya*", name)
var link = getChecksheetURL()
var text = body.replace("*link*", link)
//send email to student
var studentEmail = sendEmail(email, subject, text)
var adminEmail = "AGcourseSup#gmail.com"
var adminSubj = ss.getRangeByName("SetUpAdminIssueTitle").getValue()
var adminSubject = adminSubj.replace("*imya*", name)
var adminSubjectFinal = adminSubject.replace("*familia*", lastName)
var adminText = ss.getRangeByName("SetUpAdminIssueBody").getValue()
var adminTextReplace = adminText.replace("*imia*", name)
var adminBody = adminTextReplace.replace("*familia*", lastName)
var adminText = adminBody.replace("*link*", link)
//send email to admin
sendEmail(adminEmail, adminSubjectFinal, adminText)
}
//gets current checksheet URL
function getChecksheetURL() {
var Url = ss.getUrl()
var linkMiddle = "#gid="
var sheetID = sheet.getSheetId()
var shecksheetURL = Url + linkMiddle + sheetID
return shecksheetURL
}
//sends emails
function sendEmail(email, subject, body) {
GmailApp.sendEmail(email, subject, body)
}
Execution transcript:
[19-06-12 16:39:43:396 EEST] Execution succeeded [2.399 seconds total runtime]
It sends stably to the gmail account that is the same as spreadsheet's one.
But to another gmail account it sends about every other time.
Details:
This code is executed (I log the line after this code)
The emails are visible in my outbound box but not arriving to any of the boxes of the recepient gmail.
Not in spam etc.
I don't get any messages, error or bounce notifications.
I tried MailApp instead - it's even worse and sometimes doesn't send even to my own email.
I tried to change things in settings config, but didn't find anything to work.
I set up a filter "never send to spam" and "always star it" - didn't work.
I deleted a link from it so it has no link - didn't work.
What can be a solution?
I handled this issue. The issue is about anti-spam filters not about the code.
I gained more trust to the email account by adding "Reply To" option within GmailApp.sendEmail method. It magically solved the problem so each email reaches target now.
I want to get the contents of the google form sent to my email rather than just a notification that a new user has submitted the form. I put in the following code. But I get a response saying that namedValues is undefined. I tried actually submitting a dummy google form, but it still doesn't send me the contents of the google form on email.
function sendFormByEmail (e) {
var email = "example#gmail.com";
var Name = e.values[3];
var EmailAddress = e.values[4];
var txt = "";
for(var field in e.namedValues) {
txt += field + ' :: ' + e.namedValues[field].toString() + "\n\n";
}
MailApp.sendEmail(email, "New Client", txt);
}
If the script is attached to the form and triggered when that form is submitted, use the response object with that event. AFAIK the namedValues object you are trying to access is only available if your script is attached to the spreadsheet that recieves the form input.
I use this script when I want to quickly send a simple form submission to an email address (and not use the addons that can do this and more):
function onFormSubmit_sendEmail(e) {
var msgBody = "";
var itemResponses = e.response.getItemResponses();
for (var i = 0; i < itemResponses.length; i++) {
var itemResponse = itemResponses[i];
msgBody = msgBody + itemResponse.getItem().getTitle() + ": " + itemResponse.getResponse() + "\n\n";
}
MailApp.sendEmail("someemail#somedomain", "Form Submission", msgBody);
}
}
You can use some tests on the item title (getTitle() method) to do different things depending on the question - e.g. if one question is where you want the email sent to, do something like:
if (itemResponse.getItem().getTitle() == "Recipient Address") {
var email = itemResponse.getResponse();
You could then use that MailApp.sendMail() function. Hope this helps.