Google Apps脚本列出了Google课堂中所有课程的所有作业

时间:2018-02-11 03:07:45

标签: google-apps-script google-sheets google-classroom

我需要列出Google课堂中所有课程的所有作业。一行不起作用,但我不知道如何修复它,这会产生错误“TypeError:无法读取属性”长度“来自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 个答案:

答案 0 :(得分:1)

我完全偏离了我所做的事情,最重要的是我应该更改两个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); 
}

答案 1 :(得分:0)

部分答案。

可能是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]); 
  }