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

使用Google Apps Script向Google Classroom发布文件遇预检查失败错误

错误原因排查与解决方法

核心原因及对应修复方案

1. 错误使用邮箱作为studentIds参数

Classroom API的individualStudentsOptions.studentIds要求传入学生的Google Classroom用户ID,而非邮箱地址。你的代码直接用了表格中的邮箱值,这是触发预检查失败的核心问题之一。

修复操作:
先获取课程内学生的ID与邮箱映射关系,再匹配对应ID:

// 函数开头先拉取课程学生列表并建立映射
const studentList = Classroom.Courses.Students.list(myCourseID).students;
const emailToIdMap = {};
studentList.forEach(student => {
  emailToIdMap[student.profile.emailAddress] = student.id;
});

// 循环内替换studentIds取值
const myGmail = myRow[0];
const studentId = emailToIdMap[myGmail];
if (!studentId) {
  console.log(`未找到学生 ${myGmail} 的ID,跳过`);
  return;
}

var myMaterial = {
    assigneeMode: "INDIVIDUAL_STUDENTS",
    individualStudentsOptions: { studentIds: [studentId] },
    title: "Report Document",
    materials: [{ driveFile: {driveFile:{id: myDocId}}}]       
};

2. Drive文件权限不足

若脚本运行账号无文档访问权限,或文档未共享给目标学生,API会触发预检查失败。

修复操作:

  • 确保运行脚本的账号拥有该文档的至少查看权限;
  • 批量给对应学生添加文档权限:
// 给目标学生添加文档只读权限
Drive.Permissions.create(
  {
    type: 'user',
    role: 'reader',
    emailAddress: myGmail
  },
  myDocId,
  {sendNotificationEmail: false}
);

3. materials参数格式错误

Classroom API要求materials是数组类型,原代码中写成了单个对象,格式不符合要求。

修复操作:
将materials: { driveFile: {driveFile:{id: myDocId}}}修改为materials: [{ driveFile: {driveFile:{id: myDocId}}}](外层添加数组包裹)。

4. 课程权限或ID失效

  • 检查运行脚本的账号是否仍是该课程的教师/拥有编辑权限;
  • 确认myCourseID是否正确,课程是否被删除或归档。

报错原文:
API call to classroom.courses.courseWorkMaterials.create failed with error: Precondition check failed.

内容的提问来源于stack exchange,提问作者BTT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 01:42:45