sending automatic email newsletter from google spreadsheet - google-apps-script

I would like to be able to send an automatic email message to those who complete a google form . There responses are fed to a google spreadsheet, and I'd like to be able to set it up so that anytime someone completes the form, they are sent the email. Any idea how to do this? I've tried the google tutorial about sending emails from a spreadsheet, but since this is not an automatic function, it does not suit my needs. Any suggestions?

Create an installable trigger in the Form to run for the on Form Submit event. Use a function like getFormDataFields to grab the data from the form submission event. Use the MailApp.sendEmail function to blast out an email. Provide it the necessary parameters.
function myFunction(e) {
var data = getFormDataFields(e); //data['Question Name']
MailApp.sendEmail();
}
function getFormDataFields(e){
var dataFields = [];
for (property in e.namedValues){
dataFields[property] = e.namedValues[property][0];
}
return dataFields;
}

Related

Send an email response when a Google form is filled during specific working hours

We have requests / tickets coming across teams which are in different countries. And these requests are submitted through Google Forms. These requests / tickets are then picked at a single location and processed
There's a 5 hour non working window from 00:00 am to 5am. During this time as well we do get requests from the offices.
Is there a way if a form is submitted during this non workings hours an email notification goes to the requester letting them know that we are closed and the request submitted will be picked once we are open?
There is an option to run script. But i get an error.
If a script has to be run, how would that be possible based on the requirement?
Thanks
You can use the onFormSubmit trigger for spreadsheets. It provides all of the information for each submission via namedValues which is an object or values which is an array. As an installable trigger it is capable of performing operations that require permission like sending emails.
onFormSubmit event object
There are many examples of doing this on this site.
You will encounter such error when you tried to run your code manually using the Script Editor because your event object doesn't exist.
I tried to run your code using an on-form submit in Google Form itself and it was triggered successfully when I submit a form as an example. Google Forms events - Form submit
Using on-form submit installable trigger in Google Forms:
Sample Code:
function sendEmail(e) {
//response //getRespondentEmail()
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
//logger.log(htmlText);
var emailTo = e.response.getRespondentEmail();
var subject = "Thanks for filling the form";
var textBody = "This email requires HTML support make sure you opn";
var options ={ htmlBody: htmlText };
var today = new Date();
Logger.log(today.getHours())
if (emailTo !== undefined && today.getHours() < 5){
GmailApp.sendEmail(emailTo, subject, textBody, options);
}
}
Modifications done:
I just include an additional condition to check for the current time when the form was submitted to check if it is within 00:00 to 05:00. Refer to JavaScript getHours() Method
In this sample code, the email will only be sent when a form was submitted from 00:00 to 04:59. You can adjust the condition based on your preference if you want. You can also include checking the minutes using JavaScript getMinutes() Method
Note:
You can debug your installable triggers by adding logs in your code and by checking the Executions tab in the Apps Script Editor.

google app script: on form response, check which tab of the sheet collects the responses

I am trying to automate some extra data generated to a form response using Google App Script; (in particular, a unique identifier).
I can trigger a script on a form response, but I have multiple forms connected to this sheet document, so I need to make sure that the correct form is triggering the response, and then I would like to write the values to the correct sheet of the document.
How do I check which form is being triggered, and how can I find the sheet that collects it's responses?
I assume you've already set up the function on the sheet to capture the "on submit" event. You can get the [sheet] object from the e (event) argument.
//make sure to set up the form submit trigger from
//your script menu: Edit -> Current Project's Triggers
function onSubmit(e) {
var range = e.range
var sheet = range.getSheet();
//then do something with [sheet] object
}
I'm pretty sure you can only connect one form to a sheet, so knowing which sheet got written to will tell you which form was submitted.
Check out more about event triggers in the documentation.

Fire a POST Request using Google Sheets (on Form Submit)

I used a Google Sheet form as a newsletter registration form.
In the response form i've built a Formula which builts the request.
Column A has a timestamp
Column B the e-mail adress
Column C builts the request:
Example Output
http://newsletter.request.com/automated/action.jsp?action=SubscribetoNL&pw=XXX&email=xx#xx.com
Is there a way to have a script fire (click on or use POST) the newly created request on form submit?
thanks a lot for your help!
Well, you can not fire POST request on Form submit, but you can use of trigger functionality. You can set a trigger which is executed as soon as the form is submitted submitted. As soon as form is submitted, your custom function will fire, you can do something like this in that function,
var url = UrlFetchApp.fetch('http://newsletter.request.com/automated/action.jsp?action=SubscribetoNL&pw=XXX&email=xx#xx.com');
var responseCode = url.getResponseCode();
if(responseCode == 200) {
//do your own stuff
} else {
//do your own thing
}
Currently, doPost() is only available for standalone web apps. Refer this help article about doPost() and doGet().

Trying to launch a simple script to send a receipt after form is completed

Okay, so. I have written a simple script to send an email to anyone who completes my form on google docs. The problem is, i cannot get it to run, all i have is the guts to get the users email and send them an email. I have no means of launching the script. I have looked near and far trying to find a way to auto launch the script once the form is completed.
Tl;dr Trying to send a receipt confirming the form was completed.
here is the script:
function hallpassreceipt() {
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Get the name of the document to use as an email subject line.
var subject = 'Hall Pass';
// Append a new string to the "url" variable to use as an email body.
var body = 'This is a hall pass .';
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body); }
Read Understanding Triggers.
You need to set up an Installable Form Response trigger. One example: Google Script to email form answers and questions to a user.

How to get form details from FormResponse in google app script

I am attaching a onSubmit() trigger programmatically to different forms in my google drive. The form submit event has to trigger an email to the owner of the form. How do i access the owner of the form in my handler function. Below is my handler function. The problem i am facing is, i could not get the form reference based on the formResponse object.
function formSubmit(event) {
var formResponse=event.response;
var itemResponses = formResponse.getItemResponses();
var emailBody=formResponse.getRespondentEmail()+"has reponded to the form. Please click here "+formResponse.toPrefilledUrl()+" to view the response";
MailApp.sendEmail("owner#test.com", "Sample form submitted", emailBody);
}
In your formSubmit handler you can do FormApp.getActiveForm().getId() to get a reference to the the Form object itself. However, there is no good way to get access to the Form's owner.
You will have to rethink the usecase such that in some way you are able to grab the form owner's usecase when the form is created.
You could use:
var editorEmail = FormApp.getActiveForm.getEditors()[0].getEmail();
This is not guaranteed to be the email address of the owner, but at least one of the editors' email addresses.