Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Data from Firebase...with Google Apps Script

So, as the title suggests, I am currently working on a rather troublesome problem. Here's the scenario.

I have a Google Spreadsheet containing a template with name, email, and expiration date. However, it contains no actual data. The data itself is located in a Firebase and is constantly changing. My goal, then, is to allow the script.gs file attached to my spreadsheet to read from Firebase, either through a timed interval or by using Firebase's typical dataRef.on('value',function(){});.

I have already tried using a web app, but Caja makes mincemeat of Firebase's code, which you have to have in order to use the Firebase object. I tried including the Firebase script as a library, and later copied it directly into script.gs. Nothing worked.

I believe my problem is largely stemming from the gs environment, that is, the server. However, cURL does not seem to work when placed in script.gs. Anyway, hopefully one of you has an answer. If you require further clarification, let me know.

like image 984
J148 Avatar asked Aug 27 '14 14:08

J148


People also ask

How do I read data from Firebase realtime database?

After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.

How do I access data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I get data from Firebase REST API?

Streaming from the REST API To get started with streaming, we will need to do the following: Set the client's Accept header to text/event-stream. Respect HTTP Redirects, in particular HTTP status code 307. Include the auth query parameter if the Firebase database location requires permission to read.


1 Answers

I use Firebase with Google Apps Script. You need to use Google's urlFetch. Google Documentation

CODE EXAMPLE: Get Data

var optList = {"method" : "get"};
var recordName = "theUserID";

var rsltList = UrlFetchApp.fetch("https://yourFireBaseDB_name.firebaseio.com/" +
recordName + "/.json", optList );

In the above example, a GET request is made to firebase. The database you want data from must be included in the URL. The URL must end with /.json. Your database structure is reflected in the URL. In the above example, the database has a different record for each user ID, and the user ID is put into the URL.

UrlFetchApp.fetch() method can have one or two parameters.

fetch(url, params)

fetch Documentation

You need to use the advanced parameters to designate what type of request is being sent: For example: PUT, GET. Note: You can't designate a PATCH request directly with UrlFetchApp.fetch, but you can put an override in the header to send a PATCH request.

It's a lot safer using a PATCH request.

If you decide to write data to Firebase using UrlFetchApp.fetch, it's good to know that using a PUT request can overwrite and wipe out all your data if the URL is not configured correctly.

In the above example, the returned data gets put into the variable: rsltList

The second parameter of the fetch method is an object. {"method" : "get"}

The object is a key:value pair.

like image 147
Alan Wells Avatar answered Oct 25 '22 14:10

Alan Wells