Google Script Email with CC - javascript

I'm having an issue where I want to be able to send an email with CC's based off cell values. i.e. both the main recipient email and CC email are in the spreadsheet in the same row. The code should cycle through each row and send an email to the recipient and attaches the CC to that email.
My code is as follows:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 10; // Put in here the number of rows you want to process
// Fetch the range of cells A2:B4
// Column A = Email Address, Column B = First Name
var dataRange = sheet.getRange("A2:C")
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var name = row[0];
var emailAddress = row[1];
var daysAbsent = row[2];
var CC = row[3];
var message = "Hi " + name + ",\n. We see that you have been absent for " + daysAbsent + " days.";
var subject = "Attendance Notification";
MailApp.sendEmail(emailAddress, subject, message,{
cc: CC
});
}
}

Related

Trying to write a script where column h if true would return a value of same row column a to display in email message

Been playing around with this and i am new to this unfortunately. So my goal is if a row in column h is true, in the email message it would get the value of the same row as column h and display what would be column a as the message.
ALL HELP IS THOROUGHLY APPRECIATED
function CheckSales() {
// Fetch the repair scba
var monthSalesRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("H1:H20");
var monthSales = monthSalesRange.getValue();
var ui = SpreadsheetApp.getUi();
// Check totals sales
if (monthSales = true) {
sendEmail();
}
}
function sendEmail() {
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = 'A bottle is in need of repair!'; // Second column
var subject = 'SCBA Repair Email';
MailApp.sendEmail(emailAddress, subject, message);
}
It sounds like your spreadsheet looks like:
A
B
...
H
Message to send
Target email address
...
True/False
For rows where column H is true, you want to email the message in column A to the user in column B.
If that understanding is correct, this should do the trick:
function generateEmails() {
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1:H20");
var data = dataRange.getValues();
var subject = 'SCBA Repair Email';
for (var i = 0; i < data.length; i++) {
var row = data[i];
if (row[7]) {
var recipient = row[1];
var body = row[0];
MailApp.sendEmail(recipient, subject, body);
}
}
}

How to send whole row of data in my spreadsheet via email based on single cell edit (Google Script / GAS)

We have set up a workflow spreadsheet that has job information such as client name, job details etc. I have a nifty script that hides a whole row if you choose "Complete" from a drop down menu in the final column of that row.
What I am trying to do now is send an email with all the data in the row we are hiding.
UPDATED: Here is the code I have so far:
/**
* TITLE:
* Hide a row if a value is inputted then send an email
*/
//**GLOBALS**
// Sheet the data is on.
var SHEET = "Live Jobs";
// The value that will cause the row to hide.
var VALUE = "Complete";
// The column we will be using
var COLUMN_NUMBER = 22
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
//Ensure on correct sheet.
if(SHEET == activeSheet.getName()){
var cell = ss.getActiveCell()
var cellValue = cell.getValue();
//Ensure we are looking at the correct column.
if(cell.getColumn() == COLUMN_NUMBER){
//If the cell matched the value we require,hide the row.
if(cellValue == VALUE){
activeSheet.hideRow(cell)
SendEmail(e);
};
};
};
}
/**
* Sends emails with data from the current spreadsheet.
*/
function SendEmail(e) {
// Fetch the email address
var correctSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Live Jobs');
var emailRange = correctSheet.getRange('Z1');
var emailAddress = emailRange.getValue();
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell()
var rowValue = cell.getRow();
var startRow = rowValue; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, 1, 5);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var message = row; // Second column
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
}
}
Now, the hide row works, and the email function works if I run it from the Google Script editor, but for some reason, the email does not fire when I select "Complete" but IT WAS WORKING YESTERDAY!!!
Any tips?
M
Try this:
You probably can't do this with a simple trigger since sending email requires authorization.
var SHEET = "Live Jobs";
var VALUE = "Complete";
var COLUMN_NUMBER = 22
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()==SHEET){
if(e.range.columnStart==COLUMN_NUMBER && e.value==VALUE){
sh.hideRow(e.range.rowStart);
SendEmail(e);
}
}
}
}
function SendEmail(e) {
MailApp.sendEmail(e.range.getSheet().getRange('Z1').getValue(), 'Invoice Job Alert', e.range.getSheet(e.range.rowStart,1,1,e.range.getSheet().getLastColumn()).getValues()[0].join(','));
}

Send Email based on column value = yes

I have a Google Sheet where the Email & Name columns are auto-fill based on user submitted form. Also, I have 2 more columns where one column contain the list of giveaway code and another column is to act as an approval to send the email.
So for now I manage to create a button where when click it will send all emails with the giveaway code.
Now,
How can I only send the email to the user that only has a value "y" in the Status column?
Also, after sending the emails, the value "y" will changed to "d"? So that later, if I use the function again, it will only sending to the new users.
This is the sheet example https://docs.google.com/spreadsheets/d/1KkShktBnJoW9TmIzNsAuAJb6XslBrMwJGEJu7xeF1fk/edit?usp=sharing
This is the code
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Test1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:E1000");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[1];
var timeStamp = rowData[0];
var recipient = rowData[2];
var code = rowData[3];
var status = rowData[4];
var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
var subject = 'Here is your giveaway code!';
MailApp.sendEmail(emailAddress, subject, message);
}
}
/**
* The event handler triggered when opening the spreadsheet.
* #param {Event} e The onOpen event.
*/
function onOpen(e) {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'sendArticleCountEmails'}
];
spreadsheet.addMenu('Send Emails', menuItems);
}
Just add an if statement like this:
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Test1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:E1000");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[1];
var timeStamp = rowData[0];
var recipient = rowData[2];
var code = rowData[3];
var status = rowData[4];
var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
var subject = 'Here is your giveaway code!';
if (status == "y") {
MailApp.sendEmail(emailAddress, subject, message);
var actualRow = parseInt(i)+2;
sheet.getRange(actualRow,5).setValue("d");
}
}
}

Google Sheets email once based on cell values

This script works, except it sends multiple emails, This script check Column F for any value above 0 then sends an email.Column F is where i manually enter data. Trying to get it to only send one email and put a "Email Sent" in column H. Then check if column H has a "Email Sent" if so don't send email again.
Here Is what my sheet looks like
https://docs.google.com/spreadsheets/d/1o5jKMDECIFozrwAQx6EDvPL1NkoMQ1nbpkd6AdS9ULk/edit?usp=sharing
function sendEmails() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses"); //get the sheet "Form Responses"
var data = sSheet.getRange(2, 1, sSheet.getLastRow(),sSheet.getLastColumn()).getValues(); //get the values of your table, without the header
var lastRow = sSheet.getLastRow();//gets the last row of entered data
var yourColumn = "H";//set your column
var yourFunction = ("Email Sent");//whatever function or formula or data you want,just don't forget to use lastRow do do the row number
sSheet.getRange(yourColumn+lastRow).setValue(yourFunction);
for (var i = 0; i < data.length; i++) { // For each element of your tab
var row = data[i];
if(row[5] > 0) { // If the person have got at least 1 report outstanding (column E)
var body = "Good day "+ row[0] + " you have " + row[5] + " Internal Audit reports outstanding.";
GmailApp.sendEmail(row[1],"Outstanding Internal Audit Reports", body); // Sending a mail to alert
}
}
}
Try This:
function sendEmailsToTechs() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Form Responses");
//var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var data=rg.getValues();
var hRange=sh.getRange(2,8,sh.getLastRow(),1);
var hValues=hRange.getValues();
for (var i=1;i<data.length;i++){
var row = data[i];
if(row[5] && row[7]!='Email Sent') {
var body = "Good day "+ row[0] + " you have " + row[5] + " Internal Audit reports outstanding.";
GmailApp.sendEmail(row[1],"Outstanding Internal Audit Reports", body);
//Logger.log(body);
hValues[i-1][0]='Email Sent';
}
}
hRange.setValues(hValues);//Puts the values into the spreadsheet all at one time.
}

Gmail App Script Add BCC from Spreadsheet

I have created this code to send a template email from Google Spreadsheet. I really need to BCC another recipient. I have checked out Class Gmail App. It didn't quite make sense to me.
var EMAIL_SENT = "EMAIL_SENT";
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menu = [{
name: "Send Email",
functionName: "sendEmails2"
}];
ss.addMenu("Send Email", menu);
}
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = "Hello Team,\n\n" + row[1] + " Please do the Following:..."; // Second column
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Team Email";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Check the version of sendEmail(recipient, subject, body, options) method with optional arguments, which can include bcc.
Class GmailApp also has a similar method sendEmail(recipient, subject, body, options), which can include bcc.
Example:
...
var options = {
bcc: 'bccmail0#domain.ext, bccmail1#domain.ext'
};
MailApp.sendEmail(emailAddress, subject, message, options);
...

Categories