Since it's almost time for Eurovision again, I thought I'd see if I could find something to do with predicting the result, rather than analyzing the results afterwards (as in this Eurovision results with crossfilter and dc.js example). Turns out that Predictwise have lots of predictions, including the likely winners of the Eurovision. They don't seem to actually have an API, but they do have some tables with results in them. I also couldn't find any copyright info, so this is intended purely as a demo exercise. You'll need to look into the copyright yourself if you plan to use this data. A little scraping with Apps Script showed me what they had, which I put in the index tab in this sheet. // do a bit of digging to see what tables exist function scrape() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sh = ss.getSheetByName("index"); var hits = []; sh.getDataRange().clearContent(); sh.getRange(1,1,1,4).setValues ([["id","name","notes","timestamp"]]); for (var i = 1; i <5000 ; i++ ) { var result = getData (i); if (result) { hits.push ([result.id,result.name,result.notes,result.timestamp]); } } if (hits.length) { sh.getRange (2,1,hits.length,hits[0].length).setValues(hits); }
So with this snippet// can be called from sheet - just supply the table number function importPredictWise(tableNumber) { var table = getData (tableNumber) ; var data = [table.header]; Array.prototype.push.apply(data,table.table); return data; } function getData (tableNumber) { var URL = "http://table-cache1.predictwise.com/latest/table_"; var result = UrlFetchApp.fetch(URL+(tableNumber||321).toString()+".json",{ muteHttpExceptions:true }); if (result.getResponseCode() !== 404 && result.getResponseCode() !== 200) { throw 'error ' + result.getResponseCode() + ' ' + result.getContentText(); } return result.getResponseCode() === 404 ? null : JSON.parse(result.getContentText()); } You can do this on your sheet=importPredictWise(some-id-from-the-index-sheet) and pull in any of the 000's of predictions I found. ..such as the eurovision 2015 result (=importPredictWise(1036))
..or the likely 2016 presidential winner (=importPredictWise(321))
For more like this, see Google Apps Scripts snippets. Why not join our community , follow the blog, twitter, G+ .
You want to learn Google Apps Script?Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly. If you prefer Video style learning I also have two courses available. also published by O'Reilly. |
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >