Created
June 20, 2017 07:46
-
-
Save ahoereth/c9f01b76f70bd0e568066a0190a0cb64 to your computer and use it in GitHub Desktop.
Sending mails from Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This constant is written in column C for rows for which an email | |
// has been sent successfully. | |
var EMAIL_SENT = "EMAIL_SENT"; | |
var RESULTCOL = 15; // O | |
var ASSIGNMENT = "09 - Blindwalk"; | |
var SUBJECT = "[MATLAB] Assignment " + ASSIGNMENT; | |
var messages = { | |
'1': "You successfully passed assignment " + ASSIGNMENT + ". Congrats!\n", | |
'1?': "You passed assignment " + ASSIGNMENT + ". We want to note that it was a rather close call. If you want more specific feedback come up to us in class/after lecture or send an email.\n", | |
'0': "You failed assignment " + ASSIGNMENT + ". Sorry about that. If you are interested in more specific feedback and want to talk about the stuff you struggled with come up to us in class/after lecture or send an email.\n" | |
}; | |
function sendEmails() { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var startRow = 33; // First row of data to process | |
var numRows = 30; // Number of rows to process | |
var dataRange = sheet.getRange(startRow, 1, numRows, RESULTCOL+1); | |
var data = dataRange.getValues(); | |
// List of registered aliases in Gmail. 'from' needs to match one of those. | |
//var aliases = GmailApp.getAliases() | |
//Logger.log(aliases); | |
for (var i = 0; i < data.length; ++i) { | |
var row = data[i]; | |
Logger.log(row) | |
var emailAddress = row[3] + '@uos.de'; // C | |
var emailSent = (row[RESULTCOL] == EMAIL_SENT); // F | |
var state = (row[RESULTCOL-1] === 1 ? 'pass' : (row[RESULTCOL-1] === '1?' ? 'close' : (row[RESULTCOL-1] === 0 ? 'fail' : 'noncompete'))); | |
if ('noncompete' !== state && !emailSent) { | |
var message = "Hey " + row[1] + "!\n"; // A, Firstname | |
message += messages[row[RESULTCOL-1]]; | |
message += "\nThis is an automated message. If something doesn't feel right about it, contact us.\nGood luck with the current and all coming assignments!\n Andrea and Alex"; | |
GmailApp.sendEmail(emailAddress, SUBJECT, message, { | |
from: "", | |
replyTo: "", | |
}); | |
sheet.getRange(startRow + i, RESULTCOL+1).setValue(EMAIL_SENT); // Col right of the results | |
SpreadsheetApp.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment