r/GoogleAppsScript • • Sep 14 '20

Question Need help with an email script!

Hello all!

This is my first time putting together a script and I've been able to do as much as my knowledge and Google searching has allowed. If anyone can help, I would greatly appreciate it!

The goal of this script is use a Google sheet and automatically send emails to students reminding them of the assignments they are missing. I have been able to set up the email template and enable cc'ing the email to parents, but I can't figure out a few other things. Here is a sample sheet.

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = sheet.getSheetByName('Sheet1');
  var sheet2 = sheet.getSheetByName('Sheet2');
  var numColumns = sheet1.getLastColumn();

  var subject = sheet2.getRange(2,1).getValue();
  var n = sheet1.getLastRow();
  for (var i = 2; i < n+1 ; i++ ) {
    var emailAddress = sheet1.getRange(i,3).getValue();
    var name = sheet1.getRange(i,1).getValue();
    var assignment = sheet1.getRange(i,5).getValue();
    var message = sheet2.getRange(2,2).getValue();
    var parentAddress = sheet1.getRange(i,4).getValue();

    message=message.replace("<name>",name).replace("<assignment 1>",assignment);
    
    MailApp.sendEmail(emailAddress, subject, message,
                      {cc: parentAddress});
  }
}

There are the 3 things I need help with:

  1. The contents of the cells under the different assignments are irrelevant; I only need to indicate if something is completed or not. In my template email, Assignment 1 gets replaced with X, since it is the contents of the cell. In this case, the X denotes the assignment is completed. What I really want is to check if a cell is empty, and then the assignment/column name will be added into the body of the email with any other assignments that have no contents in the appropriate cell.

  2. How can I set a condition where if a student has every assignment completed (none of the cells are empty), then an email will not be sent?

  3. How can the number of columns/assignments be dynamic? I want to be able to add new assignments/columns without jeopardizing the script. I've included getLastColumn(); but I am not sure how to actually use it.

Thank you!

6 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/DoctorMeatBoy Sep 14 '20

Arrays are the reason I stopped pursuing any sort of coding years ago. I just couldn't figure it out! But that would awesome, thank you!

1

u/okburner11 Sep 15 '20

Also do note there is a email sending cap per day, I think it is 50 sent from a script. Probably won’t be a problem unless you have done student’s boycotting.

1

u/dale_jones Sep 15 '20

I'm assuming that @doctormeatboy is a teacher on a GAFE domain. If that's the case than it looks like the email recipients per day limit is 1500 using a max of 50 recipients per email. https://developers.google.com/apps-script/guides/services/quotas#current_quotas Hope this helps with knowing the limits of GAS email scripts. You've received plenty of good help with arrays so I won't throw my amateur hat into that ring.

2

u/DoctorMeatBoy Sep 15 '20

Thanks for the link! When it says 50 recipients per email, would that be just for the one Google sheet/script? So if I had additional sheets, that would allow for up to 50 recipients on each?

1

u/okburner11 Sep 15 '20

For every MailApp.sendEmail you can only have 50 recipients. Do you could just send that as many times as needed for one sheet.

1

u/DoctorMeatBoy Sep 15 '20

Copy. Thank you!