Trouble with simple google script send email with cc - csv

Please someone help me with this code.I need to add cc with different emails in 1 column.
Hope someone can help me.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 20; // Number of rows to process
// Fetch the range of cells A2:C3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "EXCEPTION REPORT";
MailApp.sendEmail(emailAddress, subject, message);
}
}

Using the options parameter of .sendEmail as in the reference.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 20; // Number of rows to process
// Fetch the range of cells A2:C3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var ccAddress = row[--index of cc column--];
var message = row[1]; // Second column
var subject = "EXCEPTION REPORT";
MailApp.sendEmail(emailAddress, subject, message, {cc : ccAddress});
}
}

Related

Not able to copy sheet1 data to sheet2 in specific format

https://docs.google.com/spreadsheets/d/18odtLaamxJISS7Gq-N-xPxVu1Fk55AYQoFojkmIgxbM/edit#gid=0
function myFunction() {
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Messages from BOs");
var startRow = 4; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A4:C3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1];
var message = row[2]; // Second column
MailApp.sendEmail(emailAddress, subject, message);
}
for (var i in data, i++) {
var row1 = data[i];
Browser.msgBox(data[i+1])
/**
var emailAddress1 = row[0]; // First column
var subject2 = row[1];
var message3 = row[2]; // Second column
*/
var ad = row1;
var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
ts.getRange(startRow, i+1 ).setValue(row1);
//i = i + 1;
}
}
}
Here i wanted to update sheet2 every time when email send to "Recipient EMAL ID" to the new row of sheet2.
Please see my google sheet. I tried below code but it does not work well. If any who can help me in solve this problem.
Try to change:
ts.getRange(startRow, i+1 ).setValue(row1);
with:
ts.appenRow(row1);
https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)
This is a lot simpler:
function sendEmails() {
const ss = SpreadsheetApp.getActive()
var sh = ss.getSheetByName("Messages from BOs");
var ts = ss.getSheetByName('Test');
var sr = 4; // First row of data to process
var rg = sh.getRange(sr, 1, 1, 3);
var vs = rg.getValues();
vs.forEach(row => {
MailApp.sendEmail(row[0], row[1], row[2]);
ts.appendRow(row);
});
}

Google Script is successfully executing. But Mails are not sent

Greetings to all my learned friends.
I am trying to send mails from my google spread sheet through google scripting. The following is the script. The script is executing successfully. The mails are not sent.
function sendEmails() {
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 (i in data){
var row = data[i];
var Email = row[0]; // First column
var Subject = row[1] // Second column;
var Message = row[2]; // Third column
file = DriveApp.getFilesByName('Marks.xls');function sendEmails() {
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 (i in data) {
var row = data[i];
var Email = row[0]; // First column
var Subject = row[1] // Second column;
var Message = row[2]; // Third column
file = DriveApp.getFilesByName('Marks.xls');
file2 = DriveApp.getFilesByName('Remuneration.pdf');
if (file.hasNext() && file2.hasNext()) {
MailApp.sendEmail(Email, Subject, Message, {
attachments: [
file.next().getAs('application/msexcel'),
file2.next().getAs('application/pdf')],
name: 'Automatic Emailer Script'}
)
}
}
}
file2 = DriveApp.getFilesByName('Remuneration.pdf');
if (file.hasNext() && file2.hasNext()) {
MailApp.sendEmail(Email, Subject, Message, {
attachments: [
file.next().getAs('application/msexcel'),
file2.next().getAs('application/pdf')],
name: 'Automatic Emailer Script'}
)
}
}
}
Can somebody help?

How to send email if cell date equals today

I am trying to create a Google Apps Script that works with a spreadsheet to send out an email if certain criteria is met. Specifically to send out an email if Column C equals today & column A equals false.
Link to the spreadsheet:
https://docs.google.com/spreadsheets/d/1kULWOMtZaay6PcgF5XTwVoJnKPo7JSA_AK50J8RNYzk/edit?usp=sharing
I was able to set this up to that the spreadsheet handles most of the work. Column D checks for the date, and that column A is checked and then the script will send when column D reads TRUE. I am wondering if I can have the Google Apps Script check for today's date, instead of the spreadsheet.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 50; // Number of rows to process
var numOfColumns = sheet.getLastColumn();
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, numOfColumns);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var sendTrigger = "";
var i = 0;
for (i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[4]; // fifth column
var message = row[5]; // sixth column
sendTrigger = row[3];
if (sendTrigger == 1) {
var subject = ("This is a test of the send email function");
MailApp.sendEmail(emailAddress, subject, message);
};
};
};
I want the script to check column A and column C and send out an email if Column A equals FALSE and column C equals TODAY
Try this:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 50; // Number of rows to process
var numOfColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows, numOfColumns);
var data = dataRange.getValues();
var sendTrigger = "";
var dt=new Date();
var dv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
for (var i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[4]; // fifth column
var message = row[5]; // sixth column
var d=new Date(row[2]);
if (!row[0] && new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()==dv) {
var subject = ("This is a test of the send email function");
MailApp.sendEmail(emailAddress, subject, message);
}
}
}

How do I modify this sending email code to expand the range to multiple columns?

I want to send emails from a spreadsheet to multiple people but only email them the information in all the columns and not just 1. For example, I want all the information from B2:E2 to be emailed to A2.
I am using the basic template provided in the Google Support which I have attached below.
All the changes to the code that I have made have not worked:
function sendEmails() {
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, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
There are quite few changes to make to the original script, html is our friend in this context, it will look nicer.
code :
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var colWidth = 5; // column width, including first one
// Fetch the range of cells A2:E3, one email per row
var dataRange = sheet.getRange(startRow, 1, numRows, colWidth)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var subject = "Sending emails from a Spreadsheet";
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = '<body><div style="font-family:arial,sans;font-size:10pt"><p>welcome message</p>';
message+= '<table style="border-collapse:collapse;" border = 1 cellpadding = 4><tr>';
for(var n=1 ; n<row.length ; n++){
message += '<td bgcolor="#EEF">'+row[n]+'</td>'
}
message += '</tr></table></div></body>';
MailApp.sendEmail(emailAddress, subject, 'html only',{htmlBody:message});
}
}
Sheet example :
email result , third row :
I have set up a spreadsheet which I think mimics the table structure you are looking for:
https://docs.google.com/spreadsheets/d/1S3fWaV4Sl4mIYjT-kMtOlP2V4xWSOpz721bxGWFuNqQ/edit
And the following function sends out emails:
One email per row
Email address in column 0, data in the other columns
Code:
// The range of data including email columns
// Example is set up with email in Column A, data in B and C
// Headers on Row 1, data rows on 2 and 3.
var DATA_RANGE = 'A2:C3';
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getRange(DATA_RANGE);
var values = data.getValues();
for (var i = 0, row; row = values[i]; i++) {
// Take email address from column A
var emailAddress = row[0];
// Join the remaining columns, comma-separated (as an example)
var messageData = row.slice(1).join(', ');
MailApp.sendEmail(emailAddress, 'Sending emails from a Spreadsheet', messageData);
}
}
Does this cover what you're trying to do?

Date format in body of email

I'm using a very basic automated email script. The message that I want to send out out is simply a date. In the spreadsheet this is formatted as "06/11/2013" but when the email is received it appears in the body of the email as "Wed Nov 06 2013 00:00:00 GMT-0000 (GMT)".
I want it to appear formatted in the body of the email as just the date as it is down in the spreadsheet. Is there any way to do this? Here is the (very basic) script I am using:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Date";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Not a very advanced user but any help would be appreciated!
The data is stored in the spreadsheet as a Date and therefore will appear in the end content as Date. Use the Utilities.formatDate to convert this to a String
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = Utilities.formatDate(row[1], "GMT" , "dd/MM/yyyy" ); // Second column
var subject = "Date";
MailApp.sendEmail(emailAddress, subject, message);
}
}