Ask Your Question
4

How can I retrieve just a small number of contacts using the People API in Google Apps Script?

asked 2023-07-12 18:29:04 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-12 18:58:02 +0000

scrum gravatar image

You can use the list method of the People API to retrieve a small number of contacts. The list method allows you to specify a pageSize parameter to limit the number of contacts returned per page. You can also use the pageToken parameter to retrieve additional pages of contacts if needed.

Here's an example code snippet that retrieves the first 10 contacts using the People API in Google Apps Script:

function getTenContacts() {
  var params = {
    'pageSize': 10
  };

  var contacts = People.PeopleService.people().connections().list('people/me', params);

  while (contacts.connections && contacts.connections.length > 0) {
    for (var i = 0; i < contacts.connections.length; i++) {
      Logger.log('%s (%s)', contacts.connections[i].names[0].displayName, contacts.connections[i].names[0].metadata.source);
    }
    if (contacts.nextPageToken) {
      params.pageToken = contacts.nextPageToken;
      contacts = People.PeopleService.people().connections().list('people/me', params);
    } else {
      break;
    }
  }
}

In this code, we're calling the list method with the pageSize parameter set to 10. We then loop through the connections array in the response object and log the display name and source metadata of each contact. We also check if there's a nextPageToken property in the response object, and if so, we set the pageToken parameter in the next request to retrieve the next page of contacts.

You can adjust the pageSize parameter to retrieve a different number of contacts. Keep in mind that larger pages may lead to longer load times and higher resource usage.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-12 18:29:04 +0000

Seen: 11 times

Last updated: Jul 12 '23