Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions classroom/quickstart/quickstart.gs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,24 @@
* Lists 10 course names and ids.
*/
function listCourses() {
/** here pass pageSize Query parameter as argument to get maximum number of result
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
/**
* @see https://developers.google.com/classroom/reference/rest/v1/courses/list
*/
const optionalArgs = {
pageSize: 10
// Use other parameter here if needed
pageSize: 10,
};
try {
// call courses.list() method to list the courses in classroom
const response = Classroom.Courses.list(optionalArgs);
const courses = response.courses;
if (!courses || courses.length === 0) {
console.log('No courses found.');
return;
}
// Print the course names and IDs of the courses
for (const course of courses) {
console.log('%s (%s)', course.name, course.id);
}
} catch (err) {
// TODO (developer)- Handle Courses.list() exception from Classroom API
// get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND
console.log('Failed with error %s', err.message);
const response = Classroom.Courses.list(optionalArgs);
const courses = response.courses;
if (!courses || courses.length === 0) {
console.log('No courses found.');
return;
}
// Print the course names and IDs of the courses
for (const course of courses) {
console.log('%s (%s)', course.name, course.id);
}
}
// [END classroom_quickstart]
20 changes: 9 additions & 11 deletions classroom/snippets/addAlias.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
*/
// [START classroom_add_alias]
/**
* Updates the section and room of Google Classroom.
* @param {string} course_id
* Adds an alias to a course.
* @param {string} courseId The course ID.
* @see https://developers.google.com/classroom/reference/rest/v1/courses.aliases/create
*/
function addAlias(course_id) {
function addAlias(courseId) {
if (!Classroom || !Classroom.Courses || !Classroom.Courses.Aliases) {
throw new Error('Enable the Classroom API advanced service.');
}
const alias = {
'alias': 'p:bio_101'
alias: 'p:bio_101',
};
try {
const course_alias = Classroom.Courses.Aliases.create(resource=alias, courseId=course_id);
console.log('%s successfully added as an alias!', course_alias.alias);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Request to add alias %s failed with error %s.', alias.alias, err.message);
}
const courseAlias = Classroom.Courses.Aliases.create(alias, courseId);
console.log('%s successfully added as an alias!', courseAlias.alias);
}
// [END classroom_add_alias]
23 changes: 10 additions & 13 deletions classroom/snippets/courseUpdate.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@
*/
// [START classroom_update_course]
/**
* Updates the section and room of Google Classroom.
* @param {string} courseId
* Updates the section and room of a Google Classroom course.
* @param {string} courseId The course ID.
* @see https://developers.google.com/classroom/reference/rest/v1/courses/update
*/
function courseUpdate(courseId) {
try {
// Get the course using course ID
let course = Classroom.Courses.get(courseId);
course.section = 'Period 3';
course.room = '302';
// Update the course
course = Classroom.Courses.update(course, courseId);
console.log('Course "%s" updated.', course.name);
} catch (e) {
// TODO (developer) - Handle exception
console.log('Failed to update the course with error %s', e.message);
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
let course = Classroom.Courses.get(courseId);
course.section = 'Period 3';
course.room = '302';
// Update the course
course = Classroom.Courses.update(course, courseId);
console.log('Course "%s" updated.', course.name);
}
// [END classroom_update_course]
25 changes: 12 additions & 13 deletions classroom/snippets/createAlias.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@
*/
// [START classroom_create_alias]
/**
* Creates Course with an alias specified
* Creates a course with an alias.
*/
function createAlias() {
let course = {
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
const course = {
id: 'p:bio_101',
name: '10th Grade Biology',
section: 'Period 2',
descriptionHeading: 'Welcome to 10th Grade Biology',
description: 'We\'ll be learning about the structure of living creatures from a combination ' +
'of textbooks, guest lectures, and lab work. Expect to be excited!',
description:
'We\'ll be learning about the structure of living creatures from a ' +
'combination of textbooks, guest lectures, and lab work. Expect to be ' +
'excited!',
room: '301',
ownerId: 'me',
courseState: 'PROVISIONED'
courseState: 'PROVISIONED',
};
try {
// Create the course using course details.
course = Classroom.Courses.create(course);
console.log('Course created: %s (%s)', course.name, course.id);
} catch (err) {
// TODO (developer) - Handle Courses.create() exception
console.log('Failed to create course %s with an error %s', course.name, err.message);
}
const newCourse = Classroom.Courses.create(course);
console.log('Course created: %s (%s)', newCourse.name, newCourse.id);
}
// [END classroom_create_alias]
30 changes: 16 additions & 14 deletions classroom/snippets/createCourse.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@
*/
// [START classroom_create_course]
/**
* Creates 10th Grade Biology Course.
* Creates a 10th Grade Biology course.
* @return {string} The ID of the created course.
* @see https://developers.google.com/classroom/reference/rest/v1/courses/create
* return {string} Id of created course
*/
function createCourse() {
let course = {
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
const course = {
name: '10th Grade Biology',
section: 'Period 2',
descriptionHeading: 'Welcome to 10th Grade Biology',
description: 'We\'ll be learning about the structure of living creatures from a combination ' +
'of textbooks, guest lectures, and lab work. Expect to be excited!',
description:
'We\'ll be learning about the structure of living creatures from a ' +
'combination of textbooks, guest lectures, and lab work. Expect to be ' +
'excited!',
room: '301',
ownerId: 'me',
courseState: 'PROVISIONED'
courseState: 'PROVISIONED',
};
try {
// Create the course using course details.
course = Classroom.Courses.create(course);
console.log('Course created: %s (%s)', course.name, course.id);
return course.id;
} catch (err) {
// TODO (developer) - Handle Courses.create() exception
console.log('Failed to create course %s with an error %s', course.name, err.message);
const newCourse = Classroom.Courses.create(course);
console.log('Course created: %s (%s)', newCourse.name, newCourse.id);
if (!newCourse.id) {
throw new Error('Course created but no ID was returned.');
}
return newCourse.id;
}
// [END classroom_create_course]
15 changes: 6 additions & 9 deletions classroom/snippets/getCourse.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/
// [START classroom_get_course]
/**
* Retrieves course by id.
* @param {string} courseId
* Retrieves a course by its ID.
* @param {string} courseId The course ID.
* @see https://developers.google.com/classroom/reference/rest/v1/courses/get
*/
function getCourse(courseId) {
try {
// Get the course details using course id
const course = Classroom.Courses.get(courseId);
console.log('Course "%s" found. ', course.name);
} catch (err) {
// TODO (developer) - Handle Courses.get() exception of Handle Classroom API
console.log('Failed to found course %s with error %s ', courseId, err.message);
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
const course = Classroom.Courses.get(courseId);
console.log('Course "%s" found. ', course.name);
}
// [END classroom_get_course]
34 changes: 14 additions & 20 deletions classroom/snippets/listCourses.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,25 @@
*/
// [START classroom_list_courses]
/**
* Lists all course names and ids.
* Lists all course names and IDs.
* @see https://developers.google.com/classroom/reference/rest/v1/courses/list
*/
function listCourses() {
let courses = [];
const pageToken = null;
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
const optionalArgs = {
pageToken: pageToken,
pageSize: 100
pageSize: 100,
};
try {
const response = Classroom.Courses.list(optionalArgs);
courses = response.courses;
if (courses.length === 0) {
console.log('No courses found.');
return;
}
// Print the courses available in classroom
console.log('Courses:');
for ( const course in courses) {
console.log('%s (%s)', courses[course].name, courses[course].id);
}
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed with error %s', err.message);
const response = Classroom.Courses.list(optionalArgs);
const courses = response.courses;
if (!courses || courses.length === 0) {
console.log('No courses found.');
return;
}
console.log('Courses:');
for (const course of courses) {
console.log('%s (%s)', course.name, course.id);
}
}
// [END classroom_list_courses]
27 changes: 12 additions & 15 deletions classroom/snippets/patchCourse.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@
*/
// [START classroom_patch_course]
/**
* Updates the section and room of Google Classroom.
* @param {string} courseId
* Updates the section and room of a Google Classroom course.
* @param {string} courseId The course ID.
* @see https://developers.google.com/classroom/reference/rest/v1/courses/patch
*/
function coursePatch(courseId) {
let course = {
'section': 'Period 3',
'room': '302'
if (!Classroom || !Classroom.Courses) {
throw new Error('Enable the Classroom API advanced service.');
}
const course = {
section: 'Period 3',
room: '302',
};
const mask = {
updateMask: 'section,room'
const optionalArgs = {
updateMask: 'section,room',
};
try {
// Update section and room in course.
course = Classroom.Courses.patch(body=course, id=courseId, updateMask=mask);
console.log('Course "%s" updated.', course.name);
} catch (err) {
// TODO (developer) - Handle Courses.patch() exception
console.log('Failed to update the course. Error message: %s', err.message);
}
const newCourse = Classroom.Courses.patch(course, courseId, optionalArgs);
console.log('Course "%s" updated.', newCourse.name);
}
// [END classroom_patch_course]
Loading