Google Classroom App Script移除师生报错:请求实体未找到
Google Classroom Apps Script移除用户报错解决方法
问题情况
- 从Stack Exchange找到的两段Google Classroom Apps Script代码,执行时均报错
the requested entity was not found - 将代码中的
remove替换为create时可正常运行,说明权限配置无问题 - 尝试移除已接受邀请和待接受邀请的学生,均触发相同错误
- 需求:移除待接受邀请的学生
原代码示例
function removeStudents() { //Classroom.Courses.Students.remove({ // userId: "classadmin@ruskin.ac.uk", //}, 727305413955); var ClassroomID=727305413955 var TeacherslistID="aconnell@ruskin.ac.uk"; Classroom.Courses.Teachers.remove({"userId":TeacherslistID},ClassroomID) }
报错原因
Classroom.Courses.Students.remove和Classroom.Courses.Teachers.remove仅适用于移除已经加入课程的成员,对于处于待接受邀请状态的用户,这两个方法找不到对应的实体,因此抛出the requested entity was not found错误。
解决方案
对待接受邀请的用户,需要使用Classroom.Courses.Invitations.remove方法,步骤如下:
- 获取课程下所有待处理的邀请列表
- 筛选出目标用户的邀请ID
- 通过邀请ID移除对应的邀请
修正后代码示例
function removePendingStudentInvites() { // 课程ID建议用字符串,避免JavaScript处理超大数字丢失精度 const courseId = "727305413955"; const targetUserId = "classadmin@ruskin.ac.uk"; // 获取课程下所有学生身份的待处理邀请 const inviteList = Classroom.Courses.Invitations.list(courseId, { userRole: "STUDENT", courseStates: ["ACTIVE"] }); if (inviteList.invitations) { // 遍历邀请列表,找到目标用户的邀请并移除 inviteList.invitations.forEach(invite => { if (invite.userId === targetUserId) { Classroom.Courses.Invitations.remove(courseId, invite.id); console.log(`已成功移除用户${targetUserId}的待接受邀请`); } }); } else { console.log("当前课程下没有待处理的学生邀请"); } }
额外注意事项
- 课程ID用字符串存储更稳妥,防止JS处理超大整数时出现精度丢失
- 执行前可先调用
Classroom.Courses.Invitations.list确认目标用户确实存在待接受邀请 - 若要移除待接受邀请的教师,只需将代码中的
userRole改为TEACHER即可
内容的提问来源于stack exchange,提问作者Class Admin
相关产品推荐
相关产品推荐

