2021/04/30

【GAS】指定したコースの情報を取得する方法

先日、すべてのClassroomからクラスコードを一括でスプレッドシートに自動出力する方法 を取り上げました。
今回はすべてのコースではなく「指定した1つのコース情報を取得する方法」について、考えてみます。

Google Apps Scriptに以下のスクリプトをコピー&ペーストしてください。

function getCourse() {
  const courseId = '312347346819'; //コースのIDを入力
  const course   = Classroom.Courses.get(courseId);
  console.log(course);
}

courseIdは、冒頭「すべてのClassroomからクラスコードを一括でスプレッドシートに自動出力する方法」を参考にしながら、オブジェクトのプロパティは「id」を指定してみてください。
実行ログの結果として、以下が表示されます。

これで、指定した1つのコース情報を出力することできました。

How can I export an one of course information?
After obtaining the course ID of Classroom, try running the script described above.
However, keep in mind about the courseId is differenet from classcode(enrollmentCode).

2021/04/26

【GAS】スクリプトを実行して新しいコースを作成する方法

Google Apps Scriptで新しいコースを作成したい場合は、以下のスクリプトをコピー&ペーストで実行してください。
function createCourse() {
  const course = {
    id: 'p:テストコース',
    name: 'テストコース',
    section: '前期',
    descriptionHeading: 'テストコースにようこそ!',
    description: 'こちらはGASで作成したテストコースです。',
    room: '503教室',
    ownerId: 'me',
    courseState: 'PROVISIONED'
  };

  const response = Classroom.Courses.create(course);
  console.log(`Course created:${course.id},${course.name}`);
}

以下、各プロパティの説明です。
 
  id  
Classroomによって割り当てられたこのコースの識別子です。対応するエイリアスを作成できます。たとえば、「p:微分積分学」でOKです。

 name 
コースの名前。たとえば「微分積分学」。必須項目です。

 section 
コースセクション、例えば「前期」や「後期」など。

 descriptionHeading 
説明のオプションの見出しです。たとえば、「微分積分学へようこそ」などです。

 description 
オプションの説明です。たとえば、「教科書、資料を使って授業を進めます」などです。

 room 
教室の場所を指します。

 ownerId 
コースの所有者の識別子です。①ユーザーの数値識別子、②ユーザーのメールアドレス、③"me"要求しているユーザーを示す文字列リテラルのいずれかになります。必須項目です。

 courseState 
コースの状態です。指定しない場合、デフォルトの状態はPROVISIONEDになります。

コースはオブジェクトから作成できますので、スプレッドシートに入力した情報をオブジェクトとして取得すれば一括で作成も可能です。
近日中に、Classroomコースを一括で作成する方法を紹介できればと思います。

-------------
For a description of each create field, please refer to the links below.
https://developers.google.com/classroom/reference/rest/v1/courses

The course can be created from an object, so if you get some data in the spreadsheet as an object, you can also create courses in bulk. Actually I plan on dealing with it within a few days in this blog.

2021/04/25

【GAS】参加しているコースのクラスコードをスプレッドシートに自動で一括出力する方法

管理者がすべてのクラスコードを一括で自動出力したい場合や、自身が参加している全コースのクラスコードを一括出力したい場合には、以下のスクリプトを実行してください。
また、ログの出力はスプレッドシートに行いますので、スクリプトはスプレッドシートのコンテナバインドスクリプトとしてください。
function listCourses() {

  const response = Classroom.Courses.list();
  const courses = response.courses;

  const array = [['コース名','クラスコード','コースセクション']]; //※1
  const enrollmentCodes = [];
  for (let i = 0; i < courses.length; i++) {

    enrollmentCodes.push([courses[i].name, courses[i].enrollmentCode, courses[i].section]); //※2
  }

  const sheet = SpreadsheetApp.getActiveSheet();
  
  const midashirange = sheet.getRange(1, 1, array.length, array[0].length);
  midashirange.setValues(array);
  const range = sheet.getRange(2, 1, enrollmentCodes.length, enrollmentCodes[0].length);
  range.setValues(enrollmentCodes);

}

※1…スプレッドシートの1行目に出力されます。出力するメソッドにあわせて修正してください。
※2…このスクリプトでは、「コース名」、「クラスコード」、「コースセクション」を出力します。
上記以外の情報を出力したい場合には、Classroom APIのリファレンスを参照の上、適宜メソッドを修正してください。

出力結果のイメージは次のとおりです。

How to output about all classcodes belonging your domain?
For example, if you are an administrator of school or member of something classroom and want to automatically output all classroom codes, to do execute the above script.
That log will be output to the spreadsheet, so please use the spreadsheet's container-bound script.

2021/04/22

今日からはじまります。:Start today.

はじめまして、私は@talkです。
このブログでは、実務で使えるGoogle Classroomの話を投稿していきたいと思います。 
どうぞご贔屓のほど、よろしくお願い申し上げます。

Hi, I'm @talk.
In this blog, I'd like to post about Google Classroom for practical and 'real' use. 
Please put it in your favorites, thanks.