-
-
Save denisazevedo/b8c5a37d817e8bbebc74fa261f8983c9 to your computer and use it in GitHub Desktop.
Example Ionic 2/Angular 2 Provider for a Google spread sheet
This file contains hidden or 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
| 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