The limitation with the fetch above is that it only gets us the first page of results.
**What if we want all the available results?**
Let's use a loop to continue to call for more results.
- We can modify the `per_page` and `page` values in the URL to get more than just page 1.
```js
const baseUrl = 'https://api.github.com/search/users';
async function getMatchingCandidates(location, primaryLanguage, beenCodingSince) {
const maxResults = 1000;
const resultsPerPage = 100;
const maxPages = Math.ceil(maxResults / resultsPerPage);
const allCandidates = [];
const options = {
headers: {
Accept: `token ${process.env.GITHUB_TOKEN}`,
},
for (let page = 1; page <= maxPages; page += 1) {
const params = `?per_page=${resultsPerPage}&page=${page}&q=type:user+language:${primaryLanguage}+created:<=${beenCodingSince}+location:${location}`;
const response = await fetch(baseUrl + params, options);
const data = await response.json();
const someCandidates = data.items || [];
// Once we get a page # with no data, stop the loop.
// e.g. If on page 4 we got no data, we won't make more API calls to get pages 5 - 10.
if (someCandidates.length === 0) break;
allCandidates.push(...someCandidates);
}
/* The while-loop alternative to for-loop:
let page = 1;
while (true) {
const params = `?per_page=100&page=${page}&q=type:user+language:${primaryLanguage}+created:<=${beenCodingSince}+location:${location}`;
const response = await fetch(baseUrl + params, options);
const data = await response.json();
const someCandidates = data.items || [];
// This line ensures that the loop ENDS when we hit the end of the available data from the API.
if (someCandidates.length === 0) break;
allCandidates.push(...someCandidates);
page += 1;
}
*/
return allCandidates;
}
Let's invoke that async function above and find devs in Toronto who works with JS for at least 3 years:
Morning Exercise Review (GitHub API)
This is the URL we're working with:
https://api.github.com/search/users?per_page=100&page=1&q=type:user+language:ruby+created:<=2015-10-30+location:miami
We can get different data by changing a few values in the URL:
ruby
withjavascript
Let's make the call to the API by interpolating our parameters into the URL:
async function getMatchingCandidates(location, primaryLanguage, beenCodingSince) { const params =
?per_page=100&page=1&q=type:user+language:${javascript}+created:<=${beenCodingSince}+location:${location}
; const options = { headers: { Accept:token ${process.env.GITHUB_TOKEN}
, }, };const response = await fetch(baseUrl + params, options);
const data = await response.json(); const someCandidates = data.items || [];
}
Let's invoke that async function above and find devs in Toronto who works with JS for at least 3 years:
Let's use the same API to get candidate's info on GitHub.
The endpoint for getting user info is:
GET /users/:username
Then you'll see this:
Working with Dates
Get the current date:
What if we want 3 years from now?
Turn it into something more readable:
Format the date string to YYYY-MM-DD:
Bringing
getMatchingCandidates()
andgetCandidateInfo()
togetherWithin
getMatchingCandidates()
, instead of pushing all info of all candidates intoallCandidates
array, we can push only the usernames.So the results from
getMatchingCandidates()
is now an array of GitHub usernames.Let's write another function:
Script Lab & JavaScript API
We can use JavaScript in Excel!
We can also make and style HTML pages.
An example code populating cells:
Making an API call in Script:
You can export and import "public gists" on GitHub.
Try importing this snippet Andy wrote in the demo.
Useful Excel table propperties
This is a good reference for working with tables using JS in Excel: https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-tables
Some useful properties and methods:
table.name
table.getHeaderRowRange()
- referencetable.rows.add
- referencetable.getDataBodyRange()
- refereenceExcel naming rules
Read more about it here: https://www.ablebits.com/office-addins-blog/2017/07/11/excel-name-named-range-define-use/#excel-name-rules