I searched and tried to use what I could find, but I'm stuck. I am attempting to create a script that scans a Google sheet (with about 30 sheets) for values that are newly inputted and are below 60%. I am then looking to have the script e-mail me the data that is in the 2nd column (Student's name), 4th column (teacher's name) of the current row of the data as well as the percentage (test score)that was below 60% (ex. Clarke,John Mrs. Brown 56.64%) . I am new to Java script but have been trying to learn.
Here is the script that I have so far. It finds the correct sheet without a problem. It will also email me as well (with the wrong message, but it works none the less). But there are certain things that I am stuck on. Like I know that I have to set up some sort of onEdit trigger so that it only sends the newly inputted score, but I don't know where to put it or how to do that. I also know that the dataRange code that I have seems wrong to me and the if statement also seems wrong. I am not sure how to specify "below 60%". I am a technology teacher and set up a massive data system for our school using Google Drive. I want to have the master data sheet automatically email my principal whenever a student needs help on a specific assessment. Any help you could give me would be much appreciated! Thank you guys so much for all of your time I love spending my time on here learning from everyone.
Brandon
Here is a link to an example of the data spreadsheet we use
Choppy Script Below
function sendEmail() { var spreadsheet = SpreadsheetApp.openById('spreadsheet ID'); /// The ID of the data spreadshfeet var sheet = spreadsheet.getSheets(); // gets all sheets var startRow = 3; // Third row of data to process var numRows = 40; // Number of rows to process var dataRange = sheet.getRange(startRow, 10, numRows, 50); // Start row 3 column 10, and stop row 40, column 50 // Fetch values for each row in the Range. var data = dataRange.getValues(); //Browser.msgBox(data) for (i in data) { var row = data[i]; if (dataRange.getValues() <= .60); { var emailAddress = "my email"; var message = row[2]; // Second column var subject = "ALERT - Assessment score below 60% inputted."; MailApp.sendEmail(emailAddress, subject, message); // Browser.msgBox(emailAddress) } } }
function emailAlert(e) {
var range = e.range;
if (range.getColumn() >= 10) { // Only check column I and up
var editedSheet = e.source.getActiveSheet();
var editedRow = range.getRow();
var value = range.getValue();
if (value !== 'undefined') {
if (value < 0.6) {
var studentData = editedSheet.getRange(editedRow, 1, 1, 9).getValues();
Logger.log(
'StudentId: ' + studentData[0][0] +
'\n Name: ' + studentData[0][1] +
'\n HR: ' + studentData[0][2] +
'\n Teacher: ' + studentData[0][3] +
'\n Grade: ' + studentData[0][4] +
'\n Race: ' + studentData[0][5] +
'\n G: ' + studentData[0][6] +
'\n Ed: ' + studentData[0][7] +
'\n AVG: ' + studentData[0][8]);
var emailAddress = "brandon.mause@sleschool.org";
var message = Test
var subject = "ALERT - Assessment score below 60% inputted.";
MailApp.sendEmail(emailAddress, subject, message);
// Send email..
}
}
}
}
I added the new code above with the installable trigger active. I still can't get the email function to work properly. Any Ideas? Thanks...
It sounds like your problem can be solved using the onEdit
trigger. However, since you want to send an email you'll need to add the trigger manually because the function requires authorization to send emails.
Try this:
function assessmentOnEdit(e) {
var range = e.range;
if (range.getColumn() >= 10) { // Only check column I and up
var editedSheet = e.source.getActiveSheet();
var editedRow = range.getRow();
var value = range.getValue();
if (typeof value === 'number') {
if (value < 0.6) {
var studentData = editedSheet.getRange(editedRow, 1, 1, 9).getValues();
var message = 'Assessment score: ' + Math.round((value * 100) * 10) / 10 + ' %' +
'\nStudentId: ' + studentData[0][0] +
'\nName: ' + studentData[0][1] +
'\nHR: ' + studentData[0][2] +
'\nTeacher: ' + studentData[0][3] +
'\nGrade: ' + studentData[0][4] +
'\nRace: ' + studentData[0][5] +
'\nG: ' + studentData[0][6] +
'\nEd: ' + studentData[0][7] +
'\nAVG: ' + Math.round((studentData[0][8] * 100) * 10) / 10 + ' %';
var emailAddress = 'john.doe@example.com';
var subject = 'ALERT - Assessment score below 60% inputted.';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
}
Check the google referance pages for more info on triggers: https://developers.google.com/apps-script/guides/triggers/events
EDIT
Since you want to use IMPORTRANGE
the onEdit
trigger will not fire. Try using the function below and use this function with onChange
instead of onEdit
.
function assessmentOnChange(e) {
var editedSheet = e.source.getActiveSheet();
var scriptProperties = PropertiesService.getScriptProperties();
var propertyKey = 'currentColumn';
var currentColumn = scriptProperties.getProperty(propertyKey);
if (currentColumn === null) {
currentColumn = 14; //Fisrt Empty column is N
}
var table = editedSheet.getRange(3, Number(currentColumn), editedSheet.getLastRow(), editedSheet.getLastColumn()).getValues(),
rowNumber = 3,
valuesToProcess = [];
table.forEach(function(row) {
var column = 0;
row.forEach(function(cell) {
if (typeof cell === 'number') {
valuesToProcess.push({
value: cell,
row: rowNumber,
column: Number(currentColumn) + column
})
}
column++;
});
rowNumber++;
});
var maxColumn = Number(currentColumn);
for (var i in valuesToProcess) {
assessmentOnEdit({
source: e.source,
range: {
getRow: function() {
return valuesToProcess[i].row
},
getValue: function() {
return valuesToProcess[i].value
},
getColumn: function() {
return valuesToProcess[i].column
}
}
});
if (valuesToProcess[i].column > maxColumn) {
maxColumn = valuesToProcess[i].column;
}
}
scriptProperties.setProperty(propertyKey, maxColumn + 1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.