Google函数中的OnEdit()Google脚本触发器 [英] OnEdit() google script trigger use in function

查看:195
本文介绍了Google函数中的OnEdit()Google脚本触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google电子表格/ Google表单使用Google Apps脚本开发一个小项目。

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

我想通过将班级输入Google表单来帮助大班级的学生找到学习的人。从那里,他们会自动通过电子邮件发送与他们上同一堂课的人的名单,这些人也在寻找同一堂课或讲课的人。学生输入他们的姓名,学校编号,电子邮件,班级CRN#,班级名称和讲师。它放在Google电子表格中,我像CSV文件一样操作它。

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.

我想使用 onEdit()触发器,以便在添加或编辑信息时,送出。我不了解 onEdit()事件触发器的工作原理。

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;
             }
         }

     } 
}

我想将 onEdit()函数实现为另一个函数,但是它是否可以在另一个函数中或在此 studyBudy2 中使用? code>函数?

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;
}


推荐答案

onEdit 是保留的函数名称,目的是声明要用作简单触发器的函数。

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
}

除了创建onEdit简单触发器之外,我们还可以创建可编辑的可安装触发器。可以根据需要命名用于可安装触发器的函数,但为避免混淆,最好避免使用保留的函数名和电子表格内置函数名。

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.

如果studyBudy2 ()函数执行在电子表格上进行编辑时需要触发的操作,您可以将其重命名为onEdit,声明一个调用studyBudy2()的onEdit函数或创建一个调用studyBudy2()的可安装触发器。

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.

有关更多详细信息,请阅读> https://developers.google.com/apps-script/guides/triggers/

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

这篇关于Google函数中的OnEdit()Google脚本触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆