Home > OS >  I get always 500 items when list Classroom
I get always 500 items when list Classroom

Time:12-19

When I run this code I get the value 500 as courses.length. I deleted 1 classroom, but I obtained 500 again. I suppose that number of classroom is higher, but I see always 500 classroom. What's my mistake?

function listadeicorsi()
{

       const ss=SpreadsheetApp.getActiveSpreadsheet();
       const sh1=ss.getSheetByName("Classroom");
       var page = Classroom.Courses.list();  
       var courses = page.courses;
       if (courses && courses.length > 0)
       {
            sh1.getRange(1,8,1).setValue(courses.length);
       }
       else
       {
           sh1.getRange(x,1,1).setValue("CLASSROOM NON TROVATE");
       }
 }

CodePudding user response:

You missed that Classroom API results are paged, in other words they are capped.

From https://developers.google.com/classroom/reference/rest/v1/courses/list:

pageSize integer
Maximum number of items to return. Zero or unspecified indicates that the server may assign a maximum.
The server may return fewer than the specified number of results.

If you want to make a list of all the courses you have to use the pageToken and use some sort of iteration as the results might be fewer even if you set a large pageToken.

Related

CodePudding user response:

I already tried pageToken, but I generated a loop. I will try again use of pageToken. The orginal code was this:

function listadeicorsi(){
  var pageToken;
  var optionalArgs = {
    pageSize: 1000,
  };
  const ss=SpreadsheetApp.getActiveSpreadsheet();
  const sh1=ss.getSheetByName("Classroom");
  sh1.getRange("A1:H3000").setValue("");
  sh1.getRange(1,1,1).setValue("CLASSROOM");
  sh1.getRange(1,2,1).setValue("ID");
  sh1.getRange(1,3,1).setValue("ID PROPRIETARIO");
  sh1.getRange(1,4,1).setValue("DATA CREAZIONE");
  var response = Classroom.Courses.list(optionalArgs);
  var courses = response.courses;
  var x=2;
  if (courses && courses.length > 0)
  {
    for (i = 0; i < courses.length; i  )
      {
        var course = courses[i];
        sh1.getRange(x,1,1).setValue(course.name);
        sh1.getRange(x,2,1).setValue(course.id);
        sh1.getRange(x,3,1).setValue(course.ownerId);
        sh1.getRange(x,4,1).setValue(course.creationTime);
        x  ;
      }
  } else
      {
        sh1.getRange(x,1,1).setValue("CLASSROOM NON TROVATE");
      }
}
  • Related