I'm using FirestoreApp in Google Script to access my Firestore database. 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. Any idea what I'm doing wrong?
Thanks!
Puh, here we go:
Easy to amend the logic in case you need to loop through a json object of an object of an 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);
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.