Skip to content

Instantly share code, notes, and snippets.

@leifwells
Last active December 29, 2018 05:35
Show Gist options
  • Save leifwells/7d5b7a8bb4bf0130d9faf1434ba953b8 to your computer and use it in GitHub Desktop.
Save leifwells/7d5b7a8bb4bf0130d9faf1434ba953b8 to your computer and use it in GitHub Desktop.
Example Ionic 2/Angular 2 Provider for a Google spread sheet
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the GoogleDrive provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class GoogleDriveProvider {
data: any = null;
constructor(public http: Http) {}
load( id ) {
if (this.data) {
// already loaded data
return Promise.resolve(this.data);
}
var url = 'https://spreadsheets.google.com/feeds/list/' + id + '/od6/public/values?alt=json';
// don't have the data yet
return new Promise(resolve => {
// We're using Angular Http provider to request the data,
// then on the response it'll map the JSON data to a parsed JS object.
// Next we process the data and resolve the promise with the new data.
this.http.get(url)
.map(res => res.json() )
.subscribe( data => {
console.log( 'Raw Data', data );
this.data = data.feed.entry;
let returnArray: Array<any> = [];
if( this.data && this.data.length > 0 ) {
this.data.forEach( ( entry, index ) => {
var obj = {};
for( let x in entry ) {
if( x.includes('gsx$') && entry[x].$t ){
obj[x.split('$')[1]] = entry[x]['$t'];
// console.log( x.split('$')[1] + ': ' + entry[x]['$t'] );
}
}
returnArray.push( obj );
});
}
resolve(returnArray);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment