我试图从表单中的谷歌表中获取数据JSON
,然后使用 angular 将这些数据发布到 firebase 数据库。到目前为止,我能够以 JSON 的形式获取谷歌工作表数据:我得到的响应是列和行的数组。我的问题是如何向 firebase 发出发布请求以发布这些响应?为了发出获取请求,我执行了以下方法。登陆页面.ts
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../shared/auth.service';
import { Field } from '../model/field';
@Component({
selector: 'app-landing-page',
templateUrl: './landing-page.page.html',
styleUrls: ['./landing-page.page.scss'],
})
export class LandingPagePage implements OnInit {
private collection: AngularFirestoreCollection;
myCollection: Observable<Field[]>;
constructor(private auth: AuthService, private router:Router, private http :HttpClient,private afs: AngularFirestore) {
this.collection = this.afs.collection("Exceldata");
this.myCollection = this.collection.valueChanges();
}
ngOnInit() {
this.excelData();
}
logout()
{
this.auth.signOut();
}
excelData(){
var sf = "https://docs.google.com/spreadsheets/d/1qeCEUlVt_hnuyhnoT1wxMMSv7kZW1s4cUIRLynJ0TxQ/gviz/tq";
this.http.get(sf,{responseType: 'text'}).subscribe(res=>{
const data =res.toString().match(/google\.visualization\.Query\.setResponse\(([\s\S\w]+)\)/);
if(data && data.length==2){
const obj=JSON.parse(data[1]);
const table=obj.table;
const header = table.cols.map(({label}) => label);
const rows = table.rows.map(({c}) => c.map(({v}) => v));
console.log(header);
console.log(rows);
console.log(this.collection.doc().set(Object.assign({}, rows)));
}
});
}
}
字段.ts
export interface Field{
Id:string;
Name:string;
Major:string;
}
私人收藏:AngularFirestoreCollection;
myCollection: Observable<Model[]>;
3.
constructor(private afs: AngularFirestore) {
this.collection = this.afs.collection<Model>("collectionName");
this.myCollection = this.collection.valueChanges();
}
在您的功能中:
this.collection.doc().set(Object.assign({}, newItemToSave));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句