You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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方法,步骤如下:

  1. 获取课程下所有待处理的邀请列表
  2. 筛选出目标用户的邀请ID
  3. 通过邀请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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:52:37