Firebase data not working with datatables - html

I am trying to retrieve firebase data and then inserting into tables using the the following code.
My output page:
But my table is only showing the random keys from my "JSON" file on the webpage.The logs are showing keys as well as the data.How can i put the random key with its data in table?
This is my error message:
DataTables warning: table id=example - Requested unknown parameter '1' for row 0.
$(document).ready(function()
{
var rootRef=firebase.database().ref().child("Orders/")
var table = $('#example').DataTable();
rootRef.on("child_added",snap =>
{
var dataSet = [snap.key, snap.val().Nombre];
table.rows.add([dataSet]).draw();
console.log(snap.key);
console.log(snap.val());
});
});

on your var dataset add all data from firebase you want to put on the data Table, it works
rootRef.on("child_added", snap => {
var dataSet = [snap.child("name").val(), snap.child("email").val(), snap.child("blood").val(), snap.child("Phone").val(), snap.child("Location").val()];
table.rows.add([dataSet]).draw();

Change:
var dataSet = [snap.key, snap.val().Nombre];
To:
var dataset = [snap.child("name").val(), snap.val().Nombre];

var rootRef = firebase.database().ref().child("Topics");
rootRef.on("child_added", snap => {
var topic_name = snap.child("topic_name").val();
var uid = snap.child("topic_id").val();
var downloadURL = snap.child("downloadURL").val();
$("#tableHeading").append("<tr><td>"+ uid +"</td><td>"+ topic_name +"</td><td><img src='"+downloadURL +"' height='80px' width='80px'/></td><td><button>action</button></td></tr>");
});

Related

Google sheet to BigQuery by GAS

I am following the instruction below link with following code ( to make a app script to send data to big query from google sheet ):
function myFunction() {
var projectId = 'tcndata';
var datasetId = 'dec06';
var tableId = 'dec0601';
var fileId = '1Mb7tN3xshHt0gpsxHkt5Ifje4xAeu7N9Vn_YEQAdcoc';
var ss = SpreadsheetApp.openById(fileId);
var source = ss.getSheetByName("send");
var dataToCopy = source.getRange('A2:D5');
var values = dataToCopy.getValues();
var rowsCSV = values.join("\n");
var data = Utilities.newBlob(rowsCSV, 'application/octet-stream');
function convertValuesToRows(data) {
var rows = [];
var headers = ["Contract","Product","Dest","QTY"] ;
for (var i = 1, numColumns = data.length; i < numColumns; i++) {
var row = BigQuery.newTableDataInsertAllRequestRows();
row.json = data[i].reduce(function(obj, value, index) {
obj[headers[index]] = value;
return obj
}, {});
rows.push(row);
};
return rows;
}
function bigqueryInsertData(data, tableId) {
var insertAllRequest = BigQuery.newTableDataInsertAllRequest();
insertAllRequest.rows = convertValuesToRows(data);
var response = BigQuery.Tabledata.insertAll(insertAllRequest, projectId, datasetId, tableId);
if (response.insertErrors) {
Logger.log(response.insertErrors);
}
}
bigqueryInsertData(Utilities.parseCsv(data.getDataAsString()), tableId);
}
'''
The script also no error but the big query no record data.
Any one can help, to figure out what is reason ?
Thank you
You could see these options:
You should validate the headers’s name, as it needs to match the
column names of the table in BigQuery
You should validate converting the rowsCSV to a blob to use
getDataAsString
You should validate the name of the sheet is correct
ss.getSheetByName("send"); you can use ss.getSheets()[0];
You should validate the range has data source.getRange('A2:D5'). You
can use this format getRange(row, column, numRows, numColumns).Yo can see more documentation about getRange.
Another option is for you to load CSV data from Cloud Storage into a new BigQuery table by. You can see this example:
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("post_abbr", "STRING"),
],
skip_leading_rows=1,
# The source format defaults to CSV, so the line below is optional.
source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"
load_job = client.load_table_from_uri(
uri, table_id, job_config=job_config
) # Make an API request.
load_job.result() # Waits for the job to complete.
destination_table = client.get_table(table_id) # Make an API request.
print("Loaded {} rows.".format(destination_table.num_rows))
You can see this documentation.

XmlService.parse() not able to handle HTML tables

I am looking for help from this community regarding the below issue.
// I am searching my Gmail inbox for a specific email
function getWeeklyEmail() {
var emailFilter = 'newer_than:7d AND label:inbox AND "Report: Launchpad filter"';
var threads = GmailApp.search(emailFilter, 0, 5);
var messages=[];
threads.forEach(function(threads)
{
messages.push(threads.getMessages()[0]);
});
return messages;
}
// Trying to parse the HTML table contained within the email
function getParsedMsg() {
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var doc = XmlService.parse(msgbody);
var html = doc.getRootElement();
var tables = doc.getDescendants();
var templ = HtmlService.createTemplateFromFile('Messages1');
templ.tables = [];
return templ.evaluate();
}
The debugger crashes when I try to step over the XmlService.parse function. The msgbody of the email contains both text and HTML formatted table. I am getting the following error: TypeError: Cannot read property 'getBody' of undefined (line 19, file "Code")
If I remove the getParsedMsg function and instead just display the content of the email, I get the email body along with the element tags etc in html format.
Workaround
Hi ! The issue you are experiencing is due to (as you previously mentioned) XmlService only recognising canonical XML rather than HTML. One possible workaround to solve this issue is to search in the string you are obtaining with getBody() for your desired tags.
In your case your main issue is var doc = XmlService.parse(msgbody);. To solve it you could iterate through the whole string looking for the table tags you need using Javascript search method. Here is an example piece of code retrieving an email with a single table:
function getWeeklyEmail() {
var emailFilter = 'newer_than:7d AND label:inbox AND "Report: Launchpad filter"';
var threads = GmailApp.search(emailFilter, 0, 5);
var messages=[];
threads.forEach(function(threads)
{
messages.push(threads.getMessages()[0]);
});
return messages;
}
// Trying to parse the HTML table contained within the email
function getParsedMsg() {
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var indexOrigin = msgbody.search('<table');
var indexEnd = msgbody.search('</table');
// Get what is in between those indexes of the string.
// I am adding 8 as it indexEnd only gets the first index of </table
// i.e the one before <
var Table = msgbody.substring(indexOrigin,indexEnd+8);
Logger.log(Table);
}
If you are looking for more than one table in your message, you can change getParsedMsg to the following:
function getParsedMsg() {
// If you are not sure about how many you would be expecting, use an approximate number
var totalTables = 2;
var messages = getWeeklyEmail();
var msgbody = messages[0].getBody();
var indexOrigin = msgbody.indexOf('<table');
var indexEnd = msgbody.indexOf('</table');
var Table = []
for(i=0;i<totalTables;i++){
// go over each stable and store their strings in elements of an array
var start = msgbody.indexOf('<table', (indexOrigin + i))
var end = msgbody.indexOf('</table', (indexEnd + i))
Table.push(msgbody.substring(start,end+8));
}
Logger.log(Table);
}
This will let you store each table in an element of an array. If you want to use these you would just need to retrieve the elements of this array and use them accordingly (for exaple to use them as HTML tables.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Data Studio Connector getData() not running

I can't seem to get the getData() function to run on this connector I'm building. Data studio displays my Schema properly, however when I go to 'explore' the data, an error is thrown. Looking in the project executions, the 'getData' function never runs at all.
Data Studio has encountered a system error.
Sorry, we encountered an error and were unable to complete your request.
There's no debug errors shown, and I'm not sure how to continue debugging this.
Here is my code...
var cc = DataStudioApp.createCommunityConnector();
function isAdminUser(){
return true
}
function responseToRows(requestedFields, response){
return response.map(function(item) {
var row = [];
requestedFields.asArray().forEach(function(field){
var id = field.getId()
row.push(item[id])
});
console.log(row);
return { values: row };
});
}
function getAuthType() {
var response = { type: 'NONE' };
return response;
}
function getConfig(){
var json = UrlFetchApp.fetch("<api-url>");
var data = JSON.parse(json);
var config = cc.getConfig();
var tables = data.TableNames
var configElement = config
.newSelectSingle()
.setId('tables')
.setName("Choose your data source")
.setHelpText('Choose your data source');
for(i=0;i<tables.length;i++){
configElement
.addOption(config.newOptionBuilder().setLabel(tables[i]).setValue(tables[i]))
}
return config.build();
}
function getSchema(request){
var fields = cc.getFields();
var types = cc.FieldType;
var table = request.configParams.tables;
var data = UrlFetchApp.fetch("<api-url>"+"?name="+table);
var itemArray = JSON.parse(data);
var singleRow = itemArray["Items"][0];
var keys = Object.keys(singleRow)
for(i=0;i<keys.length;i++){
var nestedKeys = Object.keys(singleRow[keys[i]])
var propName = keys[i];
var dataType = nestedKeys[0]
if(dataType == "S"){
fields.newDimension()
.setId(propName)
.setName(propName)
.setType(types.TEXT)
}else if (dataType == "N"){
fields.newMetric()
.setId(propName)
.setName(propName)
.setType(types.NUMBER)
}
}
console.log(fields.build());
console.log('get schema')
return { schema: fields.build() };
}
function getData(request){
var fields = cc.getFields();
console.log(fields);
console.log('getdata running');
// TODO: Create Schema for requested field
var table = request.configParams.tables;
var requestedFieldIds = request.fields.map(function(field) {
return field.name
});
var requestedFields = fields.forIds(requestedFieldIds);
// TODO: Fetch and Parse data from API
var response = UrlFetchApp.fetch("<api-url>"+"?name="+table);
var parsedResponse = JSON.parse(response)
// TODO: Transform parsed data and filter for requested fields
var rows = responseToRows(requestedFields, parsedResponse)
return {
schema: requestedFields.build(),
rows: rows
}
}
To see debug traces, you could simply log it with console.log() and take a look at your logs in the Google Apps Scripts dashboard :
https://script.google.com/home/executions
I don't know if this is related to your problem, but in my case I was trying to use URL Parameters and getData(request) wouldn't run no matter what values I input - it ended up being that I had to create a production deployment and Publish > Deploy from Manifest and then create an actual published version (not just FROM HEAD).

Downloading a SQL database as a CSV instead of select rows

I used a function to download some selections from a Lists object as a CSV. I'd like to do the same for the entire datasource connected to the Lists object, but am unsure of how to scale this to work with, as an example, 100,000 rows. On the button to download the List data as a CSV I have:
var rows = widget.root.descendants.MainTableBody.children._values;
var csvdata = [];
csvdata.push([["Email"],["Last Login"],["Sku ID"],["Sku Name"],["Docs Added Recently"]]);
for (var i in rows) {
var t = [];
t.push(rows[i].children.EmailField.text);
t.push(rows[i].children.LastLoginField.text);
t.push(rows[i].children.SkuIdField.text);
t.push(rows[i].children.SkuNameField.text);
t.push(rows[i].children.DocsAddedField.text);
csvdata.push(t);
}
console.log(csvdata);
exportToCsv("LMexport",csvdata);
The export function is taken from this answer.I basically need the rows var to cover the entire table, but that's a lot of data.
The schema of the datasource in question:
and the calculation used:
Here's what the table looks like in the UI for reference:
Records retrieved from query are array of objects
You can use .reduce to convert them to csv
Snippet:
function exportCsv(){
var query = app.models.Employees.newQuery();
//query.filters.... use query used to populate the datasource
var records = query.run(); //[{},{},{}...]
var csv = records.reduce(function(str, rec){
var email = rec.Email;
var login = rec.LastLogin;
var skuId = rec.SkuId;
return str + '"' + [email,login,skuId].join('","') + '"\n'; // result: "email","login","skuId"\n
},'');
return csv;
}

Update Fusion Table From

Hi Experts,
I was trying to update fusion table from google spreadsheet. I tried with the codes available in Google's documentation pages and some others also but seems to be broken.
Treads that I read
Thread 1,,,
Thread 2
Code that I tried:
Code 1
Every time am getting
Request failed for https://www.google.com/accounts/Client Login returned code 403
Please tell me how to update fusion table from google spreadsheet.
Thanks a lot,
Anees Hameed.
After repeated trails I was able to create code which read data from fusion table and write to logger console:
function GetData()
{
var FUSION_URL = 'https://www.googleapis.com/fusiontables/v1/query?';
var APIkey = 'key=AIzaSyDdZu1nYAxFa2Wm5DTVYwlAejxET5Wb8XE';
var query = 'sql=SELECT * FROM 1k6N_-P-wZ2ZMh8B_2Doym5oLsAJXdXHAqZO_H0I WHERE Ratings=7';
var options =
{
method : 'get',
};
var response = UrlFetchApp.fetch(FUSION_URL+"+query+"&"+APIkey, options);
Logger.log(response );
}
And then I tried to write data to sql tables using 'post' method, but it is not working.
function WriteData()
{
var FUSION_URL = 'https://www.googleapis.com/fusiontables/v1/query?';
var APIkey = 'key=AIzaSyDdZu1nYAxFa2Wm5DTVYwlAejxET5Wb8XE';
var query = 'sql=DELETE FROM 1k6N_-P-wZ2ZMh8B_2Doym5oLsAJXdXHAqZO_H0I WHERE Ratings=7';
var options =
{
method : 'post',
payload : query,
headers : {
key: APIkey
},
};
var responce = UrlFetchApp.fetch(FUSION_URL, options);
Logger.log(responce);
}