I am using this script to send an auto email when the date = current date then run but it is not sending the email i do not know why.
Your help will be much appreciated.
https://docs.google.com/spreadsheets/d/1wxDa9D9a1wddqu9Fh4r3uZAX0m1NKNj2H7g_uS141-0/edit#gid=0
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mysheet");
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)
}
}
}
function sendEmail() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Mysheet");
const startRow = 2;
const subject = "Sending emails from a Spreadsheet";
const vs = sh.getRange(startRow, 1, sh.getLastRow() - startRow + 1, sh.getLastColumn()).getValues();
vs.forEach(row => {
let dt = new Date();
let dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
let sdt = new Date(row[1]);
let sdtv = new Date(sdt.getFullYear(),sdt.getMonth(),sdt.getDate()).valueOf();
if (sdtv == dtv){
MailApp.sendEmail(row[0], subject, row[2]);
}
});
}
Related
https://docs.google.com/spreadsheets/d/18odtLaamxJISS7Gq-N-xPxVu1Fk55AYQoFojkmIgxbM/edit#gid=0
function myFunction() {
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Messages from BOs");
var startRow = 4; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A4:C3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1];
var message = row[2]; // Second column
MailApp.sendEmail(emailAddress, subject, message);
}
for (var i in data, i++) {
var row1 = data[i];
Browser.msgBox(data[i+1])
/**
var emailAddress1 = row[0]; // First column
var subject2 = row[1];
var message3 = row[2]; // Second column
*/
var ad = row1;
var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
ts.getRange(startRow, i+1 ).setValue(row1);
//i = i + 1;
}
}
}
Here i wanted to update sheet2 every time when email send to "Recipient EMAL ID" to the new row of sheet2.
Please see my google sheet. I tried below code but it does not work well. If any who can help me in solve this problem.
Try to change:
ts.getRange(startRow, i+1 ).setValue(row1);
with:
ts.appenRow(row1);
https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)
This is a lot simpler:
function sendEmails() {
const ss = SpreadsheetApp.getActive()
var sh = ss.getSheetByName("Messages from BOs");
var ts = ss.getSheetByName('Test');
var sr = 4; // First row of data to process
var rg = sh.getRange(sr, 1, 1, 3);
var vs = rg.getValues();
vs.forEach(row => {
MailApp.sendEmail(row[0], row[1], row[2]);
ts.appendRow(row);
});
}
here is my code in google script:
function Send(){
Browser.msgBox("Send");
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var lr = sheet.getLastRow();
var dataRange = sheet.getRange(startRow, 1, lr-1, 6);
var data = dataRange.getValues();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var name = row[0];
var emailAddress = row[1];
var date = row[2];
var city = row[3];
var status = row[6];
if (emailAddress.match('#') === null){
continue;
};
var subject = row[4];
var message = "Hey " + name + ", welcome in the team " + row[5];
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(i+2,7).setValue("Sent");
}
}
Until here everything works fine. I would like then that when "Sent" appears in the 7th column, that the whole row where "Sent" is in, is moved to another tab.
function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
var actionCol = 7;
var nameCol = 7;
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
var colNumber = s.getLastColumn()-1;
if (e.value == "Sent" && colIndex == actionCol) {
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
if (ss.getSheetByName("Welcome")) {
var targetSheet = ss.getSheetByName("Done");
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
s.deleteRow(rowIndex);
}
}
}
**If I manually write "Sent" in the 7th column, the row is moved to the other sheet. But when I run the first function and that "Sent" appears in that column, the onEdit function doesn't work.
So basically both functions work but not together at the same time.
Does someone know a fix for this?**
Issues:
You are trying to trigger an onEdit function via a script but that's not how triggers work. The official documentation states the following:
The onEdit(e) trigger runs automatically when a user changes the value
of any cell in a spreadsheet.
Namely, onEdit triggers are activated only by user actions, not by scripts nor formulas.
You don't need a separate function to check if the value is Sent and then delete the row with another function. After the email is sent you can move the data and delete the row, all within the same function.
Last but not least, when deleting rows iteratively we change the structure of the sheet and therefore the data input does not match the updated structure. To alleviate this issue, we can store the indexes of the rows we want to delete in an array, and then using that array delete the rows backwards.
Solution:
Assuming your codes work separately, this should also work:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Welcome");
var targetSheet = ss.getSheetByName("Done");
var startRow = 2;
var lr = sheet.getLastRow();
var dataRange = sheet.getRange(startRow, 1, lr-1, 6);
var data = dataRange.getValues();
var colNumber = sheet.getLastColumn()-1;
var delRows = [];
for (var i = 0; i < data.length; i++) {
var row = data[i];
var name = row[0];
var emailAddress = row[1];
var date = row[2];
var city = row[3];
var status = row[6];
if (emailAddress.match('#') === null){
continue;
};
var subject = row[4];
var message = "Hey " + name + ", welcome in the team " + row[5];
MailApp.sendEmail(emailAddress, subject, message);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
var sourceRange = sheet.getRange(i+startRow, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
delRows.push(i+startRow);
}
// delete rows in reverse order
delRows.reverse().forEach(ri=>{sheet.deleteRow(ri)});
}
you don't need the onEdit function anymore so you can delete it.
I have 2 google sheet tabs, first sheet1 have col A - Email address, Col B - message, Col C - status.
and second sheet2 have only one column Col A - voucher code, I have insert all my necessary numbers in sheet2 Col A. So each time I run my script will send email out and automatically pick the numbers from sheet2 base on the last row of sheet1 data. Email sent without issue, but I fail to get voucher code data from sheet2 Col A in my email.
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var sheet2 = SpreadsheetApp.getActive().getSheetByName("Sheet2");
var strRow2 = 2;
var numRows2 = 2;
var dataRange2 = sheet2.getRange(strRow2, 1, numRows2, 1)
var data2 = dataRange2.getValue();
for (var j = 0; j < data2.length; ++j) {
var row2 = data2[j];
var code = row2[0];
}
var startRow = 2;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0];
var message = row[1] + "\n" + code // this code no working
var emailSent = row[2];
if (emailSent !== EMAIL_SENT)
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
In order to find the last row that has content you should use: getLastRow().
Here is the solution:
function sendEmail() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("Sheet1")
var sh2 = ss.getSheetByName("Sheet2")
var EMAIL_SENT = 'EMAIL_SENT';
var sh1_LR = sh1.getLastRow();
var sh1_EA = sh1.getRange(sh1_LR,1).getValue();
var sh1_M = sh1.getRange(sh1_LR,2).getValue();
var sh2_V = sh2.getRange(sh1_LR,1).getValue();
try{
MailApp.sendEmail(sh1_EA, sh1_M, sh2_V);
sh1.getRange(sh1_LR,3).setValue(EMAIL_SENT);
}
catch(e)
{Logger.log("sendEmail failed")}
}
The script sends automated mails and needs to be triggered, when a cell (e.g. A1) is no longer empty.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 11);
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[9];
var message = row[10];
var subject = 'Subject';
MailApp.sendEmail(emailAddress, subject, message);
}
}
All you need is a simple if statement:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 11);
var data = dataRange.getValues();
for (var i in data) {
if (data[i][0] != '') { //check cell A is empty in the row the script is running for.
var row = data[i];
var emailAddress = row[9];
var message = row[10];
var subject = 'Subject';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Please someone help me with this code.I need to add cc with different emails in 1 column.
Hope someone can help me.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 20; // Number of rows to process
// Fetch the range of cells A2:C3
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 = "EXCEPTION REPORT";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Using the options parameter of .sendEmail as in the reference.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 20; // Number of rows to process
// Fetch the range of cells A2:C3
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 ccAddress = row[--index of cc column--];
var message = row[1]; // Second column
var subject = "EXCEPTION REPORT";
MailApp.sendEmail(emailAddress, subject, message, {cc : ccAddress});
}
}