r/GoogleAppsScript • u/DoctorMeatBoy • 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:
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.
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?
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!
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!