[英]Print Firestore array to Google Sheets
I'm using FirestoreApp in Google Script to access my Firestore database.我在 Google Script 中使用 FirestoreApp 来访问我的 Firestore 数据库。 In the database I have a document with an array which I'd like to print out as a table.在数据库中,我有一个包含数组的文档,我想将其打印为表格。
Here's my code:这是我的代码:
var allDocuments = firestore.getDocuments("finished/AUG1/2021-3").map(function(doc) {
return doc.fields;
});
const first = allDocuments[0];
const colum = Object.keys(first);
const sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(colum);
allDocuments.forEach(function(doc) {
const row = colum.map(function(clm) {
//return doc[clm].toString(); //if documents has array use toString();
return JSON.stringify(doc[clm], null, 4);
})
sheet.appendRow(row);
})
}
I get the following in return:我得到以下回报:
"{
""arrayValue"": {
""values"": [
{
""stringValue"": ""64MR6""
},
{
""stringValue"": ""64MR6""
},
{
""stringValue"": ""64MR6""
}
]
}
}"
I'd like to print out all the "64MR6" below or next to each other, but somehow whatever I try I can't seem to get it right.我想打印出下面或彼此相邻的所有“64MR6”,但不知何故,无论我尝试什么,我似乎都无法做到正确。 Any idea what I'm doing wrong?知道我做错了什么吗?
Thanks!谢谢!
Puh, here we go:噗,我们这里是 go:
Easy to amend the logic in case you need to loop through a json object of an object of an object.....易于修改逻辑,以防您需要遍历 object 的 object 的 json object ...........
var firestore = FirestoreApp.getFirestore (email, key, projectId);
var allDocuments = firestore.getDocuments("finished/AUG1/2021-3").map(function(doc) {
return doc.fields;
});
const first = allDocuments[0];
const colum = Object.keys(first);
const sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(colum);
allDocuments.forEach(function(doc) {
const row = colum.map(function(clm) {
//return doc[clm].toString(); //if documents has array use toString();
//return JSON.stringify(doc[clm], null, 4);
//console.log(console.log(doc[clm]["arrayValue"]["values"][1]).toString());
for (i=0;i<doc[clm]["arrayValue"]["values"].length;i++)
{
for (const [key, value] of Object.entries(doc[clm]["arrayValue"]["values"][i])) {
//console.log(`${key}: ${value}`);
console.log(`${value}`);
}
}
})
sheet.appendRow(row);
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.