【问题标题】:Ignore users already added to Google Classroom when inviting邀请时忽略已添加到 Google 课堂的用户
【发布时间】:2020-10-18 09:30:06
【问题描述】:

Google 课堂脚本可根据 google 表格中的电子邮件列表添加教师或学生。

由于列表中的用户已经是教室的成员,我收到错误“请求的实体已存在”。

问题 - 有没有办法检查 Col A 或 Col B 列表中的电子邮件地址是否已经是成员,如果是则跳过?

还是等到上面的错误再次出现,然后跳过错误并继续浏览列表以邀请列表中的所有其他用户?

谢谢 乔恩

function MultipleAccountInvite() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1 Class Invite");

var CN = ss.getRange('B1').getValue();
var CourseId = parseInt(CN).toFixed();
Logger.log(CourseId);
var Avals = ss.getRange(3, 1,20).getValues();
var Alast = Avals.filter(String).length;
Logger.log(Avals)
Logger.log(Alast)
var Bvals = ss.getRange('B3:B').getValues();
var Blast = Bvals.filter(String).length;
Logger.log(Bvals)
Logger.log(Blast)

for (var i = 0; i < Alast; i++) {
  var teachers = Avals[i][0];
  if (teachers) {
    var inviteteachers = Classroom.Invitations.create({
      "courseId": CourseId,
      "userId": teachers,
      "role": "TEACHER"
    })
    }
}

for (var j = 0; j < Blast; j++) {
  var students = Bvals[j][0];
  if (students) {
    var invitestudents = Classroom.Invitations.create({
      "courseId": CourseId,
      "userId": students,
      "role": "STUDENT"
    })
    }
}

}

【问题讨论】:

    标签: google-apps-script google-classroom


    【解决方案1】:

    最终删除了所有响应内容,因为我并不担心记录它。 使用 Try - Catch 循环。 Google Classroom 中似乎已经有足够的常识来防止用户同时被添加为教师和学生,所以一切都很好。

    非常感谢。

    最终代码:

    function MultipleAccountInvite() {
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1 Class Invite");
    
    var CN = ss.getRange('B1').getValue();
    var CourseId = parseInt(CN).toFixed();
    Logger.log(CourseId);
    var Avals = ss.getRange(3, 1,20).getValues();
    var Alast = Avals.filter(String).length;
    Logger.log(Avals)
    Logger.log(Alast)
    var Bvals = ss.getRange('B3:B').getValues();
    var Blast = Bvals.filter(String).length;
    Logger.log(Bvals)
    Logger.log(Blast)
    
    for (var i = 0; i < Alast; i++) {
      var teachers = Avals[i][0];
      if (teachers) {
        try {
        var inviteteachers = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": teachers,
          "role": "TEACHER"})
        } catch(e) {}
        }
      }
    
      for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        try {
        var invitestudents = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": students,
          "role": "STUDENT"})
        } catch(e) {}
        }
      }
    

    }

    【讨论】:

    • 很高兴您的问题得到了解决。在您的脚本中,当邀请 ID 被保存时,您可以检查教师和学生是否已被邀请。但我为我糟糕的技术深表歉意。我想学习更多。
    【解决方案2】:

    我相信你的情况和目标如下。

    • 在您的电子表格中,“A”和“B”列的值包含教师和学生的电子邮件地址。
    • 您想使用电子邮件地址邀请教师和学生。那时,当学生被邀请时,就会出现错误。您想消除错误。

    问题和解决方法:

    在您的情况下,错误发生在以下情况。

    1. "userId": students, 中的students 已经加入courseId 时。
    2. "userId": students,中的students已经被邀请,但用户还没有加入courseId时。

    关于1,可以使用“courses.students.list”的方法检查现有学生。但是关于2,当使用“invitations.list”的方法检索邀请列表时,邀请ID,即官方文档中的Identifier of the invited user.,并不是邮件地址。尽管从Classroom.Invitations.create() 返回的值包含 ID。但是在您的情况下,我担心已经邀请了几个学生并且没有保存ID。

    根据这些情况,我想提出一个解决方法来实现您的目标。在此解决方法中,使用了try catch。这样,当错误发生时,可以跳过错误。

    当这个变通办法反映到您的脚本时,它变成如下。

    修改后的脚本:

    发件人:

    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        var invitestudents = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": students,
          "role": "STUDENT"
        })
        }
    }
    

    收件人:

    var studentData = Classroom.Courses.Students.list(CourseId).students.reduce((o, e) => Object.assign(o, {[e.profile.emailAddress]: true}), {});
    var response = [];
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students && !studentData[students]) {
        try {
          var invitestudents = Classroom.Invitations.create({"courseId": CourseId,"userId": students,"role": "STUDENT"})
          response.push(invitestudents);
        } catch(e) {}
      }
    }
    console.log(response);  // You can retrieve the invitation ID here.
    
    • 在这个修改后的脚本中,可以避免上述两种情况。
    • 并且,在这个修改中,已经加入的学生可以检查studentData。已邀请但未加入的学生,使用try catch查看。

    注意:

    • Classroom.Invitations.create() 返回{"courseId":"###","role":"STUDENT","id":"###"}。所以当这个id被保存后,你可以查看id已经被邀请了。但是学生被邀请后,在不知道id的情况下,找不到id的创建方法。所以我提出了上面的修改。

    参考资料:

    补充:

    来自Problem with this line. Am getting an errorTypeError:cannot read property 'reduce' of undefined:--- var studentData = Classroom.Courses.Students.list(CourseId).students.reduce(callback, initialValue)((o, e) =&gt; Object.assign(o, {[e.profile.emailAddress]: true}), {}); var response = []; 在您的回复中,您的情况似乎没有现有学生。这种情况下,下面修改的脚本怎么样?

    修改脚本:

    从:
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        var invitestudents = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": students,
          "role": "STUDENT"
        })
        }
    }
    
    到:
    var response = [];
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        try {
          var invitestudents = Classroom.Invitations.create({"courseId": CourseId,"userId": students,"role": "STUDENT"});
          response.push(invitestudents);
        } catch(e) {}
      }
    }
    console.log(response);  // You can retrieve the invitation ID here.
    

    补充:

    来自Iamblichus's comment,我添加了try catch,用于邀请老师和学生。

    修改脚本:

    从:
    for (var i = 0; i < Alast; i++) {
      var teachers = Avals[i][0];
      if (teachers) {
        var inviteteachers = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": teachers,
          "role": "TEACHER"
        })
        }
    }
    
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        var invitestudents = Classroom.Invitations.create({
          "courseId": CourseId,
          "userId": students,
          "role": "STUDENT"
        })
        }
    }
    
    到:
    var response = [];
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        try {
          var inviteteachers = Classroom.Invitations.create({"courseId": CourseId, "userId": teachers, "role": "TEACHER"});
          response.push(inviteteachers);
        } catch(e) {}
      }
    }
    
    for (var j = 0; j < Blast; j++) {
      var students = Bvals[j][0];
      if (students) {
        try {
          var invitestudents = Classroom.Invitations.create({"courseId": CourseId,"userId": students,"role": "STUDENT"});
          response.push(invitestudents);
        } catch(e) {}
      }
    }
    console.log(response);  // You can retrieve the invitation ID here.
    

    【讨论】:

    • 这一行有问题。我收到 errorTypeError:cannot read property 'reduce' of undefined:--- var studentData = Classroom.Courses.Students.list(CourseId).students.reduce(callback, initialValue)((o, e) => Object.assign (o, {[e.profile.emailAddress]: true}), {}); var 响应 = [];
    • @Jon 感谢您的回复。我带来的不便表示歉意。从您的回复中,我在答案中添加了一个修改过的脚本。你能确认一下吗?
    • 如果我理解正确,学生和老师都会遇到同样的问题,因此教师邀请也应该包含在try...catch 块中。您对此有何看法,您会考虑将其添加到您的答案中吗?
    • @Iamblichus 哦!是的。这是正确的。从您的回复中,我可以更新我的答案。感谢您一直以来的支持。
    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多