Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write from Google Firebase to Google Sheets using Google Apps script

Trying to retrieve form entries which are stored in google firebase under the node called entries and append to a google sheet using the script editor in google sheets.

I have added the FirebaseApp library to google sheet script editor. Then my code looks like this:

function getAllData() {
  var firebaseUrl = "https://myapp.firebaseio.com/";
  var secret = "pCOCwKCC582jpqdZe2EqPqnW3IAd3UyO9oB4uaEL2";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl, secret);
  var data = base.getData();
    Logger.log(data);
}

when I run this nothing happens. Any ideas?

Next I need to add the returned data from firebase to the google sheet. I was using this code to do this via the sheets api, however I'm not sure how this works in the google script editor?

  function addEntries() {
    gapi.client.sheets.spreadsheets.values.append({
      spreadsheetId: '10lyQpQtEA7euCfdU2isrqB_bgPuy-eSbW74h7oDP3ko',
      range: "Sheet1!A1:D100",
      majorDimension: "ROWS",
        "values": [
          ["testa", "testb", "testc", "testd"]
        ],
      valueInputOption: 'USER_ENTERED'
    }).then(function(response) {

    }, function(response) {
      appendPre('Error: ' + response.result.error.message);
    });
  }
like image 534
uknowit2 Avatar asked Dec 15 '22 04:12

uknowit2


1 Answers

I'm using the newest Firebase version. This snippet code works for me.

function getFacturasClientesExistentes() {
var firebaseUrl = "https://test.firebaseio.com/FacturasBLP/clienteExistente";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData();

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Facturas Clientes Existentes");

var num = 2;
range = sheet.getRange("A"+num+":F"+num+"");
for(var i in data) {
  var values = [
  [ data[i].fecha, data[i].sucursal, data[i].cantidad, data[i].cliente, data[i].correo, data[i].estatus ]
  ];
  range.setValues(values);
  num += 1;
  range = sheet.getRange("A"+num+":F"+num+"");
  } 
}

Some notes:

  1. I have previously write the headers for my data in the spreadsheet
  2. In the line range = sheet.getRange("A"+num+":F"+num+""); from A to F I have my headers
like image 65
Xaverius Avatar answered Dec 28 '22 05:12

Xaverius