今回 API で試すのは
画面上では 授業 > 作成 > 課題 で作成するこの「課題」です
今回は以下の情報を含めた課題の「下書き」を API で作成しました
- タイトル
- 詳細
- 期限の日時
- 点数
Apps Script でClassroom の課題を作成するコード
下記 Code.gs の中にある以下の値を自身の環境に合わせて変更します
- 期限の日時: year, month, day, hours, minutes
- maxPoints
- title
- description
createSelfIntroductionAssignment() を実行すると courseId で指定したクラスに課題が作成されます
Code.gs
function createSelfIntroductionAssignment() {
const courseId = "699141862609"; // あなたのクラスID
// 期限の日時
const year = 2025;
const month = 5;
const day = 11;
const hours = 23;
const minutes = 59;
const dueDateTime = new Date(year, month - 1, day, hours, minutes);
const maxPoints = 10;
const courseWork = {
title: '30-Second Self-Introduction Transcript11',
description: `In our first class, you will introduce yourself in English for 30 seconds.
Before the class, please prepare what you want to say.
Write a short script (transcript) in a Google Document and submit it.
Instructions:
- Write your introduction in English.
- Make it short enough to speak in about 30 seconds (around 70–90 words).
- Use a Google Document for your writing.
Deadline:
- Please submit it before the day of our first class.
Note:
- You do not need to memorize everything exactly.
- Try to speak naturally by practicing with your script.`,
workType: 'ASSIGNMENT',
state: 'DRAFT',
dueDate: {
year: dueDateTime.getUTCFullYear(),
month: dueDateTime.getUTCMonth() + 1,
day: dueDateTime.getUTCDate()
},
dueTime: {
hours: dueDateTime.getUTCHours(),
minutes: dueDateTime.getUTCMinutes()
},
maxPoints: maxPoints,
};
try {
const createdWork = Classroom.Courses.CourseWork.create(courseWork, courseId);
Logger.log('Assignment created successfully: ID = ' + createdWork.id);
} catch (e) {
Logger.log('Failed to create assignment: ' + e.message);
}
}
今回試してちょっと気になった点を以下 Tips にまとめました
Tips 1
期限の日時について
自分の環境では
const courseWork = { } の中で以下のように直接日時を書くと
作成後の課題では時間がズレました
dueDate: {
year: 2025,
month: 5,
day: 11
},
dueTime: {
hours: 23,
minutes: 59
},
↑この設定で実行した場合
2025年5月11日 23:59 に設定していますが
課題作成後の期限では
2025年5月12日 8:59 になります
これは日本時間と協定世界時(UTC) のタイムゾーンの差によって発生するようです
日本時間は UTC よりも 9時間進んでいるため
コードで直接設定した時間 + 9時間 になります
それを回避するため(コードで設定した日時通りの期限にするため)
// 期限の日時
const year = 2025;
const month = 5;
const day = 11;
const hours = 23;
const minutes = 59;
で日時を指定したあと
const dueDateTime = new Date(year, month - 1, day, hours, minutes);
でコンピューターが扱える日時にして
その日時を UTC に変換しました
dueDate: {
year: dueDateTime.getUTCFullYear(),
month: dueDateTime.getUTCMonth() + 1,
day: dueDateTime.getUTCDate()
},
dueTime: {
hours: dueDateTime.getUTCHours(),
minutes: dueDateTime.getUTCMinutes()
},
Tips 2
Code.gs のように const courseWork の中で
state: 'DRAFT'
state: 'PUBLISHED'
にするとコード実行時にクラスに公開されてストリームに投稿されます
※ 内容を確認してから「割り当て」してストリームに投稿したい場合は 'DRAFT' にしておく
Tips 3
課題の編集画面で上部に表示される「Apps Script createCourseWork経由」は
Apps Script のプロジェクト名です
実行した Apps Script のスクリプトエディタのプロジェクトの名前と同じ
これは課題を公開した時のストリーム投稿にも含まれます
Tips 4: 今回の実装にはあまり関係なかった情報
今回のように Apps Script で課題を作成するときには関係ない情報でしたが
ちょっと調べたことを書き残します
上記の Code.gs には書きませんでしたが
ChatGPT が最初に出力したコードには
maxPoints: maxPoints,
の下に
associatedWithDeveloper: true
というフィールドが書かれていました
この associatedWithDeveloper フィールドは
Apps Script で実行する場合
false にしても課題作成時に自動で true になる仕様らしい
なので書いても意味がなさそうだったので今回は省略しました
実際に false にして課題作成後に
課題の情報を取得してみるとたしかに true になっていました
associatedWithDeveloper について公式のリファレンスを調べてみると
以下のように書かれています
Reference
REST Resource: courses.courseWork今のところの自分の理解では
Classroom の API で課題を作成する場合
- 明示的に associatedWithDeveloper: true にしなくても自動で true になる
- というより課題作成時に設定する値ではない
- associatedWithDeveloper フィールドを取得する際
- true の場合は提出物への変更が可能
- false の場合は提出物の変更は不可
試しに API で課題の採点をしてみると
- 課題を作成した Apps Script のプロジェクトでは採点できました
- 新規に作成したプロジェクトで同じコードを実行すると以下のエラーが出ました
- API call to classroom.courses.courseWork.studentSubmissions.patch failed with error: @ProjectPermissionDenied The Developer Console project is not permitted to make this request.
試したときのログ:
Reference
https://developers.google.com/workspace/classroom/reference/rest/v1/courses.courseWork
生徒の回答を管理する
CourseWorkType
CourseWorkState
Date
TimeOfDay
IndividualStudentsOptions
SubmissionModificationMode
Assignment
MultipleChoiceQuestion
プロジェクトの作成と管理
関連記事