do something in 5 with apps scripts number 6 - fusion crossfilter

14

Click here to load reader

Upload: bruce-mcpherson

Post on 11-Jul-2015

110 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: Do something in 5 with apps scripts number 6 - fusion crossfilter

do something useful with Apps Script in 5 minutes

6. Fusion, Crossfilter and SheetsBruce McPhersonwww.mcpher.com

Page 3: Do something in 5 with apps scripts number 6 - fusion crossfilter

Add libraries to script

● create a spreadsheet● get its id● create a script● Open resources● Add references to librariesMHfCjPQlweartW45xYs6hFai_d-phDA33

Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j

MyhWrchJeGiOowTfrMNidiSz3TLx7pV4j

M-tju_1qulZXW63vIuwyLOqi_d-phDA33

Page 4: Do something in 5 with apps scripts number 6 - fusion crossfilter

Add Fusion service

in advanced services, enable Fusion

Page 5: Do something in 5 with apps scripts number 6 - fusion crossfilter

Enable Fusion on cloud console

Page 6: Do something in 5 with apps scripts number 6 - fusion crossfilter

Workaround for fusion issue● When the Fusion API is called from a library, the script

doesn’t know it needs authorization● You need to mention Fusion in your script, even as a

comment to provoke an authorization dialog.● Put this somewhere in your script - amazing but true,

// FusionTables.Table - this will provoke a fusion authorization

Page 7: Do something in 5 with apps scripts number 6 - fusion crossfilter

layout what you are going to do

function myFunction() {

// open fusion table

// open spreadsheet as database

// get all the data

// load it to crossFilter

// create a states dimension

// create a summary of how many airports in each state

// clear sheet

// write results

}

// FusionTables.Table - this will provoke a fusion authorization

Page 8: Do something in 5 with apps scripts number 6 - fusion crossfilter

Open Fusion data sourceOpen a Fusion table - this table has a list of all airports in each state

// open fusion table

var fusionHandler = new cDbAbstraction.DbAbstraction (cDriverFusion, {

dbid:'airports',

siloid:'1Ug6IA-L5NKq79I0ioilPXlojEklytFMMtKDNzvA'

});

if (!fusionHandler.isHappy()) throw 'unable to open fusion table';

Page 9: Do something in 5 with apps scripts number 6 - fusion crossfilter

Open Sheet for resultsOpen a Sheet … we’ll write the results here. Replace the key with the one for the spreadsheet you created

// open spreadsheet as database

var handler = new cDbAbstraction.DbAbstraction (cDriverSheet, {

siloid:'airporcountbystate',

dbid:'1a4oHUWS4sgR_VwjlU6ycyqc0DlUVpKNtEIVXKVwyfLs',

});

if (!handler.isHappy()) throw 'unable to open sheet';

Page 10: Do something in 5 with apps scripts number 6 - fusion crossfilter

Get all the data from FusionThis will pick up all the airport data from the Fusion Table

// get all the data

var result = fusionHandler.query();

if (result.handleCode < 0) throw result.handleError;

Page 11: Do something in 5 with apps scripts number 6 - fusion crossfilter

Use crossfilterCrossfilter is cool library for data wrangling. Great for summarizing and grouping.

// load it to crossFilter

var cf = cCrossFilter.crossfilter(result.data);

// create a states dimension

var states = cf.dimension ( function (d) { return d.state });

// write a summary of how many airports in each state

var stateSummary = states.filter(null).group().all();

Page 12: Do something in 5 with apps scripts number 6 - fusion crossfilter

Write results to a sheetClear any current contents, and write the results out

// clear sheet

var result = handler.remove();

if (handler.handleCode < 0) throw handler.handleError;

var result = handler.save(stateSummary);

if (handler.handleCode < 0) throw handler.handleError;

Page 13: Do something in 5 with apps scripts number 6 - fusion crossfilter

Homework

Read up about crossfilter and see what other kind of summarization, filtering and analysis you can do with this dataset