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() {
  SpreadsheetApp.getUi()
      .createMenu('Pic retriever')
      .addItem('Retrieve pics', 'showPrompt')
      .addToUi();
}

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:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var numpics = result.getResponseText();
  if (button == ui.Button.OK) {
    myFunction(numpics)
  } 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 = 'http://eol.org/api/search/1.0.json?q='+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 = 'http://eol.org/api/pages/1.0.json?batch=false&id='+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);
          j=0;
          while(j < numpics) {
            var source
            if (typeof eol2.dataObjects[j] != 'undefined'){
              var source = eol2.dataObjects[j].eolMediaURL;
            }
            if(source){
              sheet.getRange(row+i,col+1+j).setValue(source);
            }
            j++
          }
        }  
      }
    }
  }
}