Skip to content

Instantly share code, notes, and snippets.

@yuemori
Last active May 23, 2020 10:00
Show Gist options
  • Save yuemori/559d68668c8dee051f351b74d900dcff to your computer and use it in GitHub Desktop.
Save yuemori/559d68668c8dee051f351b74d900dcff to your computer and use it in GitHub Desktop.
Google Classroomで自動投稿する
// クラスコードを指定する。複数指定したい場合は ["xxxxx","yyyyy"] のようにする
const CLASS_CODES = ["xxxxxx"];
// 投稿したい課題のタイトル
const COURSE_WORK_TITLE = "課題タイトル";
// 課題の説明
const COURSE_WORK_DESCRIPTION = "課題の説明";
// 課題に添付するテンプレートのリンク。このリンクのファイルが、対象のクラスのGDriveのフォルダにコピーされて投稿される
const COURSE_WORK_LINK = "https://docs.google.com/forms/d/xxxxxxxx";
// 下書きのままにしたい場合は "DRAFT" にする
const COURSE_WORK_STATE = "PUBLISHED";
function main() {
const courses = getCourses();
courses.forEach(function (course) {
const folderId = course.teacherFolder.id;
const file = copyTemplate(folderId)
createCourseWork(course, file);
});
}
function copyTemplate(folderId) {
const template = DriveApp.getFileById(getIdFromUrl(COURSE_WORK_LINK));
const fileName = template.getName() + Utilities.formatDate(new Date(), 'JST', '_yyyy/MM/dd')
const folder = DriveApp.getFolderById(folderId);
return template.makeCopy(fileName, folder);
}
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}
function getCourses() {
var res = Classroom.Courses.list();
courses = res.courses.filter(course => CLASS_CODES.includes(course.enrollmentCode));
return courses;
}
function createCourseWork(course, file) {
const resource = {
title: COURSE_WORK_TITLE,
description: COURSE_WORK_DESCRIPTION,
workType: "ASSIGNMENT",
state: COURSE_WORK_STATE,
materials: [
{
link: {
url: file.getUrl()
}
}
]
}
Classroom.Courses.CourseWork.create(resource, course.id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment