问题
I am trying to find how to use the Google Sheets API in Angular. There is no reference to Angular on the documentation page. I have tried this and it doesn't seem to be working.
Can someone direct me on how I can use the Google Sheets API inside of my Angular application?
I am currently using Angular 8
回答1:
I made an Angular library exactly for this use case!
With ng-google-sheets-db you can load data from Google Sheets in a breeze. You may check out the Stackblitz example app.
Installation
ng add ng-google-sheets-db
or
npm install ng-google-sheets-db
Usage
Google Sheets
- Create a Google Sheet:
- The first row must be the header.
- The following rows are your entries, one entry per row.
- You may have an active column, with which you can enable or disable rows/entries.
- A Google Sheets demo spreadsheet is available here.
- Share your sheet:
- [File] -> [Publish to the web] -> [Publish]
- Get the Spreadsheet ID (i.e.
1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA
): It is part of the Google spreadsheet URL. - Get the Worksheet ID: The Worksheet IDs are increasing numbers, starting at 1.
Angular
Add GoogleSheetsDbService
to your app's module as a provider and Angular's HttpClientModule
to the imports:
import { HttpClientModule } from '@angular/common/http';
import { GoogleSheetsDbService } from 'ng-google-sheets-db';
@NgModule({
...
imports: [
HttpClientModule,
...
],
providers: [ GoogleSheetsDbService ],
...
})
export class AppModule { }
Import and inject into your component's constructor:
import { GoogleSheetsDbService } from 'ng-google-sheets-db';
@Component({
...
})
export class YourComponent implements OnInit {
characters$: Observable<Character[]>;
constructor(private googleSheetsDbService: GoogleSheetsDbService) { }
ngOnInit(): void {
this.characters$ = this.googleSheetsDbService.get<Character>('1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA', 1, characterAttributesMapping);
}
Attributes Mapping
The attributesMapping
maps the Google spreadsheet columns to to your outcome object.
const attributesMapping = {
id: 'ID',
name: 'Name',
email: 'Email Address',
contact: {
_prefix: 'Contact',
street: 'Street',
streetNumber: 'Street Number',
zip: 'ZIP',
city: 'City'
},
skills: {
_prefix: 'Skill',
_listField: true
}
};
For example, the Google spreadsheet column Email Address is mapped to the outcome object attribute email
.
contact
is an example of a nested object. You may define a _prefix
as a prefix for all columns of the nested object.
skills
is an example of a list. You need to set _listField
and a _prefix
for all columns of the list. In this example, all columns starting with Skill and an increasing number are part of the list, i.e. Skill 1, Skill 2, etc.
It comes with a step by step usage guide and a demo application!
回答2:
Check this article: http://leifwells.com/2016/06/09/ionic-2--angular-2-using-a-google-spreadsheet-as-a-data-source/
You don't need any external package, but to perform a http request to your sheet.
Considering the above you can try something like this:
Publish to the web you sheet:
Go to your google sheet, then select
File > Publish to the Web
option which will end up giving you a URL which has an id inside of it which is important. In our case the url looks like this:https://docs.google.com/spreadsheets/d/15Kndr-OcyCUAkBUcq6X3BMqKa_y2fMAXfPFLiSACiys/pubhtml
The id that is in this url is:
15Kndr-OcyCUAkBUcq6X3BMqKa_y2fMAXfPFLiSACiys
Create a Google sheet provider (using as reference the first link):
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators';
@Injectable()
export class GoogleDriveProvider {
data: any = null;
constructor(public http: Http) { }
public getSheetData(): Observable<any> {
const sheetId = '15Kndr-OcyCUAkBUcq6X3BMqKa_y2fMAXfPFLiSACiys';
const url = `https://spreadsheets.google.com/feeds/list/${sheetId}/od6/public/values?alt=json`;
return this.http.get(url)
.pipe(
map((res: any) => {
const data = res.feed.entry;
const returnArray: Array<any> = [];
if (data && data.length > 0) {
data.forEach(entry => {
const obj = {};
for (const x in entry) {
if (x.includes('gsx$') && entry[x].$t) {
obj[x.split('$')[1]] = entry[x]['$t'];
}
}
returnArray.push(obj);
});
}
return returnArray;
})
);
}
}
Note: The example is using Angular 8.
来源:https://stackoverflow.com/questions/59036475/google-sheets-api-in-angular