So I'm trying to set up a reminder email to automatically be sent based on the date in a cell. Kind of like this: Google Apps Script - Send Email based on date in cell
Here's my sample workbook: https://docs.google.com/spreadsheet/ccc?key=0AiHAV8ZZ5nexdDJqODhmamhldjN1ZTRKc09iZXNBZ3c#gid=0
This is the code that I have:
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();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
//Logger.log(date);
var sheetDate = new Date(row[2]);
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(date,'GMT+0200','yyyy:MM:dd')
var 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[1]; // Second column
var subject = "It's time to practice!" +message;
MailApp.sendEmail(emailAddress, subject, message);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}
But I'm not sure if it's working, and will it automatically send the email out? Obviously, I know very little script.
You only have 1 error in your existing code shared here, that is keeping it from working:
var sheetDate = new Date(row[2]);
You only have 2 indexes in your array, so this should be:
var sheetDate = new Date(row[1]);
Also, because you are using Utilities.formatDate to yyyy:MM:dd format, you do not need to set the hours minute and seconds, because Utilities.formatDate is returning a string with no time component. Furthermore, you do not need to create sheetDate or date, those can both be constructed as the first parameter in the Utilities.formatDate (see below). One other thing on this topic, because your date values are formatted as a date in your spreadsheet, they are being returned to your script as a date object, so really, it isn't necessary to call new Date(row[1]) .. but it doesn't hurt anything.
function sendEmail() {
try{
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();
for (i in data) {
var row = data[i];
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(new Date(),'GMT-0500','yyyy:MM:dd')
var SsheetDate = Utilities.formatDate(new Date(row[1]),'GMT+0200', 'yyyy:MM:dd')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "It's time to practice!" +message;
MailApp.sendEmail(emailAddress, subject, message);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}catch(err){
Logger.log(err.lineNumber + ' - ' + err);
}
}
Related
I want to emails with attachment but I have an error
SyntaxError: Invalid or unexpected token (row 17, file "try.gs")
and I don't understand why?. The pdf named "i.pdf" is in my google drive
here is my code :
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process
// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
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];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName(‘i.pdf’);
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: ‘Simple mail’});
}
}
Can someone help me pls
The error is caused by the use of typographic / curly / single quotation characters = ‘’.
Replace them by straight single or double quotation characters '' o "".
Related
Notify via email when a cell is updated in google spreadsheet
Google AppScript syntax error for sendemail. Can't ID my problem
You have two typos when you declare var file and in the MailApp.sendEmail() function . Try this instead:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process
// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
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];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName("i.pdf");
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: "Simple mail"});
}
}
So I have a script that would launch an e-mail, and I want it to be run when a specific cell (A5) contains a value over 10. Here's the script:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var rownum = 1; // #of rows
var rowstart = 2; // where to start row
// get range of cell
// column 1 = email, column 2 is first name
var dataRange = sheet.getRange(rowstart, 1, rownum, 2)
var data = dataRange.getValues()
for (i in data) {
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
var row = data[i];
var emailadr = row[0]; // data select
var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n Sent from my email"; // body
var sbjt = "You have mail!"; //subject
MailApp.sendEmail(emailadr, sbjt, msgcontent);}
}
}
Under Current project's triggers, I have the following:
Run: onEdit
Events: From spreadsheet On edit
Now, when I set cell "A5" to "21", the script doesn't run. What's the issue? I can't seem to figure it out. Thanks.
Using logger it shows that it does work. You can see in example below. You may want to try to use a time based trigger and set it to check every x amount of time and send email. Google may not allow the sendemail to run "onedit"
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var rownum = 1; // #of rows
var rowstart = 2; // where to start row
// get range of cell
// column 1 = email, column 2 is first name
var dataRange = sheet.getRange(rowstart, 1, rownum, 2)
var data = dataRange.getValues()
for (i in data) {
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
var row = data[i];
Logger.log("Yes it is larger than 10");
// var emailadr = row[0]; // data select
// var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n Sent from my email"; // body
// var sbjt = "You have mail!"; //subject
// MailApp.sendEmail(emailadr, sbjt, msgcontent);
}
}
}
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;
// grab column 10 (the 'date end' column)
var range = sheet.getRange(2,10,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var date_end_values = range.getValues();
// Now, grab the Event name data
range = sheet.getRange(2, 5, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();
var warning_count = 0;
var msg = "Send out a follow-up email asking how the event was!";
}
//Get today's date
var todaysDate = new Date();
var numRows = numRows
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var date_end = date_end_values[i][0];
//call setHours to take the time out of the comparison
if(date_end == todaysDate.setHours(0,0,0,0)) {
MailApp.sendEmail("max#xpogo.com",
"Reminder Spreadsheet Message", msg);
}
What I'm trying to do is have gmail send me a reminder when a certain column in my data set is equal to the present date. Im new to coding and am running into trouble. Please help?
I added the following after your variables were set, to determine whether they were commensurate for your comparison (hit ctrl+Enter after executing to view the log).
Logger.log(date_end);
Logger.log(todaysDate.setHours(0,0,0,0));
They weren't comparable at all. Here are the lines I changed to make this work:
var todaysDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy");
var date_end = Utilities.formatDate(date_end_values[i][0], "GMT+1", "MM/dd/yyyy");
if(date_end == todaysDate) {
HTH
Here is my code for the following, if I change the function getFilesByName() to getFileById() then it works perfectly fine, what is different and why is this not working as it should?
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 4; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// 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 name = row[0];
var emailAddress = row[1]; // First column
var filename = DriveApp.getFilesByName(row[2]); // Second column
var message = row[3]; // Third column
var emailSent = row[4]; // Fourth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "TEST" + name;
MailApp.sendEmail(emailAddress, subject, message, {
name: 'TEST',
attachments: [filename.getAs(MimeType.PDF)]
});
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
The error is actually pretty explanatory, you have a File Iterator class (documentation) and not a File, you must apply the method next() to get the most recently updated file with that name, if you have more files with that name, you can use a combination o hasNext() and next() in a loop.
I'm trying to create a script that checks if a cell is a certain color for instance #00ff00. If the cell color isn't 00ff00, I would like the script to take the email address in that cell and send an email. Everytime I run the following script "Missing ) after argument list. (line 22, file "Code")"
Can someone help me? Thanks.
var EMAIL_SENT = new Date() ;
function sendEmails2() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.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();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Trying to get the color of a cell, if the color matches 00ff00, then I want the for loop to skip that cell and move to the next one.
var cell = sheet.getRange("A1").getBackgroundColor();
If(cell != results.getRange().getBackgroundColor(00ff00)); {
for
(var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var currentp = spreadsheet.getUrl(); //current spreadhseet page
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message,currentp);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}}}}
There is an extra ; before the { on the below line:
If(cell != results.getRange().getBackgroundColor(00ff00)); {