Last active
April 11, 2020 17:52
-
-
Save igorblumberg/ebee479e634c3b285e6721719e7ac7b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inspired by https://www.benlcollins.com/apps-script/api-tutorial-for-beginners/ | |
// custom menu | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Load Corona') | |
.addItem('Get New Data','loadAllData') | |
.addToUi(); | |
} | |
function callAPI(country) { | |
// Call Covid API | |
var response = UrlFetchApp.fetch("https://api.covid19api.com/total/country/" + country + "/status/deaths"); | |
// Parse the JSON reply | |
var json = response.getContentText(); | |
return JSON.parse(json); | |
} | |
function displayCountryData(country) { | |
var sheet = SpreadsheetApp.getActive().getSheetByName(country) | |
var results = callAPI(country); | |
var output = [] | |
var index = 1; | |
results.forEach(function(elem,i) { | |
if(elem["Cases"] >= 20) | |
{ | |
output.push([index,elem["Cases"],elem["Country"]]); | |
index++; | |
} | |
}); | |
var len = output.length; | |
// clear any previous content | |
sheet.getRange(2,1,1000,3).clearContent(); | |
// paste in the values | |
sheet.getRange(2,1,len,3).setValues(output); | |
} | |
function loadAllData() | |
{ | |
var countries = ["Brazil","Spain","Us","Italy","Israel"]; | |
countries.forEach(function(elem,i) { | |
displayCountryData(elem); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment