Gooogle课堂脚本,用于根据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"
})
}
}
}
答案 0 :(得分:1)
我相信您的情况和目标如下。
在您的情况下,在以下情况下会发生错误。
students
中的"userId": students,
已加入courseId
时。students
中的"userId": students,
,但用户尚未加入courseId
。大约1,可以使用“ courses.students.list”方法检查现有学生。但是大约2,当使用“ invitations.list”方法检索邀请列表时,正式文档中的Identifier of the invited user.
邀请ID不是电子邮件地址。尽管从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) => 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.
答案 1 :(得分:1)
最终删除了所有Response内容,因为Im并不担心记录它。 与Try-Catch循环一起使用。 看来Google课堂已经有了足够的常识,可以防止同时将用户同时添加为教师和学生,所以一切都很好。
非常感谢。
最终代码:
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) {}
}
}
}