Date format in body of email - google-apps-script

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);
}
}

Related

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);
}
}
}

Trouble with simple google script send email with cc

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});
}
}

Send email to the address in one column with body from another, when a keyword is present

I need to send mail notification to specific email address in Column A2 with body email from B2 when writing keyword in C2. Column A will be filled up from a Google Form.
https://docs.google.com/spreadsheets/d/15HrfUNhIyqLD948aRq0h9iAUW4Y_9LGe8HQm3MP2rZ0/edit?usp=sharing
function kirim() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1; // First row of data to process
var numRows = 1; // 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 = "test meneh from spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
I dont't really know what the trigger column is for and there was any subject so I left it blank. But this should do what you need. You'll probably need to change the sheetname and if you want to call this with a timer based trigger you may want to replace the first line with var ss=SpreadsheetApp.openById('SpreadsheetID');
function kirim()
{
var ss=SpreadsheetApp.getActive();
var sht=ss.getSheetByName('SimpleEmail');
var rng=sht.getDataRange();
var rngA=rng.getValues();
var changed=false;
var re=/ok/i;
for(var i=1;i<rngA.length;i++)
{
if(rngA[i][2].match(re))//case insensitive and of the following will work: Ok ok oK OK
{
GmailApp.sendEmail(rngA[i][0], '', rngA[i][1]);
rngA[i][3]='EMAIL_SENT';
changed=true;
}
}
if(changed)rng.setValues(rngA);
}

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 Search for Automatic Reporting - Google Docs

Ok I've been through this site forward and backward before asking this but for some reason I can't get the code I've implemented to work automatically. I just need the script to run (I setup a trigger already) to check the email in column 3 of sheet 5 (called Reports). I need it to send me an email with column 1 data and column 2 data if today's date matches the date given.
Can someone look at the code and give me a little help?
function sendEmails() {
var spreadsheet = SpreadsheetApp.openById('Unique ID for spreadsheet');
var sheet = spreadsheet.getSheets()[4];
var startRow = 2; // First row of data to process
var numRows = 250; // Number of rows to process
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 4)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emailAddress = 'email#email.com';
var subject = "Report Today";
for (i in data) {
var row = data[i];
if( row[3] == true) {
var message = row[0]+row[1];
try {
MailApp.sendEmail(emailAddress, subject, message);
} catch(errorDetails) {
MailApp.sendEmail("Email#email.com", "sendEmail script error", errorDetails.message);
}
}
}
}
Make sure that the dates match to be able to compare them, by setting the time (hours, mins, secs) equal to 0.
function sendEmails() {
var spreadsheet = SpreadsheetApp.openById('[Spreadsheet_Id]');
var sheet = spreadsheet.getSheets()[4];
var startRow = 2; // First row of data to process
var numRows = 10; // Number of rows to process
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 4);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emailAddress = 'email#email.com';
var subject = "Report Today";
var today = new Date();
today.setHours(0,0,0,0);
for (i in data) {
var row = data[i];
row[3].setHours(0,0,0,0);
if( row[3] == today ) {
Logger.log("Send email with: " + row);
var message = row[0]+row[1];
try {
MailApp.sendEmail(emailAddress, subject, message);
} catch(errorDetails) {
MailApp.sendEmail(emailAddress, "sendEmail script error", errorDetails.message);
}
}
}
}