So I am working on a Google Apps script that pulls an email address, subject, and body from a Google sheet file. This info is used to send an email out. Right now in my Subject column for the Google Sheets file I have =TODAY() so that the date is pulled. My script updates this column everyday so the date is always current.
The issue is that when the email comes the subject line shows
"Sat Aug 11 2018 00:00:00 GMT-0700 (PDT) "
Instead of...
08/11/18 like its setup for in Google Sheets
Not sure why this could be, My code is below.
/**
* Creates a two time-driven triggers.
*/
function createTimeDrivenTriggers() {
// Trigger every 6 hours.
ScriptApp.newTrigger('adddate')
.timeBased()
.atHour(21)
.everyDays(1)
.inTimezone("America/Los_Angeles")
.create()
ScriptApp.newTrigger('sendEmails2')
.timeBased()
.atHour(22)
.everyDays(1)
.inTimezone("America/Los_Angeles")
.create()
}
/**
* This is my hacky way to make sure sheets has today's date.
*/
function adddate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(2,2);
cell.setValue('=TODAY()');
}
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'SUCCESSFULLY 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 = 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();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1]; // Second column
var message = row[2]; // Third column
var emailSent = row[3]; // Fourth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
The problem you are encountering is that your cell is formatted as a date and you are passing it as a parameter that expects a string. At that point you are no longer controlling the conversion, and you are getting way more than you wanted. Two ways to address it.
1) Make it a string in the first place with
cell.setValue('=text(TODAY(),"mm/dd/yy"');
which does run the risk of ruining any date processing you do on it at first (though I see none, so probably fine).
So probably better is (not 1, just this)
2) is to get the displayed string while leaving the underlying date like you had it with var subject = row[1].getDisplayValue();
2) has the added benefit of going with the date format for which sheets is set up.
Related
I have been using this mailapp coding for a while, and only sends to a list of people based on the EMAIL_SENT in column K, confirming it was sent in column J. The list is long, but only sends to maybe 20 people per run based on the rule for column K (a formula sending to people actually working today). Has worked great in the past.
Now though after recently reformatting the Google Sheet for my group, it takes 20-30 seconds to send one email off, and doesn't even finish the list of 20 emails in the 6 minute time limit. My recent reformatting does include a lot more scripting on the Google Sheet overall with more Conditional Formatting, but not on the sheet that feeds this Script.
Any ideas what is slowing it down so much?
Thanks
function MorningEmail() {
var sheet = SpreadsheetApp.getActive().getSheetByName('EarlyEmail');
var startRow = 4;
var numRows = 180;
var dataRange = sheet.getRange(startRow, 1, numRows, 50)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var fromName = row[16];
var replyToaddress = row[15];
var message = row[20]+row[21]+row[22]+row[23]+row[24]+row[25]+row[26]+row[27]+row[28]+row[29]+row[30]+row[31]+row[32]+row[33]+row[34]+row[35]+row[36]+row[37]+row[38]+row[39]+row[40]+row[41]+row[42]+row[43]+row[44]+row[45]+row[46]+row[47]+row[48]+row[49];
var emailSent = row[10];
if (emailSent != EMAIL_SENT) {
var subject = row[19];
MailApp.sendEmail(emailAddress, subject, message, {
name: fromName,
replyTo: replyToaddress,
htmlBody: message
});
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
I have a spreadsheet for recording customer samples. A column [I] is used to calculate when the original entry date is over a certain period which then displays an overdue message using an if statement. I want to trigger a script to check this column daily and if 'overdue' is found to send an email to an address contained in another column [C]. I am also using another column [J] to return a message when an email has been sent. I am using the script below but am returning errors
The script marks all rows in column J with the sent email message
I appear to be collecting a bigger array of data have needed [adding two rows the the bottom]
Anyone any ideas?
//Setup the function
function autoMail () {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //Get the active spreadsheet
var sheet = ss.getSheetByName( 'TS_Requests' ); //Get the active spread
var startRow = 3; // Start of row that going to be process considering row 2 is header
var lastRow = sheet.getLastRow(); // Get the last row of data to be process
var lastColumn = sheet.getLastColumn(); // Get the last column of data to be process
var range = sheet.getRange(startRow,1,lastRow,lastColumn); //Getting the specific cell to be process
var data = range.getValues(); // Get the values inside the cell
for(var i=0;i<data.length;++i){
var row = data[i];
var email = row[2]; // Grab the email column. C in this case
var customer = row[3]; // Grab data for Email body
var item = row[5]; // Grab data for Email body
var description = row[6]; // Grab data for Email body
var status = row[8]; // Grab the column containing the value needed to be checked.
var EmailSent = row[9]
if (EmailSent != 'Email_Sent') { //Prevents e-mail being sent in duplicate
if(valstatus ='Overdue'){ // Check the values, if it's what's needed, do something
MailApp.sendEmail(email, 'Your sample request is overdue', customer + ' - ' + item + ' - ' + description); // if condition is met, send email
sheet.getRange(startRow + i, 10).setValue('Email_Sent');
}
}
}
}
I posted this question on Google Sheets forum and was directed here for help.
*Please note that I am not a coder, I've just started working with things like this.
I've created a workbook with 3 pages. It's connected to a form, so as people submit their reviews the answers are collected on the first sheet, calculated on the second sheet, and any submission with an overall score higher than 3.9 has the email address moved to the third sheet where it will be sent a follow-up email.
Here is a link to a copy of the sheet:
https://docs.google.com/spreadsheets/d/15p7_M4guRWCVHG-acRAheAROmh9t4wk3GWD_x0_rl90/edit?usp=sharing
It all works except the last page: I cannot figure out how to get the emails to send automatically as the sheet is updated. I've set the trigger to have it run when the sheet is updated, but to no avail - I have to manually go in and click the "Run" button to have the emails sent. (When I do, it pops up 'Failed to sent email: no recipient (line 21, file "Code")' but the emails are successfully delivered anyway.)
The formula I am using is this: (I have also tried swapping "sendEmails2" with "autoResponder" but that didn't change anything either.) Can anyone tell me what is wrong with it? Or is there a better way to automatically send the e-mails?
// 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 = 20000; // 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();
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 emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is
interrupted
SpreadsheetApp.flush();
}
}
}
This should do it:
function sendEmails2(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Automatic Emails');
var rg=sh.getRange(2,1,sh.getLastRow(), 3);
var vA=rg.getValues();
for (var i=0;i<vA.length;i++){
var emailAddress = vA[i][0];
var message = vA[i][1];
var emailSent = vA[i][2];
var subject = "Sending emails from a Spreadsheet";
if(emailSent != 'EMAIL_SENT'){
MailApp.sendEmail(emailAddress, subject, message);
sh.getRange(2 + i,3).setValue('EMAIL_SENT');
}
}
}
Don't forget the onFormSubmit:
I am relatively new to programming, and have recently been working on a script to send emails from a google spreadsheet when a cell in a certain column is changed. The recipient is assigned based off of an email address in another column in the same row as the change. I am having difficulty getting my code to stop running after the first email. As it is, the script runs indefinitely (at least until I run out of emails for the day).
Here is the code:
function sendNotification() {
var sheet = SpreadsheetApp.getActiveSheet();
//Get Active cell
var mycell = sheet.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
var address = sheet.getRange("C" + cellrow).getValue();
var streetAddress = sheet.getRange("F" + cellrow).getValue();
var startRow = 2;
var numRows = 2000;
// Fetch the range of cells A2:O2000
var dataRange = sheet.getRange(startRow, 1, numRows, 15)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = address; // First column
var message = streetAddress +" Has been Submitted for permitting!"; // Second column
var subject = "The above Address has been Submitted For Permitting! We will Follow up with you when it has been approved.";
//Check to see if column is H to trigger
if (cellcol == 8 && sheet.getName() == "Sheet1" && mycell !== "")
{
//Send the Email
MailApp.sendEmail(emailAddress, message, subject);
}
//End sendNotification
}
}
What can I do to resolve this? Would a loop be the best option? How would I implement this?
How about this approach?
var EMAIL_SENT = "EMAIL_SENT";
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var mycell = e.range;
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
var emailAddress = sheet.getRange("C" + cellrow).getValue();
var streetAddress = sheet.getRange("F" + cellrow).getValue();
var subject = "The above Address has been Submitted For Permitting! We will follow up with you when it has been approved."
// Fetch values for each row in the Range
var message = streetAddress +" Has been Submitted for permitting!";
var emailSent = sheet.getRange("O" + cellrow).getValue();
if ( cellcol == 8 && sheet.getName() == "Sheet1" && emailSent != EMAIL_SENT) { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(cellrow, 15).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
If you use the onEdit() the function will be triggered every time you edit the cell. Is this what you want?
What I assume you're looking for is a script that:
Reads every row of the active sheet
If the 8th column is not empty, sends an e-mail to the address in the first column
The whole script is trigger upon the user requests (not when the user edits a cell)
In this case the first approach is better, the sendNotification(). Also a loop is necessary to read all the rows. And the IF statement should be something like if (row[8] != "") then send the e-mail.
In this case row which was defined as row = data[i] in your first script will have the values of all the cells in the row being read in the loop. So row[8] will have the value of the 8th (column H), which you want to check for emptiness, thus if(row[8] != "").
Also, if I understand correctly the e-mail adress should be emailAddress = row[1] inside the loop, because the email address is different every row.
Your second approach var EMAIL_SENT = "EMAIL_SENT"; is almost similar to the simple extension of the code that sets the cells in a column to 'EMAIL_SENT' for each row after sendEmail is called given in Section 2: Improvements. Within tutorial, each cell was marked in each row every time an email is sent. With that, you should mark edited cells as unsent then you will be able to re-run the script later on, avoid sending email duplicates and will only send the edited cells.
To make your code more efficient and help you improve the performance of your scripts, there are also list of best practices given.
I am using google sheets script to send an email if not already sent.
colA: colN:
abc#gmail.com Yes
def#gmail.com
ghi#gmail.com Yes
I want my script to check the value of colB. If null, send the email then change the value to Yes. If not null, skip and proceed to next line. Here is what I have so far.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 23;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 13)
var data = dataRange.getValues();
for (var i=0; i < data.length; i++) {
var row = data[i];
// If Column N is null
if (data[i][13] === ""){
var emailAddress = row[0]; // First column of selected data
var message = "....." ; // Assemble the body text
var subject = ".....";
MailApp.sendEmail(emailAddress, subject, message);
data[i][13] = "Yes";
}
}
dataRange.setValues(data);
}
Any help would be greatly appreciated.
1) Your range has 13 columns, meaning that in JavaScript, the second array index runs 0...12. You are referring to 13, which is out of bounds.
2) As written, dataRange.setValues(data); writes over the entire dataRange. If the range contains formulas, they will be replaced by static values, which is undesirable. Otherwise, the content should stay the same as it was, except for the entries of data array that you changed. Still, if only a few values in a sheet change, it's better to update them individually, as shown below.
Instead of
data[i][12] = "Yes";
you can call
dataRange.offset(i, 12, 1, 1).setValue("Yes");