https://docs.google.com/spreadsheets/d/1tf99kQdDSGxvj8f9B383W1NZRHRsd-b6bUcl9FJAOmQ/edit?usp=sharing
I want to send email from google sheets based on the criteria in column D and subject and messages choose as per criteria.I lost it in the line to choose the three different criteria(Yes,No,Confirm) and couldn't loop it.The email will be sent to different ids in sheet1 table.
How can i achieve this in google sheets scripts and how to send emails when new entry are made into the sheet?
Any solutions would be highly helpful for my project.
Googlesheet newbie.
function checkValue(e)
{
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(“Sheet1”);
var valueToCheck = sheet.getRange(“D2:D”).getValue();
var rangeEdit = e.range.getA1Notation();
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet1');
var n=sheet1.getLastRow();
if(rangeEdit == “D2:D”)
{
if(valueToCheck = Yes )
{
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet1');
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
var subject = sheet1.getRange(i,2).getValue();
var message = sheet1.getRange(i,3).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
}
You can add Google Forms as a way to populate the data in your sheet. Using forms will give you access to new entry data which you can use to send an email.
How to add Google Forms to Google Sheet?
In your spreadsheet, click on Insert and select Form
How to determine if new entry is added and how to access the data?
You can use Installable Triggers. Installable Triggers run a function automatically when a certain event, such as submitting a form, occurs. When a trigger fires, Apps Script passes the function an event object as an argument, typically called e. The event object contains information about the context that caused the trigger to fire. You can follow the steps here on how to create Installable Trigger.
Note: For this project, make sure to use On form submit as event type.
Example:
Form:
Code:
function sendEmail(e) {
var data = e.namedValues;
var msgContent = {
'Yes':{
'Subject': 'Receipt of your goods',
'Message': 'We acknowledge receipt of goods.',
},
'No':{
'Subject': 'Non delivery',
'Message': 'We have not received the goods.',
},
'Confirm':{
'Subject': 'Confirm',
'Message': 'Confirm your message.',
}
}
var emailAddress = data['Email'][0];
var criteria = msgContent[data['Criteria'][0]];
var subject = criteria['Subject'];
var body = criteria['Message'];
MailApp.sendEmail(emailAddress, subject, body);
}
Output:
Related
I have a sheet with data that must serve as input parameters to emails in gmail.
Here is an example sheet, but basically this is it:
So, the email must be copied to the people in Column A ("Emails"), it should have a defined subject, which is on B2 and a defined body text in C2.
Is there a way to create an Appscript that connects Google Sheets to Gmail and creates an email like this below? Could you suggest a script for that based in the example sheet?
I have an example code that might work for you this would take the rows of the emails and send different emails:
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet4');
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
var subject = sheet1.getRange(i,2).getValue();
var message = sheet1.getRange(i,3).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
}
By inserting a Google Drawing:
You add a button via the Insert > Drawing menu.
You edit the drawing and after finishing it, you click on the three dots and assign a script, you just need to write the function name, in my case it is the
sendEmail
You can take a look at the Sheet and how it looks, very similar to yours.
Also if you are looking only to send a template email to all the emails from column A, you can use this one:
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet4');
var sheet2=ss.getSheetByName('somesheet');
var subject = sheet2.getRange(2,1).getValue();;
var message = sheet2.getRange(2,2).getValue();
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
}
Note:
-In order to use the template example, I used two workbooks or worksheets.
Try
function sendEmail() {
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var emailAddress = sh.getRange('A2:A'+sh.getLastRow()).getValues().join();
var subject = sh.getRange('B2').getValue();
var message = sh.getRange('C2').getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
instead of a button, you can add custom menu
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem('👉 send emails', 'sendEmail')
.addToUi();
}
To send email using App Script it only needs one line
MailApp.sendEmail(emailAddress, subject, message);
You have all these required information in your sheets that you can pull.
You can use the code below. Just change the Spreadsheet URL.
This works by looping through your rows starting from row 2 up to what row contains data to get the email address, subject, and message.
function sendMultipleEmails(){
var ss = SpreadsheetApp.openByUrl('<your_SpreadSheetURL>');
var sheet = ss.getSheetByName('Sheet1');
var n = sheet.getLastRow();
//loop through rows with data
for (i = 2; i < n+1; i++){
var emailAddress = sheet.getRange(i, 1).getValue();
var subject = sheet.getRange(i,2).getValue();
var message = sheet.getRange(i,3).getValue();
MailApp.sendEmail(emailAddress,subject,message);
}
};
Just assign this function to your button and it should work.
Refer to this link for more information on sending Emails from SpreadSheet using App Script:
https://productivityspot.com/automatically-send-emails-from-google-sheets/
On how you can add function to button:
https://spreadsheet.dev/buttons-in-google-sheets
I have a sheet named "Credentials" populated with the following data:
And another sheet in the same spreadsheet named "Clients" which is linked to a google form:
My goal here is to show on the confirmation message the data from the "Credentials" sheet which is in the same row as the row that is filled when a form is submitted. E.G: the form data that was stored in row 2 should show "usr a" and "pass a" on the confirmation message
I have this apps script to do so but it always shows the last data from the "Credentials" sheet on the form confirmation message and i can't see why:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet_clientes = ss.getSheetByName('Clients')
var sheet_demos = ss.getSheetByName('Credentials')
var form = FormApp.openByUrl(ss.getFormUrl())
var demosRange = sheet_demos.getRange('A2:B4')
var demosData = demosRange.getValues()
var lr = sheet_clientes.getLastRow()
var user = demosData[lr][0]
var password = demosData[lr][1]
form.setConfirmationMessage('Congratulations: Your user is:\n' + user + '\n Your password is:\n' + password)
}
You can use the Form Submit Trigger of Google Apps Script to run the script whenever a response was submitted.
But for setConfirmationMessage(message) Bryan P explained that
we can't conditionally set and immediately display a custom
message for the current user's response based on what their answers
were. The confirmation message is "front-loaded" in a sense.
Since we cannot use setConfirmationMessage() for this case, I created an alternative that will send the credentials to the email address the provided in the response.
Try this:
Code:
function onFormSubmit(e) {
var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var spreadsheet = sheet.getParent();
var credSheet = spreadsheet.getSheetByName("Credentials");
var creds = credSheet.getRange(row, 1, 1, 2).getValues();
var values = e.namedValues;
var email = values['Email Address'];
var username = creds[0][0];
var password = creds[0][1];
var message = 'Your user is:' + username + '\nYour password is:' + password;
var subject = 'Credentials';
GmailApp.sendEmail(email, subject, message)
}
Trigger Setup:
In your Apps Script, go to the left side menu and click Triggers.
Click Add Trigger.
Copy the setup below.
Click Save
Note: Make sure to copy and save the code provided above before creating the trigger.
Example:
References:
Event Object
Class Range
Class Sheet
Class GmailApp
Say I have a spreadsheet in google sheets with three columns EMAIL, TOPIC and TIME where I´d like to send an email to each of the EMAILs in the spreadsheet containing a link to a google form that has a question asking the preferred TIME for the given TOPIC in the spreadsheet. Is it doable to create such individualized google forms based on sheets?
The first thing that should be done is creating the forms and sending the emails. To do that, I wrote a function that loops through all the rows in your sheet (called "Sheet1", change it according to your preferences), creates a form for each row and sends it to the emails found in column A (in the sheet I've been working on, data starts at row 2 and columns are: A - email / B - topic / C - time):
function sendMails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var values = ss.getSheetByName("Sheet1").getDataRange().getValues();
for(var i = 1; i < values.length; i++) {
var email = values[i][0];
var topic = values[i][1]
var formName = email + " - " + topic;
var form = FormApp.create(formName);
var url = form.getPublishedUrl();
form.setTitle(formName);
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); // This line bounds the created form to your spreadsheet so that responses will be written in here, in an automatically created sheet
// You can set another destination spreadsheet if you don't want all these sheets created in your spreadsheet
var question = form.addDateTimeItem();
question.setTitle("Please provide a preferred time for your assigned lecture on " + topic);
var userEmail = form.addCheckboxItem();
userEmail.setChoices([userEmail.createChoice(email)]);
var topicName = form.addCheckboxItem();
topicName.setChoices([topicName.createChoice(topic)]);
var checkBoxValidation = FormApp.createCheckboxValidation()
.requireSelectExactly(1)
.build();
userEmail.setValidation(checkBoxValidation);
topicName.setValidation(checkBoxValidation);
MailApp.sendEmail(email, topic, url);
}
}
Next, you need to install an onFormSubmit trigger in your spreadsheet. You can do this to run a function that will write the preferred TIME the user chose in the form every time a form is submitted. To create the trigger, run this function in your script, only once:
function createOnFormSubmitTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('writeTime')
.forSpreadsheet(ss)
.onFormSubmit()
.create();
}
Finally, below is the function that the trigger trigs when a form is submitted. It looks for the row where topic and email match the ones coming from the form, and sets the time:
function writeTime(e) {
var response = e.values;
var time = response[1];
var email = response[2];
var topic = response[3];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();
for(var i = 1; i < values.length; i++) {
if(values[i][0] == email && values[i][1] == topic) {
sheet.getRange(i + 1, 3).setValue(time);
}
}
}
I hope this is useful to you.
I am completely new to coding anything related to Google Apps Script and JavaScript in general.
I've adapted a script to my needs, but I am getting the following error when I run it:
TypeError: Cannot find function getCell in object Sheet
Essentially, I am trying to get the value in cell D4 (4,4) and pass that value to the variable emailTo. I'm obviously not doing it correctly. The rest of the script should work fine. Any guidance is appreciated.
// Sends PDF receipt
// Based on script by ixhd at https://gist.github.com/ixhd/3660885
// Load a menu item called "Receipt" with a submenu item called "E-mail Receipt"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
var submenu = [{name:"E-mail Receipt", functionName:"exportSomeSheets"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Receipt', submenu);
}
function exportSomeSheets() {
// Set the Active Spreadsheet so we don't forget
var originalSpreadsheet = SpreadsheetApp.getActive();
// Set the message to attach to the email.
var message = "Thank you for attending ! Please find your receipt attached.";
// Construct the Subject Line
var subject = "Receipt";
// THIS IS WHERE THE PROBLEM IS
// Pull e-mail address from D4 to send receipt to
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var emailTo = sheet.getCell(4, 4).getValue();
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);
// Find and delete the default "Sheet 1"
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
// Make the PDF called "Receipt.pdf"
var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Receipt.pdf',content:pdf, mimeType:'application/pdf'};
// Send the constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the wasted sheet
DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true);
}
The issue is that getCell() is a method of Range, not Sheet. Get a Range from the Sheet, then use getCell() on the Range object
I have script which creates a new sheet in a google spreadsheet under the names of the users who fill out the form of the spreadsheet.
I would like this script to be triggered whenever a new user fills out the form associated with this spreadsheet. of course, a new user means a new E-mail address is added to the automatically generated "Username" column in the spreadsheet.
This is my code currently:
function MakeSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("default sheet");
var getEmail = Browser.inputBox("user Email: ");
var s1 = ss.getSheetByName("list of users and emails");
var index = s1.getRange(1,1,s1.getLastRow(),1).getValues();
var values = s1.getRange(1,2,s1.getLastRow(),1).getValues();
nextIndex: for( var i in index) { if( index[i][0] == getEmail ) { var getName = values[i][0];continue nextIndex;}}
ss.setActiveSheet(ss.getSheetByName("default sheet"));
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
ss.setActiveSheet(ss.getSheetByName("Copy of default sheet"));
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(getName);
SpreadsheetApp.getActiveSheet().getRange('B3').setValue(getEmail);
SpreadsheetApp.getActiveSheet().getRange('C3').setValue(getName);
}
The solution is to have a trigger on form submit. In the trigger function, you can check if the user is a 'new user'. By the way, I'm not sure how you get the email address - you either have to be a Google Apps user or you should be asking the email address explicitly.