Tuesday 9 June 2020

Access RandomFox API for images

The following Google Apps Script uses the RandomFox website to access their API and generate a random image of a fox that is then pasted into a Google Sheet. The script is a follow on from this blog post for Numbers API as I continue to develop my skills with APIs.
Screenshot of fox image generated in spreadsheet
Screenshot of fox image generated in spreadsheet
insertImage.gs
Initially this Function calls connectAPI (below) to get data from the RandomFox API website. 2 items are returned (a link and an image) from which we just want the image part of the data:
var imageResult = data['image'];
We then need to get this into the spreadsheet for which we can create a formula tha uses the image Url we now have:
var formula = "=image(\"" + imageResult + "\")";
imageCell.setValue(formula);

connectAPI.gs
This Function access the RandomFox API and returns the data to the parent Function (above) in a usable format:
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
Access RandomFox API for images download

No comments:

Post a Comment