Updating Firestore Document with GoogleSheet AppScript

170 views
Skip to first unread message

David OGUNDEPO

unread,
Feb 7, 2023, 9:53:21 PM2/7/23
to Google Apps Script Community
I'm finding it hard to update the fields in a firestore collection. The documentIDs of the persons are auto-generated. The inlined code below shows what I've been able to derive and make work, from the tutorials I have followed. Except for the 'updateFirestoreDocument' function, everything else works without error. How do I rewrite the code to export the modified cells on Google Sheets to the right persons' fields on the firestore collection? Thanks

..

"function onOpen() { SpreadsheetApp.getUi().createMenu('🔥 Firebase') 
 .addItem('⏪ Export to Firestore', 'main') 
 .addItem('⏩ Import from Firestore', 'menuImport') 
 .addToUi(); 

 function main() { 
var email = "firebaseunt.com"; 
var key = "-----BEGIN PRIE KEY-----\n"; 
var projectId = "cov-phoenix-fc"; 
var sheet = SpreadsheetApp.getActiveSheet(); 
var sheetName = sheet.getName(); 
var properties = getProperties(sheet); 
var records = getRecords(sheet); 
var firestore = FirestoreApp.getFirestore(email, key, projected); 

 updateFirestoreDocument(firestore, sheetName, documentId, properties, data); 
 exportToFirestore(firestore, sheetName, properties, records); 
 } 
 
function updateFirestoreDocument(firestore, collectionName, documentId, properties, data) { 
 var documentRef = firestore.getDocument(collectionName, documented); 
properties.forEach(function(prop) {
documentRef.updateData(prop, data[prop]); 
}); 
 } 
 function exportToFirestore(firestore, collectionName, properties, records) { records.map(function(record) { var data = {}; properties.forEach(function(prop,i) { data[prop] = record[i]; }); 
 return data; })
.forEach(function(data) { 
 // var documentId = data[properties[1]]; // first column 
 // firestore.createDocument(collectionName, documentId, data); 
firestore.createDocument(collectionName, data); 
// firestore.createDocument(collectionName, data, { id: data[1] }); }); } 

 function getProperties(sheet){
 return sheet.getRange(2, 1, 1, sheet.getLastColumn()).getValues()[0];
 } 

 function getRecords(sheet) { 
 var data = sheet.getDataRange().getValues(); 
 var dataToImport = []; 
 for (var i = 2; i < data.length; i++) { 
 dataToImport.push(data[i]); 
 } return dataToImport; 

Reply all
Reply to author
Forward
0 new messages