How to send one parent with multi child to firebase over Google app script?

北城余情 提交于 2021-02-10 06:12:34

问题


i have G-AppScript code where should make item batches as child of the parent "Article code" like photo below:

google sheets data table:

<code>google sheets data table</code>

the target result

<code>the target result</code>

i tried to write the link below:

G-AppScript

function writeDataToFirebase() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  for(var i = 1; i < data.length; i++) {

var ItemCode = data[i][0];
var Batch = data[i][3];

dataToImport[ ItemCode ]  = {
  ItemDesc:data[i][1],
  VenCode:data[i][8],
  VenName:data[i][9],
}

for(var i = 1; i < data.length; i++) {
  var Batch = data[i][3];
  dataToImport[ Batch ] = {
    barcode:data[i][2],
    ExpDate:data[i][4],
    ComPrice:data[i][5],
    TaxVal:data[i][6],
    PubPrice:data[i][7]
   }
  }
 }
 var firebaseUrl = "https://example.firebaseio.com/";
 var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
 base.setData("itemsInventory", dataToImport);
}

but i didn't get the batches as child of item code "parent"

this is the result that i got.

the founded result


回答1:


If you want the batches to be children of the article number, you need to nest the loops,iterate over the batches for each parent and push them into a batch array inside the inner loop. Then you insert the batch array as a key-value pair into dataToImport[ ItemCode] - and this for every article number, inside the outer loop.

Here is a code snippet that implements this functionality:

function writeDataToFirebase() {
  var ss = SpreadsheetApp.openById("XXX");
  var sheet = ss.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  var allBatches ={}; 
  for(var i = 1; i < data.length; i++) {
    allBatches =[]; 
    var ItemCode = data[i][0];    
    for(var j = i; j < data.length; j++) {    
      var Batch = data[j][3];
      if(data[j][0]==ItemCode)
      {  
        allBatches[Batch] = {
        barcode:data[j][2],
        ExpDate:data[j][4],
        ComPrice:data[j][5],
        TaxVal:data[j][6],
        PubPrice:data[j][7]
        }
      }
     else
      {
       break;
      }   
     }
    if(ItemCode != data[i-1][0])
     {
       dataToImport[ ItemCode ]  = { 
        ItemDesc:data[i][1],
        VenCode:data[i][8],
        VenName:data[i][9],
        Batch:allBatches
       }
     }
  }

  var base = FirebaseApp.getDatabaseByUrl("XXX")
  base.setData("", dataToImport);  
}

And a screenshot of the result:

Please note that the parameters between the brackets in the dictonaries allBatches[Batch] and dataToImport[ItemCode] must be numbers (without letters).



来源:https://stackoverflow.com/questions/57007514/how-to-send-one-parent-with-multi-child-to-firebase-over-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!