There is no bug in this code, but the code does not do any action.
I am making a code for when reading the date of the current day and relate to the date in a particular column, will send an automatic email.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(ss.getSheetByName('Orçamento'))
var sheet = ss.getActiveSheet();
var startRow = 2; // First row of data to process
var lastRow = sheet.getLastRow(); // Number of rows to process
var numColumn = sheet.getLastColumn();
// grab column 5 (the 'days left' column)
var data = sheet.getRange(2,6,lastRow-startRow+1,1 );
var numRows = data.getNumRows();
var date_values = data.getValues();
// Now, grab the reminder name column
data = sheet.getRange(2, 2, lastRow-startRow+1, 1);
var reminder_info_values = data.getValues();
//Logger.log(data)
for (i in data) {
var column = data[i];
var date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
//Logger.log(date);
var sheetDate = new Date(column[6]);
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(date,'GMT-0300','yyyy:MM:dd')
var SsheetDate = Utilities.formatDate(sheetDate,'GMT-0300', 'yyyy:MM:dd')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var obra = column[1];
var descricao = column[2];
var inicio = column[6];
var fim = column[7];
var compra = column[8]; // Contact Method
var subject = "Lembrete de início de Serviço: " +obra;
var message = "O serviço: " + descricao + "tem previsão de data de início em " + inicio + "e previsão de término em : " + fim + "a previsão de compra do material é em: " + compra + ". Fique atento para não atrasar este serviço ";
MailApp.sendEmail({
to: "bruno#formula.eng.br",
subject: subject,
body: message,
});
}
}
}
Related
Script for current date + 5 sends an email with exact 5 days from now. I want this to only output the date leaving out the timestamp
current output is: Sun Jul 10 2022 09:29:16 GMT-0400 (Eastern
Daylight Time)
expected output is: 07/0/2022
Here is what I have tried.
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
var date5 = curDate.setDate(curDate.getDate() + 5);
var dateFormat = moment(date5).format('DD/MM/YY');
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ dateFormat + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}
I am stumped as to why this doesn't work.
Try it this way:
function findEmailAddress() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Working');
var vs1 = sh1.getRange('H1:H' + sh1.getLastRow()).getValues().flat();
var sh2 = ss.getSheetByName('Match');
var vs2 = sh2.getRange('A2:B' + sh2.getLastRow()).getValues();
var matchRows = vs2.filter(row => row[0].length && vs1.includes(row[0]));
matchRows.forEach(row => {
var mailMatch = row[1];
var curDate = new Date();
curDate.setDate(curDate.getDate() + 5);
let d = Utilities.formatDate(curDate,Session.getScriptTimeZone(),"MM/dd/yyyy")
var sub = "Action Required!!! 5 Days Til Expiration"
var bod = 'You have a new expiration date for completion'
+ d + ' all tasks associated with this must be complete. *This email is automatically generated do not reply*'
GmailApp.sendEmail(mailMatch, sub, bod);
});
}
I have a spreadsheet which looks like this:
Date | Day | S1 | S2 | S3 | S4 | S5 | S6 |
-----------------------------------------------------------------
14/04/20 | Sun | P-1 | H-1 | E-1 | R-1 | F-1 | G-1 |
15/04/20 | Mon | P-1 | H-1 | E-3 | R-1 | F-2 | G-2 |
Intention is to send an email the schedules (S1 till S6) on that particular date.
The script would run on a particular time of the day. It will compare today's date with the date on the first column. if the date matches, the data on that row should be sent to my email address.
I have written the code as below:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 4; //the date starts from the fourth row
var numRows = 170; //there are a total of 170 rows
var dataRange = sheet.getRange(startRow, 1, numRows, 8);
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var dateIter = row[0];
if (dateIter.getDate() == today.getDate() &&
dateIter.getMonth() == today.getMonth() &&
dateIter.getFullYear() == today.getFullYear()) {
var sub1 = row[2];
var sub2 = row[3];
var sub3 = row[4];
var sub4 = row[5];
var sub5 = row[6];
var sub6 = row[7];
var subject = 'Things you have to study today';
var message = "You have to study today" + sub1 + ", " + sub2 + ", " + sub3 + ", " + sub4 + ", " + sub5 + ", and " + sub6 + ". All the best." ;
break;
}
}
var emailAddress = 'myemail goes here';
MailApp.sendEmail(emailAddress, subject, message);
}
I am not sure about the date functions as I wrote in my code. I am getting an error as below:
TypeError: dateIter.getDate is not a function (line 11, file "Code")
I learnt about the date functions that I wrote here in this answer https://stackoverflow.com/a/14351783/9422511
What is the proper way of doing this?
Edit:
Added what Michael has suggested. There is another error which is as shown below:
ReferenceError: today is not defined (line 12, file "Code")
I have solved the problem. I did the following:
1. Cleared the date column, re-formatted it as date. And entered the date again according to the date format in the column.
2.Modified the date portion as suggested by Michael Pearson.
3. 'Today' has been made a object as below:
var today = new Date();
I was doing a mistake initially. The mailing portion of the code is put inside the loop itself.
Here is the code:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 4;
var numRows = 170;
var dataRange = sheet.getRange(startRow, 1, numRows, 8);
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var dateIter = row[0];
var date = new Date(dateIter);
var today = new Date();
if (date.getDate() == today.getDate() &&
date.getMonth() == today.getMonth() &&
date.getFullYear() == today.getFullYear()) {
var sub1 = row[2];
var sub2 = row[3];
var sub3 = row[4];
var sub4 = row[5];
var sub5 = row[6];
var sub6 = row[7];
var subject = 'Things you have to study today';
var message = "You have to study today" + sub1 + ", " + sub2 + ", " + sub3 + ", " + sub4 + ", " + sub5 + ", and " + sub6 + ". All the best." ;
var emailAddress = 'email goes here';
MailApp.sendEmail(emailAddress, subject, message);
break;
}
}
}
Thanks for the suggestions.
dateIter references a cell, and therefore does not have a getDate() function.
If the cell contains a string value, you could do something like :
var date = new Date(row[0]);
if (date == today.getDate()
etc.
I have a Google Spreadsheet and have modified the code below as indicated to send an email alert 7 days before the date condition is due. This works perfectly, however it only looks at the first sheet and the spreadsheet contains 23 sheets in total. I think I need to include an array and loop the code but cannot work this out so would appreciate some help! Thanks in advance.
function checkReminder() {
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 = 7;
// grab column 14 (the 'days left' column)
var range = sheet.getRange(7, 14, lastRow - startRow + 1, 1);
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the reminder name column
range = sheet.getRange(7, 15, lastRow - startRow + 1, 1);
var reminder_info_values = range.getValues();
// Now, grab the deceased name column
range = sheet.getRange(3, 1);
var name_of_sheet = range.getValue();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if (days_left == 7) {
// if it's exactly 7, do something with the data.
var reminder_name = reminder_info_values[i][0];
msg = msg + "Reminder: " + name_of_sheet + " inscription work is due in " + days_left + " days.\n";
warning_count++;
}
}
if(warning_count) {
MailApp.sendEmail("my#email.com",
"Reminder Inscription Schedule Message", msg);
}
};
This function sends email reminders based on a few conditions. One of the things I need to check for is that the Visit ID (which is in column 11 in the "email log" sheet) exists in a separate sheet ("DATA", stored in the enrollmentData variable). How do I search this array and return the ID to match in the IF statement below?
function sendReminders() {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var sheet = ss.getSheetByName("Email Log");
var rows = sheet.getLastRow()-1;
var startRow = 2; // First row of data to process
var numRows = rows; // Number of rows to process
var now = new Date();
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 22)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var today = Utilities.formatDate(new Date(), "GMT-5", "m/d/yyyy")
var reminderSent = "Reminder Sent";
//get email body and subject
var bodyTem = ss.getSheetByName("Email Templates").getRange('b8').getValues();
var subject = ss.getSheetByName("Email Templates").getRange('d13').getValues();
//get enrollments data to search for visit ID
var enrollmentData = ss.getSheetByName("DATA").getRange('H:H').getValues();
for (var i = 0; i < data.length; i++) {
var row = data[i];
//set conditions
var sendReminder = row[18];
var reminderDate = row[19];
var reminderStatus = row[20];
var visitID = row[11]
//need condition to look for visit ID to not include already cancelled. Search enrollmentData for visitID and return as foundID for conditional below
if (sendReminder == "Yes" && reminderStatus != reminderSent && reminderDate >= today && visitID == foundID) {
//assemble email
var studentEmail = row[13];
var firstName = row[12];
var instructor = row[0];
var body1 = bodyTem.replace(/*name*/gi,firstName);
var body2 = body1.replace(/*instructorFull*/gi,instructor);
MailApp.sendEmail(studentEmail, subject, body2);
//need to write in that the reminder email was sent.
sheet.getRange(startRow + i, 20).setValue(reminderSent);
sheet.getRange(startRow + i, 21).setValue(now);
};
};
};
You want to search the array
var enrollmentData = ss.getSheetByName("DATA").getRange('H:H').getValues();
The method getValues always returns a double array: in this case, it's of the form [[1], [2], [3],..] since each row has one element. I usually flatten this:
var enrollmentDataFlat = enrollmentData.map(function(row) {return row[0];});
Now enrollmentDataFlat is like [1, 2, 3, ..] so indexOf will work as usual:
if (enrollmentDataFlat.indexOf(visitID) != -1) {
// it's there
}
else {
// it's not there
}
I have the below script that is running but marks the "Email_Sent" column for every row (all 1000), I would like it to only send an email if Column A, B or C has an entry. (This is copied from the first sheet if someone marks a Yes in a specific column) and only if the Sent_Email column is blank as well.
function sendEmails()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Email"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i < data.length; ++i)
{
var rowData = data[i];
var emailAddress = rowData[1];
var recipient = rowData[0];
var message1 = rowData[3];
var message2 = rowData[4];
var message3 = rowData[5];
var message4 = rowData[6];
var message5 = rowData[7];
var emailSent = rowData[9];
var message = 'Hi ' + recipient + ',\n\n' + message1 + ' ' + message2 +
',\n\n' + message3 + ',\n' + message4 + ',\n' + message5;
var subject = 'Medical Questionairre Check';
if (emailSent != 'EMAIL_SENT' && MailApp.getRemainingDailyQuota()>0 &&
emailAddress && subject && message)
{
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(i+1, 9).setValue('EMAIL_SENT');// Make sure the cell is
updated right away in case the script is interrupted
}
}
}
Here is the link to the spreadsheet.
https://docs.google.com/spreadsheets/d/1SOPiXhU3KWHpHyZEiDUSk8m5qzO5CyALWio6doQvJ4Q/edit?usp=sharing
A Simple Fix
Just had to change emailSent = rowData[25] rather than rowData[9]
function sendEmails()
{
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('Sheet2');
var dataRange=sheet.getDataRange();
var data=dataRange.getValues();
for (var i=1;i<data.length;++i)
{
var rowData = data[i];
var emailAddress = rowData[1];
var recipient = rowData[0];
var message1 = rowData[3];
var message2 = rowData[4];
var message3 = rowData[5];
var message4 = rowData[6];
var message5 = rowData[7];
var emailSent = rowData[25];//Change from 9 to 25
var message = 'Hi ' + recipient + ',\n\n' + message1 + ' ' + message2 + ',\n\n' + message3 + ',\n' + message4 + ',\n' + message5;
var subject = 'Medical Questionairre Check';
var rdq=MailApp.getRemainingDailyQuota();
if (!emailSent && rdq>0 && emailAddress && subject && message)
{
MailApp.sendEmail(emailAddress, subject, message)
//Logger.log('emailAddress: %s subject: %s message: %s emailSent: %s',emailAddress,subject,message,emailSent);
sheet.getRange(i+1, 26).setValue('EMAIL_SENT');
}
}
}
The spreadsheet after execution: