I\'ve looked around and have bits and pieces but can\'t put the puzzle together. I\'m attempting to create a script that will run on a trigger configured to run daily. The
You are comparing two different objects here
if ( currentTime == dataRange) {
currentTime is a Date object whereas dataRange is a Range object - they'll never be equal and so nothing happens. What you could do is (DATE_COL is the column having your date)
for ( var i in data ){
var row = data[i] ;
var today = new Date();
var date = new Date(row[DATE_COL]) ;
if (today.getDate() == date.getDate() &&
today.getMonth() == date.getMonth() &&
today.getYear() == date.getYear() {
/* Your email code here */
}
}
here is a working version of your code, I used Utilities.formatDate to make strings of your dates, so you can choose what you compare (only days, hours ? min ?)
I commented the mail call just for my tests, re-enable it when you need
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
Logger.log(data)
for (i in data) {
var row = data[i];
var date = new Date();
var sheetDate = new Date(row[1]);
Sdate=Utilities.formatDate(date,'GMT+0200','yyyy:MM:dd')
SsheetDate=Utilities.formatDate(sheetDate,'GMT+0200', 'yyyy:MM:dd')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[0]; // First column
var message = row[2]; // Second column
var subject = "Sending emails from a Spreadsheet";
// MailApp.sendEmail(emailAddress, subject, message);
Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}
don't forget to look at the logs ;-)
I would do it this way ....
Setup the spreadsheet like this: https://docs.google.com/spreadsheet/ccc?key=0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc
Change the code to:
function sendEmails() {
var spreadsheet = SpreadsheetApp.openById('Type spreadsheet key here from spreadsheet URL');
/// e.g. var spreadsheet = SpreadsheetApp.openById('0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc');
var sheet = spreadsheet.getSheets()[0]; // gets the first sheet, i.e. sheet 0
var range = sheet.getRange("B1");
var dateString = new Date().toString();
range.setValue(dateString); // this makes all formulas recalculate
var startRow = 4; // First row of data to process
var numRows = 50; // 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();
for (i in data) {
var row = data[i];
if( row[3] == true) {
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Task Item Due";
try {
MailApp.sendEmail(emailAddress, subject, message);
} catch(errorDetails) {
// MailApp.sendEmail("eddyparkinson@someaddress.com", "sendEmail script error", errorDetails.message);
}
}
}
}
The trigger:
Because of the trigger, you will need to open the spreadsheet using openById. To do this, in the code replace 'Type spreadsheet key here from spreadsheet URL'. To find the key, open the google docs spreadsheet, the link has "key=A0a0df..." paste the code. See the example.
JavaScript: if you want to learn more about using Java Script, I recommend http://www.w3schools.com/js/default.asp