Google Apps Script 列出 Google 课堂中所有课程的所有作业
2018-02-11
2454
我需要列出 Google Classroom 中所有课程的所有作业。有一行不起作用,但我不知道如何修复它。它稍后会产生错误消息
TypeError: Cannot read property "length" from undefined.
。
function listAssignments2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('TAREAS');
var response = Classroom.Courses.list();
var courses = response.courses;
for (i = 0; i < courses.length; i++) {
var course = courses[i];
// This line below is not written right, which gives me an error further on.
var class = Classroom.Courses.CourseWork.list(course.id);
}
var w = class.courseWork;
var arr=[];
// The error comes out here stating "TypeError: Cannot read property "length" from undefined.".
for (i = 0; i < w.length; i++) {
var c = w[i];
var ids = c.id;
var user = c.creatorUserId;
var type = c.workType;
var ti = c.title;
var des = c.description;
var st = c.state;
var sch = c.scheduledTime;
var due = c.dueDate;
arr.push([ids,user,type,ti,des,st,sch,due]);
}
sh.getRange(1, 1, arr.length, arr[0].length).setValues(arr);
}
2个回答
我完全搞错了我所输入内容的顺序,而且我还应该更改两个 for 循环变量。
function listAllAssignments() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('TAREAS');
var response = Classroom.Courses.list();
var courses = response.courses;
// First, the "arr" array had to be out of the for loop, this was the first error.
var arr =[];
for (i = 0; i < courses.length; i++) {
var course = courses[i];
var id = course.id;
var class = Classroom.Courses.CourseWork.list(id);
var w = class.courseWork;
/* The second and third error were here. First I had separated
the for loops, when I should have put the second for loop inside
the first, like I have here. Then, I should have changed the variable
of the second for loop (like I did here with the "k"). This seems to
have fixed the problems. */
for (k = 0; k < w.length; k++) {
var c = w[k];
var ids = c.id;
var user = c.creatorUserId;
var type = c.workType;
var ti = c.title;
var des = c.description;
var st = c.state;
var sch = c.scheduledTime;
var due = c.dueDate;
arr.push([ids,user,type,ti,des,st,sch,due]);
}
}
sh.getRange(1, 1, arr.length, arr[0].length).setValues(arr);
}
Jason Jurotich
2018-02-13
部分答案。
可能是 Classroom.Courses.list() 返回了一个空响应。为了防止您的脚本引发错误替换,请使用类似以下内容
var response = Classroom.Courses.list();
var courses = response.courses;
if (courses && courses.length > 0) {
for (i = 0; i < courses.length; i++) {
var course = courses[i];
Logger.log('%s (%s)', course.name, course.id); // Replace this with operations to be don on each iteration
}
} else {
Logger.log('No courses found.');
}
顺便说一句,问题上的脚本有一个
for
语句,每次迭代都会为
class
分配一个新值。您很可能应该在
for
的代码块中包含以下几行:
var w = class.courseWork;
var arr=[];
// The error comes out here stating "TypeError: Cannot read property "length" from undefined.".
for (i = 0; i < w.length; i++) {
var c = w[i];
var ids = c.id;
var user = c.creatorUserId;
var type = c.workType;
var ti = c.title;
var des = c.description;
var st = c.state;
var sch = c.scheduledTime;
var due = c.dueDate;
arr.push([ids,user,type,ti,des,st,sch,due]);
}
Wicket
2018-02-12