Firebase RealtimeDB to Google Sheets through AppScript - firebase

I am trying to write Firebase RealtimeDB data into a Google Sheets spreadsheet through Google Appscript.
This is what my RealtimeDB looks like:
My goal is
I have tried the following code snippet:
function writeSheets() {
var firebaseUrl = "https://<mydatabase>.firebaseio.com/Attendees";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData();
var ss = SpreadsheetApp.openById("<mySheet>");
var sheet = ss.getSheetByName("Sheet1");
var num = 2;
range = ss.getRange("A"+num+":B"+num+"");
for(var i in data) {
var values = [[ data[i].ID, data[i].Name ] ];
range.setValues(values);
num += 1;
range = sheet.getRange("A"+num+":B"+num+"");
}
console.log(JSON.stringify(data));
}
It seems to print empty cells into Google Sheet. Not exactly sure why.
Any input would be greatly appreciated. Thanks.
EDIT:
Console Log :
[20-04-08 20:46:26:102 HKT] TypeError: Cannot read property 'ID' of null
at writeSheets(Code:13:27)

There is a extra null in the data. Filter it out and map the array of objects to a 2D array and setValues the 2D array:
const data = [null,{"ID":1001,"Name":"Bob"},{"ID":1069,"Name":"Steve"},{"ID":1420,"Name":"Bill"}];
const out = data.filter(Boolean).map(Object.values);
console.log(out);
/*SetValues the entire array*/
//sheet.getRange(1,1,out.length, out[0].length).setValues(out);

Related

recursive function on each pixel in google earth engine

I want to filter time series in the google earth engine which requires two for loops over time-series of a single pixel. I searched around and not found any example related to this. I know about .map function and I am using it for the generation of RVI on the earth engine. I found about .toArray function but not found any example related to my problem.
I will appreciate any help in this regard. Also, I am new to the earth engine so this may be a trivial question for others.
This is the code that I have. I took it from a blog and modified it according to my need. I am stuck after this.
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD_FLOAT');
// Filter VH, IW
var vh = sentinel1
// Filter to get images with VV and VH dual polarization.
//.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
// Filter to get images collected in interferometric wide swath mode.
.filter(ee.Filter.eq('instrumentMode', 'IW'))
// reduce to VH polarization
//.select('VH')
// filter 10m resolution
.filter(ee.Filter.eq('resolution_meters', 10));
// Filter to orbitdirection Descending
var vhDescending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'));
// Filter time 2015
var vhDesc2015 = vhDescending.filterDate(ee.Date('2021-01-01'), ee.Date('2021-04-30'));
// Filter to MKD roi
var s1_mkd = vhDesc2015.filterBounds(roi);
print('All metadata:', s1_mkd);
var count = s1_mkd.size();
print('Count: ', count);
//var dates = s1_mkd.aggregate_array("system:time_start")
//print('dates: ', dates);
var dates = s1_mkd
.map(function(image) {
return ee.Feature(null, {'date': image.date().format('YYYY-MM-dd')})
})
.distinct('date')
.aggregate_array('date')
print('dates: ', dates);
var featureCollection = ee.FeatureCollection(dates
.map(function(element){
return ee.Feature(null,{prop:element})}))
//Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
collection: featureCollection,
description: 'Timeseries',
folder: 'WC_raw',
fileFormat: 'CSV',
});
var rvi4s1 = function(img){
var vh = img.select('VH');
var vv = img.select('VV');
var col = vv.divide(vv.add(vh)).sqrt().rename('dop');
var dop = col.select('dop')
var value = dop.multiply(vh.multiply(4).divide(vv.add(vh))).rename('rvi4s1');
return value;
};
var rvi = s1_mkd.map(rvi4s1);
print(rvi);

Exception: Bad value (line 9, file "Code")

I want to read my data from my firebase and write it in google sheet. When i run this Apps Script I got the error
Exception: Bad value (line 9, file "Code")
var ss= SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range=sheet.getRange(1,1,4,2);
var data= getFirebaseData("Entries");
Logger.log(data)
range.setValues(JSON.parse(data))
}
function getFirebaseData(data){
var firebaseUrl = "**";
var secret = "**";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl,secret);
var result = base.getData(data);
return result;
}
the data in Firebase is like this:
Entries
date:"09/09/2020"
docAmount:88
docNo:55
partyName: "ffg"
Solutions that I might accept,(solve this issue)or (show a better way to do the process)
You want to write an object's (data) key/value pairs to two columns in a sheet. setValues accepts a 2D array, not an object, so you should first transform data to a 2D array:
const array = Object.keys(data).map(key => [key, data[key]]);
sheet.getRange(1, 1, array.length, array[0].length).setValues(array);
Note:
Making the range dimensions dependent on the array dimensions (array.length, array[0].length) will give you more flexibility (what if the object has 5 properties instead of 4?).
This will only work for simple objects, not if they have nested properties.
Reference:
setValues(values)
Object.keys()
getData(path, optQueryParameters)

Read Data from Firebase to Sheets

I'm having trouble splitting the data once I have retrieved it using .getData().
The firebase data is in the form:
mainTag: subTag1:"["Test1",70,0,18]", subTag2:"["Test2",65,2,18]", etc...
This is as far as I've managed to get as I'm not sure of the format of the data. I can't directly set it to the value of a single cell or a range of cells because the parameters don't match the method signature for the sheet. I've tried splitting the data as if it were an object and as if it were a string but keep getting nulls logged in the logger so I'm not sure what to do with it.
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("TestSheet");
var range = sheet.getRange("A1:D2");
var firebaseUrl = "https://.....firebaseio.com/";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData("mainTag");
//range.setValues(data);
Logger.log(data);
Logs: {subTag2=["Test2",65,2,18], subTag1=["Test1",70,0,18]}
}
My objective is to set the value of 4 cells in a row to the data inside each subtag. (A1="Test1", B1=70, C1=0, D1=18) Then subtag2 is in row 2 and so on. I haven't got to splitting the data yet as I'm not sure how to format the data from firebase for it to be able to be used in .setValues()
You need to loop through the subtags and access the values belonging to each key
This can be best achieved with Object.keys().
Sample:
var data = base.getData("mainTag");
var array = [];
for(var i in data) {
var newdata = Object.keys(data[i]).map(function (key) {
return data[i][key];
});
array.push(newdata);
}
var range = sheet.getRange(1,1, array.length, array[0].length);
range.setValues(array);

Limit with getting info about audience from Analytics API

I'm trying to get audienc name aduience id etc we' ve created on our google analytics account. We have around 2,4k audiences list but I can just get 999 of them. I can't find any soultions. Code is below
function main() {
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadshe');
var sheet = spreadsheet.getSheetByName('Sh');
function listRemarketingAudiences(accountId, propertyId) {
var request = Analytics.Management.RemarketingAudience.list(
accountId,
propertyId
);
var leno = Object.keys(request).length
console.log(leno);
sheet.getRange(1,1).setValue("audianceName");
sheet.getRange(1,2).setValue("audianceId");
sheet.getRange(1,3).setValue("audianceDefinition");
sheet.getRange(1,4).setValue("audianceDescription");
for ( var i = 2; i <3000; i++) {
var audianceName = request.items[i+154].name ;
Logger.log(audianceName);
console.log(i);
sheet.getRange(i,1).setValue("elo")
var audianceId = request.items[i].id ;
sheet.getRange(i,2).setValue(audianceId);
// var audianceId = request.items[i].
var audienceDefinition = request.items[i].audienceDefinition ;
sheet.getRange(i,3).setValue(audienceDefinition);
var audienceDescription = request.items[i].description ;
sheet.getRange(i,4).setValue(audienceDescription);
};
}
listRemarketingAudiences('xxxxx', 'UA-xxxxx-1');
}
Currently you are supplying only the required parameters: accountId and webPropertyId. These are necessary to identify the Analytics property, where you are looking for the data.
Based on the documentation, optional parameters can be passed, which are actually in connection with the pagination, which you are trying to achieve.
As the developer guide is not mentioning the absolute limit of the result, you could experiment with higher limits, with a code something like this:
request = gapi.client.analytics.management.remarketingAudience.list(
{
'accountId': accountId,
'webPropertyId': propertyId,
'max-results': 5000
}
If you can't get all the data at once, you need to implement paging yourself, where an other paramerer, start-index will be necessary. You need to call the function several times, preferably from a loop, where start index is continuously increased.
request = gapi.client.analytics.management.remarketingAudience.list(
{
'accountId': accountId,
'webPropertyId': propertyId,
'start-index': 999,
'max-results': 1000
}
I wrote sth like this:
var optional = {'startIndex': 12,
'maxresults': 212};
function listRemarketingAudiences (accountId, propertyId, optional){
var request = Analytics.Management.RemarketingAudience.list(
accountId,
propertyId,
optional.maxresults
);
and an error occure:
We're sorry, a server error occurred. Please wait a bit and try again. (line 9, file "Code")

How to separate multiple columns from a range in an array?

I have a range of data in a Google Sheet and I want to store that data into an array using the app script. At the moment I can bring in the data easily enough and put it into an array with this code:
var sheetData = sheet.getSheetByName('Fruit').getRange('A1:C2').getValues()
However, this puts each row into an array. For example, [[Apple,Red,Round],[Banana,Yellow,Long]].
How can I arrange the array by columns so it would look: [[Apple,Banana],[Red,Yellow],[Round,Long]].
Thanks.
It looks like you have to transpose the array. You can create a function
function transpose(data) {
return (data[0] || []).map (function (col , colIndex) {
return data.map (function (row) {
return row[colIndex];
});
});
}
and then pass the values obtained by .getValues() to that function..
var sheetData = transpose(sheet.getSheetByName('Fruit').getRange('A1:C2').getValues())
and check the log. See if that works for you?
Use the Google Sheets API, which allows you to specify the primary dimension of the response. To do so, first you must enable the API and the advanced service
To acquire values most efficiently, use the spreadsheets.values endpoints, either get or batchGet as appropriate. You are able to supply optional arguments to both calls, and one of which controls the orientation of the response:
const wb = SpreadsheetApp.getActive();
const valService = Sheets.Spreadsheets.Values;
const asColumn2D = { majorDimension: SpreadsheetApp.Dimension.COLUMNS };
const asRow2D = { majorDimension: SpreadsheetApp.Dimension.ROWS }; // this is the default
var sheet = wb.getSheetByName("some name");
var rgPrefix = "'" + sheet.getName() + "'!";
// spreadsheetId, range string, {optional arguments}
var single = valService.get(wb.getId(), rgPrefix + "A1:C30");
var singleAsCols = valService.get(wb.getId(), rgPrefix + "A1:C30", asColumn2D);
// spreadsheetId, {other arguments}
var batchAsCols = valService.batchGet(wb.getId(), {
ranges: [
rgPrefix + "A1:C30",
rgPrefix + "J8",
...
],
majorDimension: SpreadsheetApp.Dimension.COLUMNS
});
console.log({rowResp: single, colResp: singleAsCols, batchResponse: batchAsCols});
The reply will either be a ValueRange (using get) or an object wrapping several ValueRanges (if using batchGet). You can access the data (if any was present) at the ValueRange's values property. Note that trailing blanks are omitted.
You can find more information in the Sheets API documentation, and other relevant Stack Overflow questions such as this one.

Resources