kru*_*ens 5 google-sheets-api angular
我试图找到如何在 Angular 中使用 Google Sheets API。文档页面上没有对 Angular 的引用。我已经尝试过这个,但它似乎不起作用。
有人可以指导我如何在我的 Angular 应用程序中使用 Google Sheets API 吗?
我目前正在使用 Angular 8
我专门为这个用例制作了一个 Angular 库!
使用ng-google-sheets-db,您可以轻而易举地从 Google 表格加载数据,并将Google 表格用作Angular 应用程序的(只读)后端!您可以查看Stackblitz 示例应用程序。
ng add ng-google-sheets-db
Run Code Online (Sandbox Code Playgroud)
或者
npm install ng-google-sheets-db
Run Code Online (Sandbox Code Playgroud)
1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA):它是 Google 电子表格 URL 的一部分。一个很好的概述指南是Workspace 开发人员入门。
添加GoogleSheetsDbService到您的应用程序的模块作为提供者和 Angular 的HttpClientModule导入:
import { HttpClientModule } from '@angular/common/http';
import { API_KEY, GoogleSheetsDbService } from 'ng-google-sheets-db';
@NgModule({
...
imports: [
HttpClientModule,
...
],
providers: [
{
provide: API_KEY,
useValue: <YOUR_GOOGLE_SHEETS_API_KEY>,
},
GoogleSheetsDbService
],
...
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
导入并注入组件的构造函数:
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', "Characters", characterAttributesMapping);
}
Run Code Online (Sandbox Code Playgroud)
将attributesMapping在谷歌电子表格列映射到你的结果对象。
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,
},
};
Run Code Online (Sandbox Code Playgroud)
例如,Google 电子表格列电子邮件地址映射到结果对象属性email。
contact是嵌套对象的示例。您可以将 a 定义_prefix为嵌套对象的所有列的前缀。请注意,_prefix可能需要尾随空格。
skills是一个列表的例子。您需要为列表的所有列设置_listField和_prefix。在此示例中,所有以 _Skill _ 开头且数字递增的列都是列表的一部分,即Skill 1、Skill 2等。请注意,_prefix可能需要尾随空格。
检查这篇文章:http://leifwells.com/2016/06/09/ionic-2--angular-2-using-a-google-spreadsheet-as-a-data-source/
您不需要任何外部包,但需要对您的工作表执行 http 请求。
考虑到上述情况,你可以尝试这样的事情:
将您的表格发布到网络:
转到你的谷歌表格,然后选择
File > Publish to the Web选项,最终会给你一个 URL,里面有一个 id,这很重要。在我们的例子中,url 如下所示:
https://docs.google.com/spreadsheets/d/15Kndr-OcyCUAkBUcq6X3BMqKa_y2fMAXfPFLiSACiys/pubhtml此 url 中的 id 是:
15Kndr-OcyCUAkBUcq6X3BMqKa_y2fMAXfPFLiSACiys
创建一个 Google 表格提供程序(使用第一个链接作为参考):
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;
})
);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:该示例使用 Angular 8。
| 归档时间: |
|
| 查看次数: |
8234 次 |
| 最近记录: |