I am trying to make a simple script that sends an email to a user who fills out a survey through a Google Form.
They will take the survey, answers are recorded in a spreadsheet, and this script will email them a copy of the questions (the headings), along with their answers (Should be the last row of the table.)
The email it is sent to was going to be the last column, which is not included in the email.
This was the script I was working on to get this working:
function sendFormByEmail(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var emailSubject = "EMAIL-SUBJECT-HERE";
var yourEmail = "CURRENTLY-JUST-A-STATIC-EMAIL-ADDRESS"; //get this from a field.value then just cut off that field from being included in the email (just do column-1)...
var headers = headersRange.getValues()[0]; //google api to get the header values.
var lastRow = s.getLastRow();
var message += headers.toString();
message += (headersRange.getValue()[last]).toString(); //should get the last row of data?
MailApp.sendEmail(yourEmail, emailSubject, message);
}
Add a trigger that executes whenever a form is submitted and attach this function to your trigger.
In your code, you can use e.values[] in your code to refer to the values submitted by the user. This way you will not have to read the spreadsheet
function sendFormByEmail(e) {
var email = e.values[0]; // assuming 1st field is email
var firstName = e.values[1]; // assuming 2nd field is first name
...
}
You will probably find it hard to read the results of an email with all the questions in one line, and all answers in another.
It's easy to loop through the headers & form values at once, and just skip over the email address if you wish.
function sendFormByEmail(e) {
var emailSubject = "EMAIL-SUBJECT-HERE";
var emailQ = 1; // Column number with email address
var headers = SpreadsheetApp.getActiveSheet().getDataRange().getValues()[0]; //google api to get the header values.
var message = "Your survey responses:\n\n";
// Print Questions & Answers in pairs
for (i=0; i < headers.length; i++) {
if (i == emailQ) continue; // skip email address
message += headers[i]+"\n"
+ e.values[i]+"\n\n";
}
MailApp.sendEmail(e.values[emailQ], emailSubject, message);
}
Related
I want to write a script that will send an email based on the criteria of a single column.
This is for an inventory management system that will send emails to remind that certain items are low in count.
eg.
if row 5's column J is = "reminder", send values of row 5's column B and C to my email. (col B and C is item description and quantity count respectively)
I just started learning about google app script so I'm not sure how to write it myself, but from other forum posts, this is what I got.
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
values.forEach(function(row) {
var indent = row[10]; //column j
if (indent !== '') {
var EditedRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("store list").getRange(indent);
var Edited = EditedRange.getValue();
}
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("mail").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = Edited;
var subject = 'For Action: Indent' ;
MailApp.sendEmail(emailAddress, subject, message);
});
}
I split the code up and tested emailing and apparently the email portion of the codes work, but it is the retrieving of data that is not working and I don't really know how to get it working.
To get started, have a look at the Apps Script reference
There you will see that to retrieve the value of a single cell it is bets to use the method getValue() instead of getValues()
You need to specify the range (cell) wrom ehre you want to get the value
The easiest way to do so is with the method getRange() specifying A1 notation
A simple sample would be:
var sheet = SpreadsheetApp.getActiveSheet();
var checkValue = sheet.getRange("J5").getValue();
if (checkValue == "reminder"){
var B5Value = sheet.getRange("B5").getValue();
var C5Value = sheet.getRange("C5").getValue();
var message = B5Value + " and " + C5Value;
var emailAddress = "you need to define the email address of the recipient either in the code or a cell of the sheet";
var subject = "Define here your subject";
MailApp.sendEmail(emailAddress, subject, message);
}
Please make sure to dedicate enough time to study the Apps Script documentation to understand this code and be able to troubleshoot and perform simple modifications.
I'm setting up an email alert on my google sheet, in which if a value in one column goes below 2 I get an email alert on my mail.
I have mulitple domains and it is very tedious to check the domains expiry date every day. I created a sheet in which I put up domain name, registrar,hosted, todays date, expiry date, Expiry remaining days. Now I want to create an alert on "Expiry remaining days column", so that when the remaining days is below 2 days it send an alert email to my list of email addresses.
What I have already done is getting an email for one cell value but what I want to do is get it on whole column or number of column values.
function CheckSales() {
// Fetch the values
var domainexpireRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("domain list").getRange("F2");
var domainexpire = domainexpireRange.getValues();
// Check domain expiring
if ( domainexpire < 2){
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Company emails").getRange("B2:B4");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = '2019 smtp Domain Expiring in two days. Please check the sheet domain sheet '; // Second column
var subject = '2019 smtp Domain Expiring';
MailApp.sendEmail(emailAddress, subject, message);
}
}
I have a sheet which has multiple rows and columns, what I want to do is check column F and if any of the value on column F is less than 2 then send an alert email to my email addresses.
My code is working fine if I copy paste the code to multiple times and change the "getRange("F2");" to F2,F3,....F100 .
But it is not good way to put up a code. Can anyone tell me if I can edit some code and get the result.
I tried using "getRange("F2:F");" & getRange("F2:F54");but it doesn't worked out.
Thanks for your help.
Try using a for statement to loop through the whole sheet, the one below should work:
function checkSales() {
// Fetch the values
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("domain list");
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
// loop through each row starting at row 2
for (var i = 1; i < data.length; i++) {
var domainExpire = data[i][5];
// Check domain expiring
if (domainExpire < 2) {
var emailRange = spreadsheet.getSheetByName("Company emails").getRange("B2:B4");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = '2019 smtp Domain Expiring in two days. Please check the sheet domain sheet '; // Second column
var subject = '2019 smtp Domain Expiring';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Try this:
function CheckSales() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('domain list');
var domainexpireRange=sh.getRange(1,6,sh.getLastRow()-1,1);
var domainexpire=domainexpireRange.getValues();
for(var i=0;i<domainexpire.length;i++) {
// Check domain expiring
if (domainexpire[i][5]<2){
var emailRange=ss.getSheetByName("Company emails").getRange("B2:B4");
var emailAddress=emailRange.getValues().map(function(r){return r[0];}).join(',');
// Send Alert Email.
var dateString=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy");
var message=Utilities.formatString('%s smtp Domain Expiring in two days. Please check the sheet domain sheet.',dateString);
var subject=Utilities.formatString('%s smtp Domain Expiring',dateString);
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
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()
}
I am new to stack overflow and am looking for some advice and some guidance on a google spreadsheet i have been working on.
A demo of it can be found at
https://docs.google.com/spreadsheets/d/1t9WfcG_1_mAavpN0l3v58JdD04Y1lL3PhAEoTUkq0Z4/edit?usp=sharing
Basically the jist of it is that i require the following.
A sendEmail button in the tool bar at the top that will send a html formatted email to any email on the active page in a specific colour.
Essentially this is to allow me to send reminder to clients in groups and by colour status. Currently i have to mail each client individually using a canned response which is pretty time consuming.
So far i am using the following script as a basis, i can successfully send an email but i run into errors if email fields are empty. I am looking for it to overlook the empty fields and continue searching for an email address. Also i can only get it to locate a template of an active sheet but would require it to locate a template of another sheet and use that. Finally when i try adding in HTML the script successfully picks up the template but sends it as plain text and its not sent as HTML.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 100; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 100)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[8]; // Eight column
var message = row[15]; // Fifteenth column
var subject = "Welcome To Phorest";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Looking forward to speaking with someone that can help me work all this out and save me some data entry in the process.
Appreciated.
You can send an HTML-containing email the following way:
var email = "some#email.com";
var subject = "Hello!";
var html = '<p> What is your favourite fruit? </p> <br>' +
'<select>' +
'<option value="apple">Apple</option>' +
'<option value="orange">Orange</option>' +
'</select> ';
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: html
});
I am stuck at a point in some code and need some expert help. I want to reference column "E" on the sheet "Form Responses", which will return a name of a person. The same name can be found in column "A" of sheet "Email". In column "B" of sheet "Email" will be the email address that I want to send data to. I am stuck at how to produce this email address. Here is what I have so far...
function emailData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Form Responses");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+(lastRow)+":AK"+(lastRow)).getValues();// get the range and values in one step
var headers = responses.getRange("A1:AK1").getValues();// do the same for headers
var recipient = responses.getRange("E"+(lastRow)).getValues();
var emailSheet = ss.getSheetByName("Email");
var names = emailSheet.getRange("A2:A20").getValues();
var emails = emailSheet.getRange("B2:B20").getValues();
var subject = "Capacity Campaign Form";
var message = composeMessage(headers,values);// call the function with 2 arrays as arguments
Logger.log(message);// check the result and then send the email with message as text body
MailApp.sendEmail(recipient,subject,message);
}
function composeMessage(headers,values){
var message = 'Here is the data from the form submission:'
for(var c=0;c<values[0].length;++c){
message+='\n'+headers[0][c]+' : '+values[0][c]
}
return message;
}
I must give props to #Serge for helping me with the array. Alas, any help that you could provide would be awesome!
You've got the names and email addresses you need to search, in two 2-dimensional arrays. Each of those arrays is an array of rows. A simple search would be this:
var email = ''; // If we don't find a match, we'll fail the send
for (var row=0; row < names.length; row++) {
if (names[row][0] == recipient) {
email = emails[row][0];
break; // end the search, we're done
}
}
...
MailApp.sendEmail(email,subject,message);
There are more elegant ways to do this, I'm sure, but this should work for you.