r/GoogleAppsScript • • Mar 11 '20

Resolved Trying to send an email when a row is added (Google Sheets)

I'm trying to add a script to Sheets that will automatically send out an email with information from a new row that has been added. I've been looking into different options to do this, but I haven't had much luck. It doesn't help that I am very new to writing scripts. If anybody has a good resource to point me to for my specific application, I would love to hear it!

6 Upvotes

6 comments sorted by

4

u/schoraria911 Mar 12 '20

IMO, an Apps Script can be written quite easily for this depending on how the rows are being added. Would you mind sharing the sheet with dummy data where you’d want this to happen? Perhaps DM for more details too. Happy to help!

2

u/[deleted] Mar 11 '20

Take a look at the YAMM add-on. If that one doesnt work for your needs there should be a few sheets addons that would work

2

u/elvisofdallasDOTcom Mar 11 '20

The other suggestions are probably better for you if you aren't interested in learning Google Apps Script (quickly).

If you want to use GAS -- here's one thread with several approaches: https://support.google.com/docs/thread/26659691?hl=en

I recommend starting your GAS learning with something easier - use Zapier or another tool to get this objective resolved, then go back and learn the stucture of Google apps, so you can design an appropriate script for this type of thing sometime later.

Good luck! 😀

2

u/rtstorm Mar 12 '20

Hello, i have similar script that work when the new value is added.

function sendEmail() {

var ss = SpreadsheetApp.getActiveSpreadsheet();

var sheet = ss.getSheetByName("Input");

//here he find the last row

var last_row = sheet.getRange("F1").getValue();

//here you put receiver email

var emailAddress = "``[email protected]" ;

var message = "message";

// here it pick up the subject title from L1 cell

var subject = sheet.getRange("L1").getDisplayValues();

//here it pick up the data that will be send, so it take from A1 to C and last row

var data = sheet.getRange("A1:C"+last_row).getDisplayValues();

//another range of data if you want

var data_total = sheet.getRange("F5:G5").getDisplayValues();

Here it pick up the value from I1 and in thet cell you count what row is the last

var data_poslano = sheet.getRange("I1").getValue();

//here it count for another range

var data_poslano_count = sheet.getRange("F1").getValue();

//This is the value (lets say 'YES') that will put in the row when he send email. So the script // //know that this row was sent

var source_range = sheet.getRange("G1");

//here is the column where he put the 'YES' when the email was send

var target_range = sheet.getRange("D"+(last_row));

//this is the table format for body of mail

var TABLEFORMAT = 'cellspacing="0" cellpadding="0" dir="ltr" border="0" style="width:30%;table-layout:inherit;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:0px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'

var htmltable = '<table ' + TABLEFORMAT +' ">';

for (row = 0; row<data.length; row++) {

htmltable += '<tr>';

for (col = 0 ;col<data[row].length; col++){

if (data[row][col] === "" || 0) {htmltable += '<td>' + '</td>';}

else

if (row === 0) {

htmltable += '<th>' + data[row][col] + '</th>';

}

else {htmltable += '<td>' + data[row][col] + '</td>';}

}

htmltable += '</tr>';

}

htmltable += '</table>';

var htmltable2 = '<table ' + TABLEFORMAT +' ">';

for (row = 0; row<data_total.length; row++) {

htmltable2 += '<tr>';

for (col = 0 ;col<data_total[row].length; col++){

if (data_total[row][col] === "" || 0) {htmltable2 += '<td>' + '</td>';}

else

if (row === 0) {

htmltable2 += '<th>' + data_total[row][col] + '</th>';

}

else {htmltable2 += '<td>' + data_total[row][col] + '</td>';}

}

htmltable2 += '</tr>';

}

htmltable2 += '</table>';

//here is the check send vs need to be sent, if true it will send email

if(data_poslano_count != data_poslano){

Logger.log(data);

Logger.log(htmltable);

MailApp.sendEmail(emailAddress,subject,message,{htmlBody: htmltable+htmltable2})

var values = source_range.getValues();

target_range.setValues(values);

}

}

So in your spreadsheet you need to setup the:

F1 that will count the rowns

G1 that is basicly value 'YES'

I1 it will count the 'YES' in rows, that is how the scipt know if the email is send for this row.

0

u/[deleted] Mar 11 '20

[deleted]

1

u/lordph8 Mar 12 '20

Automate.io is pretty good and gives you more free automatons (250 per month),