映射行和列以存储在 firebase 数据库中
Map row and columns to store in firebase database
我正在使用 google sheet 数据显示在我使用 angular 的页面中。当我尝试显示数据时,它以数组和字段名称的形式出现 0 1 2 3
。我想知道我们如何映射行和列,以便以正确的方式显示数据。这是我收到的回复的屏幕截图。
列:
(3) ['Id', 'Name', 'Major']
0: "Id"
1: "Name"
2: "Major"
行:
0: Array(3)
0: 202
1: "word"
2: "comp"
1: Array(3)
0: 203
1: "John"
2: "science"
我正在尝试获取如下数据:UserData:
Id:202,
Name:"word"
Major:"comp"
Id:203,
Name:"John"
Major:"Science"
我用过的方法是
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);
this.collection.doc().set(Object.assign({}, rows));
}
});
}
在您的情况下,下面的示例脚本怎么样?
示例脚本:
在此示例脚本中,使用了您的 header
和 rows
值。
const values = rows.map(e => header.reduce((o, f, j) => Object.assign(o, {[f]: e[j]}), {}));
使用您的示例值时,values
returns 以下值。
[
{"Id":202,"Name":"word","Major":"comp"},
{"Id":203,"Name":"John","Major":"science"},
{"Id":204,"Name":"nsia","Major":"cmpi"}
]
参考文献:
我正在使用 google sheet 数据显示在我使用 angular 的页面中。当我尝试显示数据时,它以数组和字段名称的形式出现 0 1 2 3
。我想知道我们如何映射行和列,以便以正确的方式显示数据。这是我收到的回复的屏幕截图。
列:
(3) ['Id', 'Name', 'Major']
0: "Id"
1: "Name"
2: "Major"
行:
0: Array(3)
0: 202
1: "word"
2: "comp"
1: Array(3)
0: 203
1: "John"
2: "science"
我正在尝试获取如下数据:UserData:
Id:202,
Name:"word"
Major:"comp"
Id:203,
Name:"John"
Major:"Science"
我用过的方法是
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);
this.collection.doc().set(Object.assign({}, rows));
}
});
}
在您的情况下,下面的示例脚本怎么样?
示例脚本:
在此示例脚本中,使用了您的 header
和 rows
值。
const values = rows.map(e => header.reduce((o, f, j) => Object.assign(o, {[f]: e[j]}), {}));
使用您的示例值时,
values
returns 以下值。[ {"Id":202,"Name":"word","Major":"comp"}, {"Id":203,"Name":"John","Major":"science"}, {"Id":204,"Name":"nsia","Major":"cmpi"} ]