Google 课堂 - 列出所有 类
Google Classroom - List all classes
我有一个功能可以列出我所有的 google 类 但它只返回 500 个结果,我如何实现分页以允许生成完整列表?
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);
}
谢谢
在你的情况下,使用 pageToken
怎么样?当你的脚本修改后,变成如下。
修改后的脚本:
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);
}
参考:
我有一个功能可以列出我所有的 google 类 但它只返回 500 个结果,我如何实现分页以允许生成完整列表?
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);
}
谢谢
在你的情况下,使用 pageToken
怎么样?当你的脚本修改后,变成如下。
修改后的脚本:
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);
}