首先使用 angular 获取然后 Post google sheet 数据到 firebase 数据库

First Get and then Post google sheet data to firebase database using angular

我正在尝试以 JSON 形式从 google sheet 获取数据,然后使用 angular post 将这些数据 post 到 firebase 数据库。 到目前为止,我能够以 JSON 的形式获取 google sheet 数据: 我得到的响应是列和行的数组。我的问题是如何向 firebase 发出 post 请求以 post 这些响应? 为了发出获取请求,我做了以下方法。 登陆-page.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)));
    }
       
 });
}
}

Field.ts

export interface Field{
    Id:string;
    Name:string;
    Major:string;
}
  1. 安装 AngularFirestore
  2. 声明:

私有 collection:AngularFirestoreCollection;

myCollection: Observable<模型[]>;

3.

constructor(private afs: AngularFirestore) { 
            this.collection = this.afs.collection<Model>("collectionName");
            this.myCollection = this.collection.valueChanges();
          }
  1. 在你的函数中:

    this.collection.doc().set(Object.assign({}, newItemToSave));