指定したクラスに参加している生徒と教師の名前とIDの取得を試したコードです
デフォルトでは 30 人まで取得できるようです
Query parameters
それより多い場合は nextPageToken がある限り繰り返し取得するコードを以下に書きました
courseId を自身の環境に合わせて書き換えて
listAllParticipantsPaged() を実行すると
courseId で指定したクラスに参加している生徒と教師の一覧を出力できます
Code.gs
Tips
生徒や教師の数がそれぞれ30人以下ならば以下のコードの方がシンプルに取得できる
Code.gs
Reference
Code.gs
function listAllParticipantsPaged() {
const courseId = '699141862609';// 対象のクラスのcourseId
const opts = { pageSize: 30 }; // デフォルトの 30 件ずつ取得する
// 生徒
let token = null;
do {
const res = Classroom.Courses.Students.list(courseId, { ...opts, pageToken: token });
(res.students || []).forEach(s =>
Logger.log(`生徒: ${s.profile.name.fullName} ${s.userId}`)
);
token = res.nextPageToken;
} while (token);
// 先生
token = null;
do {
const res = Classroom.Courses.Teachers.list(courseId, { ...opts, pageToken: token });
(res.teachers || []).forEach(t =>
Logger.log(`先生: ${t.profile.name.fullName} ${t.userId}`)
);
token = res.nextPageToken;
} while (token);
}
Tips
生徒や教師の数がそれぞれ30人以下ならば以下のコードの方がシンプルに取得できる
Code.gs
function listAllParticipants() {
const courseId = '699141862609';
// Students
Classroom.Courses.Students.list(courseId).students?.forEach(s =>
Logger.log(`生徒: ${s.profile.name.fullName}, ID: ${s.userId}`)
);
// Teachers
Classroom.Courses.Teachers.list(courseId).teachers?.forEach(t =>
Logger.log(`先生: ${t.profile.name.fullName}, ID: ${t.userId}`)
);
}
Reference
Method: courses.students.list
Query parameters