Google Sheet to send an email when cell has specific value - google-apps-script

I have a Google Form for students to input data and I would like an email sent to myself or another person if a specific question has a certain answer. In the email I would like the name of the student that submitted the form to be included in the email. The problem I'm having is the email is only sending the information from Line 2 of my Sheet. (I have line 1 Frozen as headers). I need it to pull from the last line of data entered from the Google Form.
Google Form - https://docs.google.com/forms/d/1r1Tsyfl71zfzgzhs3-_qFB57FcSSEX1zS5BkTgz-ESA/prefill
Google Sheet Data - https://docs.google.com/spreadsheets/d/1P1RD7My91jLSHS9hgEOBPv5oYnfCJQviN6Nzy_cH7Cc/edit?usp=sharing
Here is my script code:
function CheckStudent(e) {
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
{
var studentData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange("C2");
var studentData = studentData.getValue();
var message = studentData + ' is not doing well today!' // Second column
var subject = 'Student Daily Check Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
}

If you are already registering the email in the sheet submitted by the form, then is as easy as creating a installable trigger.
You can do that using the event object.
If you are unfamiliar with triggers then I recommend you to have a read at the official documentation in there you can see a few examples of how to create them:
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("CheckStudent") // You will need to adapt your function
// to recieve the event parameter
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
Once you have executed the previous code then your function will trigger every time someone submit the form.
EDIT
Now taht I think about you can just do the trigger directly in the form object not going through the sheets.
Changing the event object code slighlty:
var form = FormApp.getActiveForm();
ScriptApp.newTrigger('CheckStudent')
.forForm(form)
.onOpen()
.create();

Related

How to code a Google form to add a link to view form submissions with questions

I have looked at the Google Apps script how-tos, but it is not self-explanatory to a newbie like me.
I have also tried to adapt online video scripts to what I need and they always return errors.
I need help with how to enter the code below so it generates a link on the form response spreadsheet that shows the complete form with the answers that users submitted.
Below is the code I found on the Google Apps script, but I don't know how to enter it.
toPrefilledUrl() generates a URL for the form in which the answers are pre-filled based on the answers in this form response.
function addFormResponseUrl(e) {
// Get the Form response URL and add it to the Google Spreadsheet
var responseUrl = formResponse.getEditResponseUrl();
var row = e.range.getRow(1);
var responseColumn = 1; // Column where the response URL is recorded.
responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}
In order to retrieve the EditResponseUrl and paste it into the destination spreadsheet next to the form response:
Write a function that retrieves the latest form response and pastes the respective EditResponseUrl into the destination spreadsheet
Bind the script to the destinaiton spreadsheet
Attache to the function an installable FormSubmit trigger
Sample script:
function addFormResponseUrl(e) {
//open the form by its id
var form =FormApp.openById("XXX");
var formResponses = form.getResponses();
// get the latest form response
var lastResponse = formResponses[formResponses.length-1];
//get the response URL
var responseUrl = lastResponse.getEditResponseUrl();
// get the sheet where the responses have been stored
var responseSheet = SpreadsheetApp.getActive().getActiveSheet();
//get the row where the last response has been stored
var row = e.range.getRow();
// Safe the response Url in the first free column after the form responses.
var responseColumn = e.values.length+1;
responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}

Delete the emails stored in a Google Form's responses

Background:
I am trying to create a google form that will be filled out by people, and then send a second form to be filled by someone else (couple). The form response needs to be anonymous, but the responses will also need to be linked.
My solution so far - using a unique ID for linking (kudos -> How to assign a unique ID to a google form input?) then using the email inputted to send off a message with a pre-made link to the next form with the ID and then delete the email address from the sheet.
The Question
The next sanitation which I am stuck on - is removing the email address form the Google Form response. One way would be to use deleteAllResponses() but that will also delete the basic data analysis pre-done by Google that I intend to use (to save time on creating some of the graphs).
I tried to find a way to access the response object and delete/modify the email field, but was unable to achieve that.
Would appreciate the help.
function myFunction() {
//delete the last email that was added
function deleteEmails(Row){
sheet.getRange('B'+Row + ':B'+Row).clearContent();
}
//delete all responses - would like to change this to only delete/modify the email field.
function deleteResponses() {
var form = FormApp.openById('The form ID');
form.deleteAllResponses();
}
//get the data on the sheet
var sheet = SpreadsheetApp.openById("The sheet ID").getSheetByName('Form Responses 1');
var StartRow = 1;
var LastRow = sheet.getLastRow();
var Range = sheet.getRange(LastRow, 2, 1, 62)
var AllValues = Range.getValues();
//if the person disagrees to fill the form - dont send an email
if (AllValues[0][1] != 'disagree') {
var SendTo = AllValues[0][0];
var body = "Thank you for answering pfa a link for the sedond form";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: 'Thank you for answering',
htmlBody: body,
});
}
//delete emails and responses
deleteEmails(LastRow)
deleteResponses()
}

Google form to answer sheet to separate sheet to email

I am trying to work out a solution to something and whilst I can get snippets of help from the weeb I cannot pin it down to what I want it to do. So, what I want to achieve:
A google form is submitted which creates a record in a google spreadsheet.
using the query function I take that information instantly to another sheet in the same spreadsheet. Whilst in there it goes through some if statements which creates a unique ID for the submission, separates the date and the time of the submission into separate columns and adds a email address that needs to be notified of this submission. For the purpose of this test, the submitter and the notification emails are the same.
the script monitors the second sheet and, on form submission (trigger), takes the information from the last row in a certain sequence to create an email:
function formsubmission(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetbyName('email');
var startRow = 2;
var lastRow = e.sheet.getLastRow();
var formid = e.sheet.column[1];
var notificationemail = e.sheet.column[2];
var formdate = e.sheet.column[3];
var formtime = e.sheet.column[4];
var submitter = e.sheet.column[6];
var yn = e.sheet.column[7];
var subject = formid + "submitted";
var message = "Thank for answering" + yn + "on" + formdate + "at"+ formtime;
MailApp.sendEmail (notificationgroup, subject, message,{
cc: submitter,
});
}
Needless to say that the above script does not work.
If any of you kind people want to help. The form is test form to which the above refers is here and the related spreadsheet is here.
I would prefer not to have a separate sheet but the one accepting submissions does not allow for any functions or anything else in it.
Any help to make the above script work would be greatly appreciated.
Thank you

google apps script trigger event upon update from external source

I have a Google Sheet that is updated via "Zapier" from my CRM (Capsule) application. I need to auto trigger an email to a given address whenever the spreadsheet is updated. The CRM software successfully adds a new record to the end of the spreadsheet when a new organization is created. I have a script that monitors the last row and sends an email to the addressee by an on change event but this only happens if I go in and change the spreadsheet data myself.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var dataRow = sheet.getLastRow();
var emailCell = sheet.getRange(dataRow, 4);
var emailAdd = emailCell.getValues();
var newRecordSource = sheet.getRange(dataRow, 1, 1, sheet.getLastColumn());
var newRecord = newRecordSource.getValues();
//var message = "This record has just been added:" +newRecord;
var message = "New record added to Workflow Sheet!: " +newRecord;
var subject = "Test from Workflow Sheet!";
MailApp.sendEmail(emailAdd, subject, message);
}
Can anyone help?
If the emails will be sent to a Gmail address then you can do this without Apps Script: Log into the account that will receive the emails, open the relevant spreadsheet, and select "Notification rules..." from the Tools menu and set it up as desired.
If you must do it from Apps Script, you will probably need to use a time-based trigger to monitor for changes, which is considerably more complicated than using onChange, unfortunately.

Google sheets script that sends a notification upon form submission if data is recorded in a specific column

I'm trying to create a script for new google sheets that sends a notification if a form is submitted and contains data in a specific column.
In this example, if the question that populates data in column E is answered in the form, I would like an email notification with a link to the spreadsheet.
In the below script, if I set the trigger to on edit it will work if column E is edited. I cannot get it to work with on form submit, however. Can anyone help with fixing this script to work on edit?
Thanks!
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "email#email.com";
var subject = 'subject';
var body = 'body';
if (cell.indexOf('E')!=-1) {
MailApp.sendEmail(recipients, subject, body);
}
}
when your script runs on a form submission trigger , there is no such thing as active cell, the form data is inserted in the last row of the sheet.
You only have to look at this last row and check the right column (getLastRow() and column 5 for "E").