使用Google Apps脚本从Firebase读取数据... [英] 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.
我有一个Google Spreadsheet,其中包含带有名称,电子邮件和有效期的模板.但是,它不包含任何实际数据.数据本身位于Firebase中,并且不断变化.因此,我的目标是允许通过计时间隔或使用Firebase的典型dataRef.on('value',function(){});
从Firebase读取附加到我的电子表格的script.gs
文件.
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(){});
.
我已经尝试过使用Web应用程序,但是Caja制作了Firebase代码的百果馅,而使用Firebase对象则必须这样做.我尝试将Firebase脚本包含在库中,然后将其直接复制到script.gs
中.什么都没用.
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.
我相信我的问题很大程度上源于gs环境,即服务器.但是,将cURL
放在script.gs
中时似乎不起作用.无论如何,希望你们中的一个有答案.如果您需要进一步说明,请告诉我.
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.
推荐答案
我将Firebase与Google Apps脚本一起使用.您需要使用Google的urlFetch. Google文档
I use Firebase with Google Apps Script. You need to use Google's urlFetch. Google Documentation
var optList = {"method" : "get"};
var recordName = "theUserID";
var rsltList = UrlFetchApp.fetch("https://yourFireBaseDB_name.firebaseio.com/" +
recordName + "/.json", optList );
在上面的示例中,向Firebase发出了 GET 请求.您要从中获取数据的数据库必须包含在URL中. URL必须以/.json
结尾.您的数据库结构反映在URL中.在上面的示例中,数据库的每个用户ID都有不同的记录,并且该用户ID被放入URL中.
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()方法可以具有一个或两个参数.
UrlFetchApp.fetch() method can have one or two parameters.
fetch(url, params)
您需要使用高级参数来指定要发送的请求类型:例如: PUT,GET .注意:您不能直接使用UrlFetchApp.fetch
指定 PATCH 请求,但是可以在标题中放置替代以发送 PATCH 请求.
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.
使用 PATCH 请求更安全.
如果您决定使用UrlFetchApp.fetch
将数据写入Firebase,则很高兴知道,如果URL配置不正确,使用 PUT 请求可以覆盖并清除所有数据.
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.
在上面的示例中,返回的数据被放入变量:rsltList
In the above example, the returned data gets put into the variable: rsltList
fetch方法的第二个参数是一个对象. {"method" : "get"}
The second parameter of the fetch method is an object. {"method" : "get"}
对象是key:value
对.
这篇关于使用Google Apps脚本从Firebase读取数据...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!