Filling Google Forms with Google Sheets - html

I have an HTML form which saves its responses to a Google Sheet. The form contains the following fields:
Name:
Email:
Subject:
Message:
Now, what I want is I want to automate sending a thank you email to a recipient as soon as he/she fills the form to the address mentioned in the "Email" field. I don't want to use Google Forms as the backend as it is hard to bypass the "Response Recorded" Confirmation page. Also, avoiding PHP would be better for me!
Is there a way to send these e-mails automatically? The format of the email is as follows:
From: <my email address>
To: <email address from the "Email" field in the spreadsheet>
Subject: Re: Submission Received
Hey <name from the "Name" field in the spreadsheet>!
Body of the mail
If there is a way to bypass the confirmation page of Google Forms, please let me know! That would also do!

If you have the data in google sheets, then you can handle your automation there. I setup this sample file that you could probably base your data set on.
From there I attached the below script to the spreadsheet. You would then need to set a trigger to execute this script on a somewhat regular frequency (5 minutes? 1 minute?). I think it should accomplish what you are going for. There's a check built in to ensure that partial data is not sent.
const ss = SpreadsheetApp.getActiveSheet();
const sentMessageColumn = ss.getRange("E:E").getColumn();
function regularProcedure(){
var aCell = ss.getRange(ss.getLastRow(),sentMessageColumn)
while(aCell.isBlank()){
var eRow = aCell.getRow();
if(!(ss.getRange(eRow,2).isBlank() ||
ss.getRange(eRow,3).isBlank() ||
ss.getRange(eRow,4).isBlank()))
{
var newMail = GmailApp.createDraft(
ss.getRange(eRow,2).getValue(),
ss.getRange(eRow,3).getValue(),
ss.getRange(eRow,4).getValue());
newMail.send();
aCell.setValue(true);
}
aCell=aCell.offset(-1,0);
}
}

Related

How to have Google Forms send automated email with new responses

I have a Google Form which I would like to automatically email someone when a new response is submitted. So far, I have just a simple HTML page with text in the body, however I would like the email content to include the form data as well.
Currently, this is what I have written:
function sendEmail(e) {
//response
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
var emailTo = "jeffreyabr#gmail.com"
var subject = "New SAP Role Request"
var textBody = "This email requires HTML support. Please make sure you open it with an email client that supports HTML"
var options = {htmlBody: htmlText};
GmailApp.sendEmail(emailTo, subject, textBody, options);
This came from following this basic YouTube tutorial.
Is there more Google Apps Script that I can add to accomplish this? Can I do this from Forms or must I do it from within Sheets?
The e.response object also contains the form data, which can be accessed by using e.response.getItemResponses().
Then to get the question, use getItem().getTitle(). To get the answer, use getResponse().
If you do not need the HTML response, then you can append the questions and answers to the textBody to display them on the email. Otherwise, you would have to add a script in your email.html using HTML scripts or google.script.run.
References:
Event Objects | onFormSubmit(e)
Class FormResponse

Allow only selected account to submit a Google Form

Is there a way (Apps Script maybe?) to inform google forms submitter that his submission will not be gaethered because the form is restricted to selected google accounts?
My try looked like this, but it has 2 problems :
-it is accepting answers submitted from people other than verified and verified2 (no idea how to add that)
-it only displays my custom message if someone tries to submit second answer
function onFormSubmit(e){
var af = FormApp.getActiveForm();
var defaultClosedFor = af.getCustomClosedFormMessage();
af.setCustomClosedFormMessage("You are not using an account with submission access. Please log in to account with correct authorization");
var responses=af.getResponses();
if(responses[responses.length-1].getRespondentEmail()=="verified#gmail.com" ||
responses[responses.length-1].getRespondentEmail()=="verified2#gmail.com"
){
af.setCustomClosedFormMessage(defaultClosedFor);
}
}
I see multiple problems with the code:
Custom Closed Form Messages are only displayed when the form is closed for responses manually or the form only allows one response per email account.
Email addresses are not collected on the form. It has to be enabled manually on the Form settings.
Solution:
Enable Collect Email Addresses option on Google Form Settings:
Use setConfirmationMessage() to display custom messages depending on the entered email when the form is submitted.
Sample code:
function createTrigger() {
var form = FormApp.openById('****your-form-id****');
ScriptApp.newTrigger('validateForm')
.forForm(form)
.onFormSubmit()
.create();
}
function validateForm(e){
var af = FormApp.getActiveForm();
af.setCollectEmail(true);
var responses=af.getResponses();
var defaultConfirm = "Your response has been recorded.";
var rejectMessage = "You are not using an account with submission access. Please log in to account with correct authorization.";
if(responses[responses.length-1].getRespondentEmail()=="verified#gmail.com" ||
responses[responses.length-1].getRespondentEmail()=="verified2#gmail.com")
{
af.setConfirmationMessage(defaultConfirm);
}
else {
af.setConfirmationMessage(rejectMessage);
af.deleteResponse(responses[responses.length-1].getId());
}
}
Note: Please run createTrigger() first manually in GAS to create the on form submit trigger.

Google Sheet/script trigger for auto email when new cell value is yes/no

New user here and not versed in code much. Im working on a COVID-19 form for our company and looking for some help with google script/trigger where when an employee fills out the google form and selects yes/no on the google form the google sheet that collects the data will send off an email based on the value in the cell.
IE: employee A enters "no" to agreeing to comply to policy it will email the manager informing them that someone entered "no".
I have a test formula that is working but when I set up a min by min trigger for it to kick off it just continues to kick off. Im assuming this is obviously due to it not having a code to only send new entries??
Any help would be greatly appreciated.
To reiterate im trying add a script that sends an email only ONCE per user when someone fills out the google form and they choose the wrong answer.
Code I have now:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var currentValue = sheet.getRange("F2:F1000").getValue();
if (currentValue = ("Yes")) {
MailApp.sendEmail("test#example.com", "ALERT: Please see person in question!", "The message body that you want to send.");
}
}
Try this:
You haven't identified the person in question.
function onMyEdit(e) {
const sheet=e.range.getSheet();
if(sheet.getName()=='Sheet1') && e.range.columnStart==6 && e.range.rowStart>1 && e.value=='No') {
//MailApp.sendEmail("test#example.com", "ALERT: Please see person in question!", "The message body that you want to send.");
Logger.log("ALERT: Please see person in question!");
}
}
Since this is sending an email which requires authorization you will have to create an installable trigger using the Edit/Current Project Triggers menu.

Google Sheet Bespoke Reporting: Filter & Privacy

I seek an approach by which I can provide user a bespoke Google Sheet dashboard on the basis of some ID that is entered or transferred via URL.
To explain: as of now, raw data sits in a master Google Sheet and is processed and summarised in another Google Sheet dashboard that requires to enter an ID which acts as filter to the raw data so that the summary only presents insights associated with that particular ID and user - that works.
However, Each user should enter only their ID and see their summary. Right now all users have access to the same public Sheet and the possibility of parallel access is problematic.
How may I generate individual Sheets (one per user) that is based on a template?
Is this possible with default functionality, or Apps Script? Any advice is highly appreciated, thank you!
This is one option you can try, you can get the email ID of the user who opens the google spreadsheet like so:
function onOpen(e) {
var email = Session.getEffectiveUser().getEmail()
var ui = SpreadsheetApp.getUi()
ui.alert(email)
//doSomethingSpecificBasedOnEmail(email) call function that fliters data based on email ID
}
Note: There are few nuances to using Session.getEffectiveUser(). Based on permission and security setting, it can give you a blank user/email.
https://developers.google.com/apps-script/reference/base/session#getActiveUser()
Second Option:
If email ID is not an option and since you are ok with users entering an ID to access the data. This code will ask for an ID and create a copy of sheet called template and also set the value of A1 as the ID. The sheet can then use the ID to get ID specific data and make plots.
function onOpen(){
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Get ID:');
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
var id = response.getResponseText();
var ss = SpreadsheetApp.getActive()
var lookupSheet = ss.getSheetByName(id)
if(lookupSheet == null){
var template = ss.getSheetByName("Template")
var newSheet = template.copyTo(ss)
newSheet.setName(id)
newSheet.getRange(1,1).setValue(id)
newSheet.activate()
} else
{
lookupSheet.activate
}
} else {
Logger.log('The user clicked the close button in the dialog\'s title bar.');
}
}
An example of this can be found here: https://docs.google.com/spreadsheets/d/1RPHUGKi7u9jJVc-3xCaYlrZ8kaHWvpl6gPZxUL1g32Y/edit?usp=sharing
Note: People can see each others sheet though, which I assume based on your post is not an issue. However, if that is not the case the best option is to use google Web App script.

Google Apps Script — Automatically Email and Share

I am attempting to write a small Google Apps script that will send a confirmation email and automatically share a folder with a logged-in user after they complete a form. Here is the script:
function formSubmitReply(e) {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: "Keuka College Brand Download Center",
htmlBody:
"<p>Thank you for requesting access to the Keuka College Brand Download Center. Your access has been approved.</p>" +
"<p>You may access the download center by <a href='https://drive.google.com/a/keuka.edu/folderview?id=0B856ZeGoaGT_MG5BMEtEVGwzYkk&usp=sharing'>using this link,</a> " +
"visiting <a href='http://brand.keuka.edu'>Keuka College Brand Central,</a> or through your Google Drive.</p><p>Please contact the Office of Marketing and Communications " +
"with any questions you may have.</p>",
name: "Keuka College",
replyTo: "marketing#keuka.edu"
});
var folder = DocsList.getFolder('Brand Download Center');
folder.addViewer(Session.getActiveUser());
}​
This seems to be working, except it keeps emailing it to me -- not the user who is completing the form. I am not sure if it is sharing correctly.
Could someone provide some insight? This is my first time working with Google Apps script.
Session.getActiveUser() is the user that uses the form editor and the user that created the trigger that calls this function.
What you want us the user that filled the form.
Look at the documentation here to see how you can retrieve that value. Note that it will only be available if you are in a domain.
EDIT (sorry for the delay)
Here is a sample code to show how to get the form respondent in formApp (works only in GAFE or GA Business)
It will send you a mail with the name of the last form submitter (don't forget to create an installable onEdit trigger to test this).
function onSubmit(){
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var lastResponse = formResponses[formResponses.length-1];
var user = lastResponse.getRespondentEmail()
var userString = 'user = '+user;
MailApp.sendEmail(Session.getActiveUser().getEmail(),'form submission report',userString);
}