I want to fetch all coursework currently due and insert it, formatted, into a Google Sheet. That means all coursework that has a due date in the future. I’ve never used the Google Classroom API. How would I do this in Apps Script? Thanks.
3
Fetch All Coursework that has Due Date
You may use this sample script as a basis for yours (which fetches all the courseworks with due dates on Google Classroom). Kindly follow the step by step procedure below.
Steps:
1.) On your spreadsheet, open Apps script via extensions.
2.) On the app script, click the +
icon on Services
and add Google Classroom API
.
3.) Navigate to Project Settings
and tick the box for Show "appsscript.json" manifest file in editor
4.) appsscript.json:
{
"timeZone": "Asia/Manila",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Classroom",
"version": "v1",
"serviceId": "classroom"
}
]
},
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/classroom.courses",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.profile.photos",
"https://www.googleapis.com/auth/classroom.rosters",
"https://www.googleapis.com/auth/classroom.coursework.me",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.coursework.students.readonly"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
Note: Set your preferred timezone
5.) Code.gs:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var cl = Classroom.Courses.list();
var id = cl.courses.map(c => c.id);
var cw = id.map(x => Classroom.Courses.CourseWork.list(x));
var dt = cw.map(x => x.courseWork.map(y => y.dueDate).map(z => [z.year, z.month, z.day].toString()));
var op = cw.map(x => x.courseWork.map(y => y.id)).flat().map((x, i) => [new Date(dt.flat()[i]), x]).filter(x => x[0] > new Date());
var rg = ss.getRange(1, 1, op.length, op[0].length);
rg.setValues(op);
}
6.) Expected output after Run
:
7.) Sample Course Works created on Google Classroom:
Reference:
Manage Coursework and Grades