Using nextpagetoken to pull more than 500 results in Google Apps Script query

本小妞迷上赌 提交于 2019-12-10 18:25:32

问题


In a helpful response to this post I learned that I can pull a maximum of 500 user results from my domain directory at once. Now I am trying to get a script to continue to run and pull batches of 500 until it pulls all the users from the domain (or, in this case, the OU) and adds them all to the same spreadsheet.

Right now my code looks like this:

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

The commenter on my last post indicated "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." He included the following snippet:

var usersPage2 = AdminDirectory.Users.list({
                                      domain: 'klht.org',
                                      maxResults: 500,
                                      query: "orgUnitPath=/OU",
                                      pageToken: nextPageToken
                                      }).users;

I found that simply appending it below the code above throws a "ReferenceError: "nextPageToken" is not defined. (line 13, file "Code")". Now I'm stuck again. Can you help me make this work?


回答1:


the nextPageToken I referred to in the previous answer is a property of the first query's results.

to best cope with an undetermined maximum number of users in your example you'll best do this by wrapping the query to the AdminSDK in a while loop that terminates as soon as there are no more users to find:

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


来源:https://stackoverflow.com/questions/34418624/using-nextpagetoken-to-pull-more-than-500-results-in-google-apps-script-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!