Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9782

if and for statements on Google Sheets with multiple tabs

$
0
0

I am trying to figure out how to send an email to a bunch of people on my mailing list, but with it being individualized for each person. I have their names and email addresses on one Google Sheet (the email tab). On the same file, I have their name and the item that they are missing on another sheet tab (the master sheet). Basically, I would like to have an email sent to each person with their name and missing item (s) listed so that they know what they need to turn into the warehouse.

Below you will see my Apps Script code. I was able to test it, and it worked only for the first 2 names in my list, when the name in the cell on one tab equaled the name on the other one that was on the master list. There are a couple of problems that I've come across with this:

  1. It only emails for the name when it equals the name on the master sheet. If names are not in the exact same order, then it doesn't work. This is an issue because the master sheet is continuously updating, so the email sheet doesn't have the names in the exact same order as the master sheet.
  2. There are also multiple of the same names on the master sheet. This is because some people have more than one missing item. So I am trying to figure out how to have the code list all the missing items for that person in the same email. (I also have a message I've written on another tab to be sent in the email).

So based on these problems, I am wondering if I am using the if else statement right. Or any of this. I am still pretty new to Apps Script, so any advice would be appreciated.


function sendEmail() {    var ss = SpreadsheetApp.getActiveSpreadsheet();    var sheetname = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Missing Handoff (Google Form)');    var sheet1 = ss.getSheetByName('Email Messaging');    var sheet2 = ss.getSheetByName('Emails');    var sheet3 = ss.getSheetByName('Missing Handoff (Google Form)')    var subject = sheet1.getRange(2,1).getValue();    var n = sheet2.getLastRow();    for (var i = 2; i < n + 1; i++) {      var emailAddress = sheet2.getRange(i,2).getValues();      var name1 = sheet2.getRange(i,1).getValues();      var name2 = sheet3.getRange(i,5).getValues();      var itemMissing = sheet3.getRange(i,12).getValues();      var message = sheet1.getRange(2,2).getValues();      if (name1 == name2) {        message = message.replace("<name>",name2).replace("<item>", itemMissing);        MailApp.sendEmail(emailAddress,subject,message);      } else {        message = ""      }    }}

Viewing all articles
Browse latest Browse all 9782

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>