Colintree Custom listview extension update one element

I have figured it out, just needs a different syntax to generate the dataToImport:

Like so:

In sheet:

|name|id|colour|
|John|1|red|
|Dave|2|blue|
|Sue|3|green|
|Jane|4|yellow|

in script:

dataToImport[name] = '["'+[data[i][0] + '","' + data[i][1]+'","'+data[i][2]]+'"]' ;

which produces in firebase:

fbtest
Dave: "[\"Dave\",\"2\",\"blue\"]"
Jane: "[\"Jane\",\"4\",\"yellow\"]"
John: "[\"John\",\"1\",\"red\"]"
Sue: "[\"Sue\",\"3\",\"green\"]"

In Ai2 with project bucket set to fbtest you can Call the tag "Dave" and you get back a list

"["Dave","2","blue"]"

which you can then work with

thumbsup2

Here is my script:

function writeDataToFirebase2() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  for(var i = 1; i < data.length; i++) {
  var name = data[i][0];
  dataToImport[name] = '["'+[data[i][0] + '","' + data[i][1]+'","'+data[i][2]]+'"]' ;
  }
  var firebaseUrl = "https://XXXXX.firebaseio.com/";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
  base.setData("fbtest", dataToImport);
}

For readers/tryers: to get this to work, you have to add a library to your google apps script project. Best to follow THIS, get it working then modify to suit.

[EDIT]

and before anybody asks, this is how you write the script if you want to make a list of lists, and have all the data in the same tag: (uses same data as above)

function writeDataToFirebase3() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  var lolData = [];
  for(var i = 1; i < data.length; i++) {
    lolData.push('["'+[data[i][0] + '","' + data[i][1]+'","'+data[i][2]]+'"]') ;
  }
  dataToImport = '['+lolData+']';
  var firebaseUrl = "https://XXXXXX.firebaseio.com/";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
  base.setData("fbtest/myList", dataToImport);
}
2 Likes