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);
}
}
}
}
Related
I have a Google Script running the sendEmails() function with the installable onChange trigger. This works well but I'm seeing that it also executes overnight, when no changes are made to the spreadsheet.
Anyone know what may be happening/ how to prevent this?
Here's the code that's running in the script editor:
/**
* Sends emails with data from the current spreadsheet.
*/
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:C2
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[4]; // Fifth column
var message = row[0]; // First column
var subject = row[3]; // Fourth Column
MailApp.sendEmail(emailAddress, subject, message);
}
}
In the executions/trigger log I see that a trigger executed last night at 5:07 am and the night before at 1:16am, when no changes were made to the spreadsheet
screenshot of trigger log
Wasn't able to figure out the underlying issue, but created code which prevents the script from sending an email (to Asana to create a task in this case) for any row where an email was previously sent.
var TASK_SENT= "TASK_SENT";
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow();
// Fetch the range of cells UPDATE
var dataRange = sheet.getRange(startRow, 1, numRows, 48);
// 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 message = row[45] //Next Steps Column
var staff = row[1] // Your Name Column
var internal_part = row[7] //Who was the Check-in with? Column
var external_part = row[13] //What Site was your Check-in With? Column
var check_dt = row[2] //Date of Contact Column
var check_dt = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yy")
var site_disc = row[8] //What site was discussed?
var emailAddress = row[46]; // Email address column
var taskSent = row[47]; // Task_Sent column
if (taskSent != TASK_SENT) { // Prevents sending duplicates
var subject = staff + " " + 'check-in w/' + " " + internal_part + external_part + " " + 're:' + " " + site_disc + " " + check_dt + " " + 'Next Steps';
if (emailAddress == "") {
continue;}
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 48).setValue(TASK_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Try this to see if it will eliminate the overnight emails.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
for (var i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[4]; // Fifth column
var message = row[0]; // First column
var subject = row[3]; // Fourth Column
if(emailAddress && message && subject) {
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
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);
}
}
}
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);
}
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?
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);
}
}