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 script.

I have a Google spreadsheet containing a template with name, email and expiration date. However, it contains no actual data. The data itself is in Firebase and is constantly changing. My goal is to allow the file script.gs

attached to my spreadsheet to be read from Firebase, either at a time slot or using Firebase typical dataRef.on('value',function(){});

.

I've already tried using a web app, but Caja produces a Firebase mince that you must use to use the Firebase object. I tried to include the Firebase script as a library and then copied it directly into script.gs

. Nothing succeeded.

I believe my problem is mainly related to the gs environment, i.e. the server. However cURL

, it doesn't seem to work when posted to script.gs

. Anyway, hopefully one of you has an answer. If you need further clarification, please let me know.

+3


source to share


2 answers


I am using Firebase with Google Apps Script. You need to use Google urlFetch. Google Docs

EXAMPLE CODE: 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 being made in firebase . The database you want to fetch data into must be included in the URL. The URL must end with /.json

. Your database structure appears in the url. In the above example, the database has a different entry for each user ID and the user ID is put into the URL.

The UrlFetchApp.fetch () method can take one or two parameters.

fetch(url, params)

      

retrieving documentation



You need to use advanced parameters to specify what type of request is sent: for example: PUT, GET . Note. You cannot assign a PATCH request directly with UrlFetchApp.fetch

, but you can put an override in the header to send a PATCH request .

It is much safer using a PATCH request .

If you choose to write data to Firebase with UrlFetchApp.fetch

, it's good to know that using a PUT request can overwrite and destroy all of your data if the url is not configured correctly.

In the above example, the returned data goes into a variable: rsltList

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

The object is a pair key:value

.

+5


source


Ok, I figured out a pretty hacky way to do it, but it does the job anyway. I have set up a local script to read from Firebase. Every time a value is added, the script sends an ajax call to a google form that has a spreadsheet full of responses. Ajax call simulates form submission and viola! I can send information from a client via Firebase to a second client via ajax in googlesheethe where I script.gs

can access the information and act on it. The best part of this solution IMHO is that I don't have to mess with google paranoid code / mutilators parsers :)



0


source







All Articles