Created
February 13, 2018 19:23
-
-
Save citostyle/a94fbaa3cea1fe022afc62f21d6858cf to your computer and use it in GitHub Desktop.
Sending individual emails to students with App Script
This file contains 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
function sendGradeEmails() { | |
var spreadSheet = SpreadsheetApp.getActiveSheet(); | |
var dataRange = spreadSheet.getDataRange(); | |
// Fetch values for each row in the Range. | |
var data = dataRange.getValues(); | |
var template = "Dear {name},\n\n" + | |
"Here are your total percentage points (out of 100) for Software Construction:\n" + | |
"Exam: {exam}%\n" + | |
"Exercise: {exercise}%\n" + | |
"Total: {total}%\n" + | |
"Grade: {grade}"; | |
var subject = "Individual Points"; | |
var replyTo = "someReplyEmail@ifi.uzh.ch"; | |
var length = data.length; | |
var start = 4; // Start of the actual grade sheet (I know, kind of magic number) | |
for (var i = start; i < length; i++) { | |
(function(val) { | |
var row = data[i]; | |
var body = template; | |
var emailAddress = row[3]; | |
var templateReplacements = { | |
'name' : row[2] + " " + row[1], | |
'exam' : Math.round(row[4]*100, 2), | |
'exercise' : row[5], | |
'total' : Math.round(row[6], 2), | |
'grade' : row[7], | |
'email' : emailAddress | |
} | |
for (var key in templateReplacements) { | |
body = body.replace('{' + key + '}', templateReplacements[key]); | |
} | |
Logger.log(emailAddress); | |
Logger.log(body); | |
MailApp.sendEmail(emailAddress, replyTo, subject, body); | |
})(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment