Created
July 29, 2020 03:18
-
-
Save rheajt/18bde5ea02dbaeb74f3b178d8548ceea to your computer and use it in GitHub Desktop.
Google Classroom API to Build Classrooms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() { | |
// 1. get the courses from the spreadsheet | |
const courses = getCoursesFromSpreadsheet(); | |
// 2. create the courses on the classroom | |
const responses = courses.map((course, ind) => { | |
const resource = createCourseResource(course); | |
const response = Classroom.Courses.create(resource); | |
course[3] = response.alternateLink; | |
course[4] = response.enrollmentCode; | |
return course; | |
}); | |
// 3. write the course url and share code the spreadsheet | |
return writeCoursesToSpreadsheet(responses); | |
} | |
function writeCoursesToSpreadsheet(courses) { | |
const ss = SpreadsheetApp.getActive(); | |
const sheet = ss.getSheetByName('CourseSingleSheet'); | |
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()); | |
//set new values | |
range.setValues(courses); | |
return true; | |
} | |
function getCoursesFromSpreadsheet() { | |
const ss = SpreadsheetApp.getActive(); | |
const sheet = ss.getSheetByName('CourseSingleSheet'); | |
const data = sheet.getDataRange().getValues(); | |
data.shift(); | |
return data.filter(row => row !== null); | |
} | |
function createCourseResource(course) { | |
const [courseName, instructorName, instructorEmail] = course; | |
const resource = Classroom.newCourse(); | |
resource.name = courseName; | |
resource.ownerId = 'me'; | |
resource.description = instructorName; | |
return resource; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment