Run a script when cell is not empty - google-apps-script

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);
}
}
}

Related

Not able to copy sheet1 data to sheet2 in specific format

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);
});
}

Trying to create a script that on form submit sends an email to the submitter if they answered false

I'm looking on form submit to send an email to the submitter if they filled any of the questions as false, but the script is sending emails to any submitter who entered all true as well. I know the if script works with numbers as I used it in another project but when I changed the question to true false it seems to have broken the if statement. I also know the rest of the script works because it worked before I added the "if (certification == 'FALSE') {" line.
var EMAIL_SENT = 'EMAIL_SENT';
var FALSE = 'FALSE';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification == 'FALSE') {
if (emailSent !== EMAIL_SENT) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
}
Please try amending to the following:
var EMAIL_SENT = 'EMAIL_SENT';
var FALSE = 'FALSE';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
var error=0;
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification == 'FALSE') {
error++;
}
}
if (emailSent !== EMAIL_SENT && error >0) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
I ended up changing it to the following code and got it to work.
enter code here
var EMAIL_SENT = 'EMAIL_SENT';
var TRUE = 'true';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification !== true) {
if (emailSent !== EMAIL_SENT) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
}

How to pick the value numbers from another sheet using google app script?

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")}
}

Google Apps Script: sendEmail from sheet using cc and/or bcc, and avoid return of EMAIL_SENT to sheet

I have a script that sends an email out to addresses on a sheet and then returns an entry in a column in the sheet that reads EMAIL_SENT.
function sendEmail()
{
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow();
var EMAIL_SENT = "EMAIL_SENT";
var dataRange = sheet.getRange(startRow, 1, numRows, 2000)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i)
{
var row = data[i];
var name = row [0].split(",")[1];
var emailAddress = row [2];
var Setting = Utilities.formatDate(new Date(row [2]), "CST", "MM-dd-YYYY");
var Place = row [4];
var emailSent = row[5];
if (emailSent != "EMAIL_SENT")
{
var message = "message body";
var subject = "Email to Client";
MailApp.sendEmail
(emailAddress, subject, message,
{htmlBody:message,
cc:'internalemail#internalemail.com'});
sheet.getRange(startRow + i, 6).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}}}
As you can see from the script, I am also wanting to cc a copy of the email to an internal email address, but avoid the return of the EMAIL_SENT to the sheet for this internal email.
The script as written above sends two emails, but it does not seem to send the cc'd email to the internal email address, and it returns an instance of 'EMAIL_SENT' to the column in the source sheet.
I commented out some of your code and made some notes. Without seeing the sheet you were working from, it's a bit challenging to figure out where the error was, but this should work.
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow();
var EMAIL_SENT = "EMAIL_SENT";
var dataRange = sheet.getRange(startRow, 1, numRows, 5/*2000*/); //Confused why you were selecting 2000 columns.
var data = dataRange.getValues();
for (var i = 0; i < data.length; i++) { //switched to i++ instead of ++i
//var row = data[i]; This isn't needed
var name = data[i][0]; //row [0].split(",")[1];
var emailAddress = data[i][1]; //row [2];
var Setting = Utilities.formatDate(new Date(/*row [2]*/), "CST", "MM-dd-YYYY"); //Looks like you were creating a Date from the email address (row[2])...?
var Place = data[i][3];
var emailSent = data[i][4];
if (emailSent != "EMAIL_SENT" && emailAddress != "") {
var message = "message body";
var subject = "Email to Client";
MailApp.sendEmail(emailAddress, subject, message,{htmlBody:message, cc:'internalemail#internalemail.com'});
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
Here is how I would organize the data, but you can obviously adapt to your needs. When setting dataRange, I grabbed 5 columns as the data only extends to column E.

Gmail app send email with multiple attachments

I would like to learn tosend bulk google mails with attachments using a spreadsheet.
In a spreadsheet, i have put the email address, the content template, and the attachments urls.
Here is what I found as code that I tried to use for my purpose :
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 5;
var blobs = [];
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[2]; // 3rd column
var subject = "Attachments";
var message = row[4]; // 5th column
var attachments = row[3]; // Fourth column
var emailSent = row[0]; // 1st column
if (emailSent != EMAIL_SENT) {
GmailApp.sendEmail(emailAddress, subject, message, attachments);
sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
When I tried lunching it, the email was sent but without attachments.
Please help me.
Many thanks in advance.
Your not stating what attachments to send. I see your calling something within the spreadsheet but not the attachment.
Try using something like this
var file = DriveApp.getFilesByName('attacheddoc.pdf');
if (file.hasNext()) {
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(attacheddoc.PDF)],
name: 'Attached Documents'
}
So your code should look like
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 5;
var blobs = [];
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[2]; // 3rd column
var subject = "Attachments";
var message = row[4]; // 5th column
var attachments = row[3]; // Fourth column
var emailSent = row[0]; // 1st column
if (emailSent != EMAIL_SENT) {
var file = DriveApp.getFilesByName(attachments.pdf');
if (file.hasNext()) {
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(attachments+'.PDF')],
name: 'Attached Documents'
}
}
}
}
Hopefully that works for you. Would require some tinkering. However, I can't really do that for you without seeing the sheet. If it works +1?? :)