-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcrmanager.gs
166 lines (150 loc) · 5.24 KB
/
gcrmanager.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Google Classroom Manager
* Developer: Kanakorn Horsiritham
* Computer Center, Prince of Songkla University
* Hat Yai, Songkhla, THAILAND
* Create Date: 2015-09-08
* Website: http://sysadmin.psu.ac.th/author/kanakorn-h/
* Base on original demo code of Classroom API
* https://developers.google.com/classroom/quickstart/apps-script
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('GCR Manager')
.addItem('List All Courses', 'listAllCourses')
.addItem('List by Selected Teachers', 'listCourseByTeacher')
.addItem('List by Selected Students', 'listCourseByStudent')
.addToUi();
}
function listAllCourses() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.insertSheet();
var optionalArgs = { };
listCourses( optionalArgs, sheet);
}
function listCourses(optionalArgs, sheet) {
var response = Classroom.Courses.list(optionalArgs);
var courses = response.courses;
if (courses && courses.length > 0) {
sheet.appendRow(["course.id",
"course.name",
"course.section",
"course.descriptionHeading",
"course.description",
"course.room",
"course.ownerId",
"course.creationTime",
"course.updateTime",
"course.enrollmentCode",
"course.courseState",
"course.alternateLink"
]);
for (i = 0; i < courses.length; i++) {
var course = courses[i];
var ownerEmail ="";
try {
ownerEmail = Classroom.UserProfiles.get(course.ownerId).emailAddress;
} catch (e) {}
sheet.appendRow([course.id,
course.name,
course.section,
course.descriptionHeading,
course.description,
course.room,
ownerEmail,
course.creationTime,
course.updateTime,
course.enrollmentCode,
course.courseState,
course.alternateLink
]);
}
} else {
Logger.log('No courses found.');
}
}
function listCoursesReport(optionalArgs, sheet) {
var response = Classroom.Courses.list(optionalArgs);
var courses = response.courses;
if (courses && courses.length > 0) {
sheet.appendRow(["course.id",
"course.name",
"course.section",
"countStudents"
]);
for (i = 0; i < courses.length; i++) {
var course = courses[i];
var ownerEmail ="";
try {
ownerEmail = Classroom.UserProfiles.get(course.ownerId).emailAddress;
} catch (e) {}
var countStudents=0;
try {
countStudents = Classroom.Courses.Students.list(course.id).students.length;
} catch (e) {}
sheet.appendRow([course.id,
course.name,
course.section,
countStudents
]);
}
} else {
Logger.log('No courses found.');
}
}
function getUserProfiles(userId, sheet){
var response = Classroom.UserProfiles.get(userId);
sheet.appendRow(["id" , response.id]);
sheet.appendRow(["fullName" , response.name.fullName]);
sheet.appendRow(["emailAddress", response.emailAddress]);
sheet.appendRow(["permissions" , response.permissions.toString()]);
sheet.appendRow(["photoUrl" , response.photoUrl]);
}
function listCourseByTeacher(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var selected=sheet.getActiveRange();
var teachers = [];
for (i=0;i<selected.getHeight();i++){
for (j=0;j<selected.getWidth();j++){
if (selected.getValues()[i][j] != "") {
teachers.push(selected.getValues()[i][j]);
}
}
}
for (m=0;m<teachers.length;m++) {
var optionalArgs = {
teacherId: teachers[m]
};
var response = Classroom.Courses.list(optionalArgs);
if (ss.getSheetByName(teachers[m]) == null) {
var teacherSheet = ss.setActiveSheet(ss.insertSheet(teachers[m]));
getUserProfiles(teachers[m], teacherSheet);
listCoursesReport(optionalArgs, teacherSheet);
}
}
}
function listCourseByStudent(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var selected=sheet.getActiveRange();
var students = [];
for (i=0;i<selected.getHeight();i++){
for (j=0;j<selected.getWidth();j++){
if (selected.getValues()[i][j] != "") {
students.push(selected.getValues()[i][j]);
}
}
}
for (s=0;s<students.length;s++) {
var optionalArgs = {
studentId: students[s]
};
var response = Classroom.Courses.list(optionalArgs);
if (ss.getSheetByName(students[s]) == null) {
var studentSheet = ss.setActiveSheet(ss.insertSheet(students[s]));
getUserProfiles(students[s], studentSheet);
listCoursesReport(optionalArgs, studentSheet);
}
}
}