在 Google 教室 API 查询中列出 class 中超过 30 名学生

List more than 30 students in a class in Google Classroom API query

此刻,我有一个脚本可以正确列出 Google 教室中 class 的学生,但它不会列出所有学生,只列出前 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);  
    }
  }
}

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

例如,考虑鲍勃(来自会计)。这种请求分页样式意味着他无法请求包含 20,000 个项目的单个响应,在此期间其他人的服务速度都较慢。但是,他可以请求接下来的 100 个项目,共 200 次。当 Bob 从他最近的查询中使用这 100 个项目时,其他人可以在不中断的情况下使用该服务。

要进行此设置,您需要使用保证至少执行一次的代码循环,并使用包含在对 .list() to control the loop. In Javascript / Google Apps Script, this can be a do .. while 循环调用的响应中的 nextPageToken :

// 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);

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

    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
    }
          

我修改了Google(https://developers.google.com/apps-script/add-ons/editors/sheets/quickstart/attendance)提供的例子中的getRoster函数如下,对我有用

function getRoster(courseId) {
  var studentNames = []
  var studentEmails = []
  var nextPageToken = ''  

  do {
    var optionalArgs = {
      pageSize: 30,
      pageToken: nextPageToken
    };
    var response = Classroom.Courses.Students.list(courseId, optionalArgs)
    var students = response.students
    nextPageToken = response.nextPageToken    

    for (var i = 0; i <= students.length; i++) {
      try {
        studentNames.push(students[i].profile.name.fullName)
        studentEmails.push(students[i].profile.emailAddress)
      } catch (err) {        
      }
    }
  } while (nextPageToken);
  return { "studentNames":studentNames, "studentEmails":studentEmails }
}