title: Using nextpagetoken to pull more than 500 results in Google Apps Script query
body: <p>In a helpful response to <a href="https://stackoverflow.com/questions/34398021/directory-query-returns-only-100-users">this post</a> 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.</p>
  
  <p>Right now my code looks like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>function writeToSpreadsheet(){
    var values = [];
    var users = AdminDirectory.Users.list({
      domain:'klht.org',
      maxResults: 500,
      query: "orgUnitPath=/CleverTeachers/King"
    }).users; 
    for (var i=0; i&lt;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);
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre class="lang-js prettyprint-override"><code>var usersPage2 = AdminDirectory.Users.list({
                                        domain: 'klht.org',
                                        maxResults: 500,
                                        query: "orgUnitPath=/OU",
                                        pageToken: nextPageToken
                                        }).users;
  </code></pre>
  
  <p>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?</p>