在Google Classroom API查询中列出30多名学生

时间:2018-03-17 14:39:10

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

此时,我有一个脚本可以正常列出Google课堂中的班级学生,但它没有列出所有学生,只列出了前30名。我需要它列出所有学生,没有不管有多少。我现在拥有以下内容:

function listStudents() {
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var sh = s.getSheetByName('CLASS');
  var r = sh.getDataRange();
  var n = r.getNumRows();
  var d = r.getValues();
  for (x = 0; x < n; x++) {
    var i = d[x][0];
    if(i == ''){ continue; } else if (i == 'D') {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getSheetByName('LISTSTUDENTS');
      var tea = Classroom.Courses.Students.list(d[x][8]);
      var t = tea.students;
      var arr = [];

      try {
        for (i = 0; i < t.length; i++) {
          var c = t[i]; 
          var ids = c.profile;
          var em = ids.emailAddress;
          arr.push([em]);   
        }
      }
      catch (e) { continue; } 

      sh.getRange(d[x][14], d[x][15], arr.length, arr[0].length).setValues(arr);  
    }
  }
}

2 个答案:

答案 0 :(得分:3)

您在查询中只收到30名学生,因为您只访问结果的第一页。几乎所有&#34;高级服务&#34;函数以类似的方式处理集合,因为它们在调用中返回可变数量的项目(通常达到可在查询中指定的大小,但有限制)。这是为了确保每个使用它的人都能及时获得服务。

例如,考虑Bob(来自Accounting)。这种请求分页方式意味着他无法请求包含20,000个项目的单个响应,在此期间,其他人的服务速度较慢。但是,他可以要求接下来的100件物品,200次。虽然Bob从他最近的查询中消费了这100个项目,但其他人可以在不中断的情况下使用该服务。

要进行此设置,您需要使用保证至少执行一次的代码循环,并使用对.list()调用的响应中包含的nextPageToken来控制环。在Javascript / Google Apps脚本中,这可以是do .. while循环:

// Runs once, then again until nextPageToken is missing in the response.
const roster = [],
    // The optional arguments pageToken and pageSize can be independently omitted or included.
    // In general, 'pageToken' is essentially required for large collections.
    options = {pageSize: /* reasonable number */};

do {
  // Get the next page of students for this course.
  var search = Classroom.Courses.Students.list(courseId, options);

  // Add this page's students to the local collection of students.
  // (Could do something else with them now, too.)
  if (search.students)
    Array.prototype.push.apply(roster, search.students);

  // Update the page for the request
  options.pageToken = search.nextPageToken;
} while (options.pageToken);
Logger.log("There are %s students in class # %s", roster.length, courseId);

答案 1 :(得分:0)

对于那些为此而苦苦挣扎的人,这是代码

    function listStudent() {
    var pageSizeValue = 300; /*** change with numbers that you want*/
    var nextPageToken = '';
    var courseID = 1234; /*** change with numbers that you want*/
    var ownerArray = [];

    do {
        var optionalArgs = {
            pageSize: pageSizeValue,
            pageToken: nextPageToken
        };
        var cls = Classroom.Courses.Students.list(courseID, optionalArgs);
        var nextPageToken = cls.nextPageToken;
        
       const ssData = cls.students.map(c => {
       return [c.profile.id,c.profile.name.fullName,c.profile.emailAddress]
              });

        Array.prototype.push.apply(ownerArray, ssData);

       } while (nextPageToken);
         const ss = SpreadsheetApp.openById("1234"); // <<< UPDATE THIS
         const sheet = ss.getSheetByName("Sheet1"); // <<< UPDATE THIS
     sheet.getRange(2,1,ownerArray.length,ownerArray[0].length).setValues(ownerArray); // <<< UPDATE THIS
    }