Retrieve Encyclopedia of life pics from Google Spreadsheets

I recently had to find pictures corresponding to a relatively large number of species names. For doing this the Encyclopedia if Life is an interesting solution because it has a well developed API and it covers the entire breadth of taxonomic groups, from plants to animals, insects and everything in between. After copy-pasting several species names and urls, I realized it would probably be most efficient to automate this through a script in a Google Spreadsheet.

The script below adds a menu item to your Google Spreadsheet when executed. You then select species names in a single column, click on the menu item and choose the number of photos that should be retrieved. Then, the links to the photos are added to the columns adjacent to the species name. Simply make sure that you have enough free columns for the number of photos you want to retrieve.

To execute the script, simply click on Script Editor, copy-paste the script below and click on Run! Enjoy!

function onOpen() {
      .createMenu('Pic retriever')
      .addItem('Retrieve pics', 'showPrompt')

function showPrompt() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
      'Make sure species names are highlighted and sufficient cells to the right are free.',
      'Please enter the number of pictures desired:',

  // Process the user's response.
  var button = result.getSelectedButton();
  var numpics = result.getResponseText();
  if (button == ui.Button.OK) {
  } else if (button == ui.Button.CANCEL) {
  } else if (button == ui.Button.CLOSE) {

function myFunction(numpics) {
 var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveRange();
  var col = range.getColumn();
  var row = range.getRow();
  var data= range.getValues();
  for (var i = 0; i < data.length; i++) {
    var source1, source2, source3;
    if(data[i][0] != ''){
      var url = ''+data[i][0];
      var response = UrlFetchApp.fetch(url);
      var json = response.getContentText();
      var eol1 = JSON.parse(json);
      if(typeof eol1.results[0] !='undefined'){
        var eolid = eol1.results[0].id;
        if (eolid){
          var url2 = ''+eolid+'&images='+numpics+'&videos=0&sounds=0&maps=0&text=0&iucn=false&subjects=overview&licenses=all&details=true';
          var response = UrlFetchApp.fetch(url2);
          var json2 = response.getContentText();
          var eol2 = JSON.parse(json2);
          while(j < numpics) {
            var source
            if (typeof eol2.dataObjects[j] != 'undefined'){
              var source = eol2.dataObjects[j].eolMediaURL;