1
votes

I'm working on a small project in Google Apps Scripts using Google spreadsheet / Google Forms.

I want to help students in large class sizes find people to study with by entering the class into a Google Form. From there they would be automatically emailed a list of people taking the same class as them, who are also looking for people in the same class or lecture to study with. The student inputs their name, school Id number, email, class CRN#, Class Name and instructor. Its put into the Google spreadsheet and I manipulate it like a CSV file.

I want to use the onEdit() trigger so that when information is added or edited, an email will be sent out. I'm not understanding how the onEdit() event trigger works.

This is my code for the class sorting:

function studyBudy2() {
    var ss = 
    SpreadsheetApp.openById("1ZxTdRdhy0iR6HH7jB75KL4g-SCr7nPZEilXrzECe7yg").getActiveSheet();
    var numOfStu = ss.getLastRow();

    /*var range = ss.getRange(2, 4, numOfStu-1);
    var values = range.getValues();

    // get emails out of spreadsheet
    for (var row in values) {
        for (var col in values[row]) {
            emails = values[row][col]
            Logger.log(emails);
            // sends emails
            MailApp.sendEmail(emails, "pls work","now");
        }
     }
     */
     var theMass = []
     var rAnge = ss.getRange(2,1,numOfStu-1,7);
     var vAlues = rAnge.getValues()
     for (var row in vAlues) {
         var student = [];
         var buzznumber = vAlues[row][2];
         var classCRN = vAlues[row][4];
         var class = vAlues[row][5];
         var proffessor = vAlues[row][6];
         student.push(buzznumber);
         student.push(classCRN);
         student.push(class);
         student.push(proffessor);
         theMass.push(student);
     }

     for (var i = 0; i < numOfStu-2; i++){
         var theStudent = theMass[i]
         var theCRN = theStudent[1];
         var theClass = theStudent[2];
         var theProffessor = theStudent[3];
         var theBuzznumber = theStudent[0];
         Logger.log(theClass);
         for (var j= 1; j < numOfStu-1; j++){
             if (i+j <= numOfStu-2 && theMass[i+j][1] == theCRN && (i+j != i)){
                 Logger.log("Youre in the same Section!")
             } 
             else if (i+j <= numOfStu-2 && theMass[j+i][2] == theClass && theProffessor != theMass[j+i][3] && (i+j != i)){
                 Logger.log("Youre taking the same Course!");
             }
             else if(j+i <= numOfStu-2 && theClass == theMass[j+i][2] && theProffessor == theMass[j+i][3] && (i+j != i)){
                 Logger.log("Youre in the same lecture!");                              
             }     
             else if (j+i > numOfStu-2){
                 continue;
             }
         }

     } 
}

I'm thinking the onEdit() function should be implemented as another function but does it go in another function or in this studyBudy2 function?

function onEdit(e) {
    var activeSheet = SpreadsheetApp.openById("1ZxTdRdhy0iR6HH7jB75KL4g-SCr7nPZEilXrzECe7yg").getActiveSheet();
    var row = e.range.getRow();
    var studentNum = row+1;
}
1

1 Answers

1
votes

onEdit is a reserved function name which purpose is to declare a function to be used as a simple trigger.

function onEdit(e){
  //do something
}

Besides creating an onEdit simple trigger, we could create a on-edit installable trigger. Functions to be used for installable triggers could be named as we wish but to avoid confusions it's better to avoid using reserved functions names and spreadsheet build-in functions names.

If studyBudy2() function does what you needs to triggered when a edit is made on the spreadsheet, you could rename it as onEdit, declara an onEdit function that call studyBudy2() or create an installable trigger that calls studyBudy2().

Bear in mind that only edits made by users directly on the spreadsheet will fire a simple/installable on edit trigger. If you want something fire when a form response is submitted then you should use a on form submit installable trigger.

For further details please read https://developers.google.com/apps-script/guides/triggers/