Home > Software design >  Google Classroom - List all classes
Google Classroom - List all classes

Time:10-13

I have a function to list all of my google classes but its only returning 500 results, how do i implement paging to allow a complete list to be generated?

   function listCourses() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getSheetByName('Classes');
      var response = Classroom.Courses.list();
      var courses = response.courses;
      var arr=[];//You could put column headers in here
      for (i = 0; i < courses.length; i  ) {
        var course = courses[i];
        var ids = course.id;
        var title = course.name;
        var sec = course.section;
        var state = course.courseState;
        var guardian = course.guardiansEnabled;
        arr.push([ids,title,state,guardian]); 
      }
      sh.getRange(2, 1, arr.length, arr[0].length).setValues(arr);   
    }

Thanks

CodePudding user response:

In your situation, how about using pageToken? When your script is modified, it becomes as follows.

Modified script:

function listCourses() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Classes');
  var arr = [];
  var pageToken = "";
  do {
    var response = Classroom.Courses.list({pageToken});
    var courses = response.courses;
    arr = arr.concat(courses.map(({id, name, section, courseState, guardiansEnabled}) => [id, name, section, courseState, guardiansEnabled]));
    pageToken = response.nextPageToken;
  } while (pageToken);
  sh.getRange(2, 1, arr.length, arr[0].length).setValues(arr);
}

Reference:

  • Related