使用 nextpagetoken 在 Google Apps 脚本查询中提取超过 500 个结果
Using nextpagetoken to pull more than 500 results in Google Apps Script query
在对 的有用回复中,我了解到我一次最多可以从我的域目录中提取 500 个用户结果。现在我正在尝试让脚本继续 运行 并提取 500 个批次,直到它从域(或者在本例中为 OU)中提取所有用户并将它们全部添加到同一个电子表格中。
现在我的代码如下所示:
function writeToSpreadsheet(){
var values = [];
var users = AdminDirectory.Users.list({
domain:'klht.org',
maxResults: 500,
query: "orgUnitPath=/CleverTeachers/King"
}).users;
for (var i=0; i<users.length; i++){
values.push([users[i].name.fullName, users[i].primaryEmail]);
}
var spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}
我上次 post 的评论者指出 "part of the returned object will be a 'nextPageToken' you can add this into a follow-on query or queries until you have all your users." 他包括以下片段:
var usersPage2 = AdminDirectory.Users.list({
domain: 'klht.org',
maxResults: 500,
query: "orgUnitPath=/OU",
pageToken: nextPageToken
}).users;
我发现简单地将它附加在上面的代码下面会抛出一个 "ReferenceError: "nextPageToken" is not defined. (line 13, file "Code")"。现在我又卡住了。你能帮我完成这项工作吗?
我在上一个答案中提到的 nextPageToken
是第一个查询结果的 属性。
为了最好地处理您的示例中未确定的最大用户数,您最好通过将查询包装到 AdminSDK 中的 while 循环来完成此操作,该循环在找不到更多用户时立即终止:
function writeToSpreadsheet(){
var values = [],
users = [],
userListQuery = {},
nextPageToken = '',
listObject = {
domain:'klht.org',
maxResults: 500,
query: "orgUnitPath=/CleverTeachers/King"
},
i = 0,
spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
do {
if (nextPageToken && nextPageToken !== '') {
listObject.pageToken = nextPageToken;
}
userListQuery = AdminDirectory.Users.list(listObject);
// if there are more users than fit in the query a nextPageToken is returned
nextPageToken = userListQuery.nextPageToken;
// Add the query results to the users array
users = users.concat(userListQuery.users);
} while (nextPageToken);
for (i = 0; i < users.length; i += 1) {
values.push([users[i].name.fullName, users[i].primaryEmail]);
}
SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}
在对
现在我的代码如下所示:
function writeToSpreadsheet(){
var values = [];
var users = AdminDirectory.Users.list({
domain:'klht.org',
maxResults: 500,
query: "orgUnitPath=/CleverTeachers/King"
}).users;
for (var i=0; i<users.length; i++){
values.push([users[i].name.fullName, users[i].primaryEmail]);
}
var spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}
我上次 post 的评论者指出 "part of the returned object will be a 'nextPageToken' you can add this into a follow-on query or queries until you have all your users." 他包括以下片段:
var usersPage2 = AdminDirectory.Users.list({
domain: 'klht.org',
maxResults: 500,
query: "orgUnitPath=/OU",
pageToken: nextPageToken
}).users;
我发现简单地将它附加在上面的代码下面会抛出一个 "ReferenceError: "nextPageToken" is not defined. (line 13, file "Code")"。现在我又卡住了。你能帮我完成这项工作吗?
我在上一个答案中提到的 nextPageToken
是第一个查询结果的 属性。
为了最好地处理您的示例中未确定的最大用户数,您最好通过将查询包装到 AdminSDK 中的 while 循环来完成此操作,该循环在找不到更多用户时立即终止:
function writeToSpreadsheet(){
var values = [],
users = [],
userListQuery = {},
nextPageToken = '',
listObject = {
domain:'klht.org',
maxResults: 500,
query: "orgUnitPath=/CleverTeachers/King"
},
i = 0,
spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
do {
if (nextPageToken && nextPageToken !== '') {
listObject.pageToken = nextPageToken;
}
userListQuery = AdminDirectory.Users.list(listObject);
// if there are more users than fit in the query a nextPageToken is returned
nextPageToken = userListQuery.nextPageToken;
// Add the query results to the users array
users = users.concat(userListQuery.users);
} while (nextPageToken);
for (i = 0; i < users.length; i += 1) {
values.push([users[i].name.fullName, users[i].primaryEmail]);
}
SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}