Can googlechart query more than 1 spreadsheet at the same time? - javascript

because I tried query 3 sheets with 3 charts at the same time with 1 handle function ,it works but the result is wrong (it display the same chart in 3 div)
So I think I have to add handle function on each chart like
google.charts.load('current', {packages: ["geochart"]});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var query1 = new google.visualization.Query("https://docs.google.com/spreadsheets/d/14VouG7zZqHGB9CA6bxQx6CXX-TvOYkSqTmrN5DAj1Do/edit#gid=1175123524");
var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1RsugJPtz2EdHOLaiL0SvR9bh61H-vAgn9x1QBjIJ--c/edit?usp=sharing');
var query3 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1zPP22gUPLDqTrvASIU3OXpmeHL_9IBS2O4z75g-1BHY/edit?usp=sharing');
query1.send(handleQueryResponseTR1);
query2.send(handleQueryResponseTR2);
query3.send(handleQueryResponseTR3);
}
function handleQueryResponseTR1(response1) {
if (response1.isError()) {
alert('Error in query: ' + response1.getMessage() + ' ' + response1.getDetailedMessage());
return;
}
function handleQueryResponseTR2(response2) {
if (response2.isError()) {
alert('Error in query: ' + response2.getMessage() + ' ' + response2.getDetailedMessage());
return;
}
function handleQueryResponseTR3(response3) {
if (response3.isError()) {
alert('Error in query: ' + response3.getMessage() + ' ' + response3.getDetailedMessage());
return;
}
var data1 = response1.getDataTable();
var data2 = response2.getDataTable();
var data3 = response3.getDataTable();
but I got an error
handleQueryResponseTR2 is not defined
So I don't know if google chart can query more than 1 sheet in one page?

I don't believe googlechart queries have any problems with multiple queries at the same time, to answer the question.
Regarding your code:
Your handleQueryResponseTR2 function is a local function in handleQueryResponseTR1 but you try to call it from drawRegionsMap(). handleQueryResponseTR2() is not defined in that scope.
Move handleQueryResponseTR2() to upper scope to make it visible to the calling function.
In fact, move all your handleQueryResponse functions to the upper level.
var data1;
var data2;
var data3;
function handleQueryResponseTR1(response1) {
if (response1.isError()) {
alert('Error in query: ' + response1.getMessage() + ' ' + response1.getDetailedMessage());
return;
}
data1 = response1.getDataTable();
}
function handleQueryResponseTR2(response2) {
if (response2.isError()) {
alert('Error in query: ' + response2.getMessage() + ' ' + response2.getDetailedMessage());
return;
}
data2 = response2.getDataTable();
}
function handleQueryResponseTR3(response3) {
if (response3.isError()) {
alert('Error in query: ' + response3.getMessage() + ' ' + response3.getDetailedMessage());
return;
}
data3 = response3.getDataTable();
}
function drawRegionsMap() {
var query1 = new google.visualization.Query("https://docs.google.com/spreadsheets/d/14VouG7zZqHGB9CA6bxQx6CXX-TvOYkSqTmrN5DAj1Do/edit#gid=1175123524");
var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1RsugJPtz2EdHOLaiL0SvR9bh61H-vAgn9x1QBjIJ--c/edit?usp=sharing');
var query3 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1zPP22gUPLDqTrvASIU3OXpmeHL_9IBS2O4z75g-1BHY/edit?usp=sharing');
query1.send(handleQueryResponseTR1);
query2.send(handleQueryResponseTR2);
query3.send(handleQueryResponseTR3);
}
Please, note that the handleQueryResponse functions are called asynchronously when the responses are received.

Related

Retrieve data from firebase resulting in undefined

I was having this problem when trying to pull from firebase. My database structure as such:
I am trying to pull the merchantName and its relevant branches details:
function getAllMerchant(){
var query = firebase.database().ref('merchants');
return query.once('value').then(data => {
var result = [];
data.forEach(snapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
var branchName = merchantData.branches.branchName;
var branchAddress = merchantData.branches.branchAddress;
console.log('check ' + merchantName + ' ' + branchName + ' ' + branchAddress);
result.push({merchantName: merchantName, branchName: branchName, branchAddress: branchAddress});
});
return result;
});
resolve(result);
}
However, when I printed out the branchName and branchAddress, I am getting undefined. I managed to print out merchantName though. Any ideas?
Thanks!
You're not iterating over the branches of the merchant.
data.forEach(merchantSnapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
console.log('check ' + merchantName);
merchantSnapshot.child("branches").forEach(brancheSnapshot => {
var branchName = brancheSnapshot.val().branchName;
var branchAddress = brancheSnapshot.val.branchAddress;
console.log(' ' + branchName + ' ' + branchAddress);
});
});

async function inside a for loop

Hi i´m trying to convert sqlite database to NeDb, with this code:
const sqliteJSON = require('sqlite-json');
const Datastore = require('nedb')
const exporter = sqliteJSON('etecsa.db');
db = new Datastore('etecsa.nedb');
db.loadDatabase();
tables = ['fix','movil'];
tables.forEach(function(table) {
sql = 'select count(1) from ' + table;
exporter.json(sql, function (err, json) {
toNeDB(table, JSON.parse(json)[0]['count(1)'])
});
}, this);
var toNeDB = function(table, count) {
var inc = 10000;
console.log(table + ' => ' + count)
for (var i = 0; i < count + inc; i += inc) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ i + ' , ' + inc;
console.log(i)
exporter.json(sql, function(err, json) {
var data = JSON.parse(json);
db.insert(data, function (err, newDoc) {});
});
}
}
the problem is that the for loop its not working as I desire. I need to use it to change the sql pagination because the sqlite database is very huge and I can´t pass all the data on a single query.
UPDATE using async.map
const sqliteJSON = require('sqlite-json');
const Datastore = require('nedb')
var range = require("range");
var async = require("async");
const exporter = sqliteJSON('etecsa.db');
db = new Datastore('etecsa.nedb');
db.loadDatabase();
tables = ['fix','movil'];
tables.forEach(function(table) {
sql = 'select count(1) from ' + table;
exporter.json(sql, function (err, json) {
toNeDB(table, JSON.parse(json)[0]['count(1)'])
});
}, this);
var toNeDB = function(table, count, cb) {
var inc = 10000;
var pagination = range.range(1,count+inc,inc)
async.map(pagination, function (page, cb){
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ page + ' , ' + inc;
console.log(page, table, inc);
exporter.json(sql, function(err, json) {
var data = JSON.parse(json);
console.log(data[0])
db.insert(data, function (err, newDoc) {});
});
}.bind({ table: table, inc: inc }), function(err,results){
})
}
and the output:
1 'fix' 10000
10001 'fix' 10000
....
1150001 'fix' 10000
1 'movil' 10000
10001 'movil' 10000
...
3730001 'movil' 10000
{ number: '8775031',
name: 'UNION ELECTRICA',
address: 'S ALLENDE #666 OQUENDO SOLEDAD',
province: 7 }
{ number: '8734454',
name: 'EMP ESTB ESP Y SERVICIOS',
address: 'ESAPDA #256 CONCORDIA S LAZARO',
province: 7 }
If you need to know when each action occurred, you should put the console.log inside the callback.
Something like that:
var toNeDB = function(table, count) {
var inc = 10000;
console.log(table + ' => ' + count)
for (var i = 0; i < count + inc; i += inc) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ i + ' , ' + inc;
exporter.json(sql, (function(i) {
return function(err, json) {
console.log(i)
var data = JSON.parse(json);
db.insert(data, function (err, newDoc) {});
}
})(i));
}
}
You could use recursion instead of a loop, that way you would be sure the next iteration won't execute until the first is done.
var proc = function (i, count, table) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC
LIMIT ' + i + ' , ' + inc'
console.log(i)
exporter.json(sql, function (err, json) {
var data = JSON.parse(json)
db.insert(data, function (err, newDoc) {
if (i < count) {
i += inc
proc(i, count, table)
}
})
})
}
var toNeDB = function (table, count) {
var inc = 10000
console.log(table + ' => ' + count)
proc(0, count, table)
}
let me know if that works

ContextMenuItem context function is not executing

I want my context menu item to be visible only if the clicked node is a link i.e. and href is either a magnet link or a torrent link. But item is visible for all the links because context function is not executing, can anybody help why context function is not executing?
Here is the code:
exports.main = function() {
var cm = require("sdk/context-menu");
var contextCode = ' self.on("context", function (node) { '+
' while(node.nodeName!="A") { node = node.parentNode; } '+
' var pat_magnet = /^magnet:/i; ' +
' var pat_torrent = /.torrent$/i; ' +
' if(pat_torrent.test(node.href) || pat_magnet.test(node.href)) { return true; } '+
' else { return false; } '+
' }); ';
var clickCode = ' self.on("click", function(node,data){ '+
' while(node.nodeName!="A") { node = node.parentNode; } '+
' var pat_hash = /[0-9abcdef]{32,40}/i; ' +
' var result = node.href.match(pat_hash); '+
' var hash = "" '
' if(result != null) { hash=result[0]; } '+
' var xhr = new XMLHttpRequest(); '+
' if(hash != "") { '+
' var apiCall = "https://www.furk.net/api/dl/add?api_key=*************&info_hash="+hash; '+
' } '+
' else{ '+
' var apiCall = "https://www.furk.net/api/dl/add?api_key=*************&url="+encodeURI(node.href); '+
' } '+
' xhr.open("GET",apiCall,true); '+
' xhr.onreadystatechange = function(){ if(xhr.readyState = 4) { if (xhr.response.status = "ok") { alert("Torrent added to Furk."); } else { alert("Torrent could not be added to Furk."); } } } '+
' xhr.send(null); '+
' });';
cm.Item({
label: "Add to Furk",
context: cm.SelectorContext("a[href]"),
contentScript: contextCode + clickCode
});
};
Please always post self-containied examples that can be directly tried in the future.
Now back to your problem: The content script actually has a syntax error.
The following line:
' var pat_torrent = /.torrent$/i ' +
lacks a semicolon, and should be:
' var pat_torrent = /.torrent$/i; ' +
The reason automatic semicolon insertion (ASI) does not work here is: The "code" is actually a string that has no newlines in it whatsoever. If there were newlines, then ASI would have worked.
Anway, another reason not to have complex content script inline. Have a look at contentScriptFile.
This error is actually logged, but the presentation sucks. In the Browser Console:
[20:57:51.707] [object Error] (expandable)
In terminal:
console.error: context-magnet:
Message: SyntaxError: missing ; before statement
Here is a fixed, reproducible sample:
var cm = require("sdk/context-menu");
var contextCode = ' self.on("context", function (node) { '+
' while(node.nodeName!="A") { node = node.parentNode; } '+
' var pat_magnet = /^magnet:/i; ' +
' var pat_torrent = /.torrent$/i; ' +
' if(pat_torrent.test(node.href) || pat_magnet.test(node.href)) { return true; } '+
' else { return false; } '+
' }); ';
cm.Item({
label: "magnet test",
context: cm.SelectorContext("a[href]"),
contentScript: contextCode
});
Edit ' var hash = "" ' has the same problem, and there are might be other such errors that I missed skimming this new code.
As I already said, please use contentScriptFile and not contentScript for long-ish scripts.
Another edit
Here is a builder using contentScriptFile, where I also fixed a couple of other errors, the most important of which are:
Use permissions so that the XHR will work.
Correctly set up the XHR to use responseType and overrideMimeType().
Use onload/onerror instead of onreadystatechange.

use variable in next function (Google Sheet Queries)

I have a function that defines a variable and I would like to use the value in the next function. Each function has it's own query. I think I either need to combine the queries into one function, of which I don't know how to do or setup a global variable with the first query so it can be referenced in the second function.
I've been reading that global variables and they say they're not a good practice. Can someone help me with how to reuse a variable from one function to the next or combine the two queries else another JS method I should consider?
google.setOnLoadCallback(queryValue1);
function queryValue1 () {
var query2 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=A2:A17&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=9');
query2.send(function (response) {
if (response.isError()) {
alert('Error in query2: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data1 = response.getDataTable();
// fetch the data from range cell (row, column) into the span "bx"
for (var z = 0; z <= 15; z++) {
document.getElementById('a' + (z + 22)).innerHTML = data1.getValue(z, 0);
}
});
}
google.setOnLoadCallback(queryValue3);
function queryValue3 () {
var query3 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=B2:B17&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=10');
query3.send(function (response) {
if (response.isError()) {
alert('Error in query3: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data3 = response.getDataTable();
var m1 = data3.getValue(0, 0);
var red22 = "<span style='color:#ff0000' ";
var yellow22 = "<span style='color:#FF9900' ";
var green22 = "<span style='color:#009900' ";
if (m1 <= 70)
{
m1 = red22;
}
else if (71 === m1 && m1 <= 89)
{
m1 = yellow22;
}
else if (m1 >=90)
{
m1 = green22;
}
console.log ("m1= " + m1)
var m1 = (m1 + a22 + "</span>");
console.log ("m1= " + m1)
});
}
Thank you...
As it is, your making two essentially parallel asynchronous network calls (the Query.send() calls). There's no telling which one is going to return first (or if one will fail), so relying in one callback on data from the other is unreliable and a bad idea, no matter whether you use a global variable or something else.
I think you probably just want to chain the two asynchronous calls, so try something of this shape:
google.setOnLoadCallback(queryValue1);
function queryValue1 () {
var query2 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=A2:A17&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=9');
query2.send(function (response) {
if (response.isError()) {
alert('Error in query2: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
// Get the data you want to pass to query3 here
// var query3Data = ...
queryValue3(query3Data);
});
}
function queryValue3(passedData) {
var query3 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=B2:B17&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=10');
query3.send(function (response) {
if (response.isError()) {
alert('Error in query3: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
// Do something with passedData here
// ...
});
}
So don't start the second query until the first one returned.

How to debug page loading errors in phantomJs

Is there a way to debug page.open method of phantomjs ? My application loads some files saved locally but unfortunately the only info one can get when opening the page is if it was loaded successfully or not. What more interesting the very same page loads properly when opened in the browser.
Here's my code :
var system = require('system'),
page = require('webpage').create();
var openPage = function () {
var url = 'http:\\localhost:53794/file.html';
page.open(url, function (status) {
if (status !== 'success') {
console.log("FAIL:" + url);
phantom.exit(2);
}
var date = new Date().getTime();
var outputFilename = outputPath + 'print-' + date + '.png';
setTimeout(function () {
page.render(outputFilename);
outputArray.push(outputFilename);
setTimeout(function () {
phantom.exit(1);
}, 1);
}, 1);
});
}
openPage();
via: http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/
After creating the page variable, but before calling page.open() add the following code:
page.onResourceError = function(resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};
Now you can print out the reason for a problem in your page.open() callback, e.g.:
var page = require('webpage').create();
page.onResourceError = function(resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};
page.open(
"http://www.nosuchdomain/",
function (status) {
if ( status !== 'success' ) {
console.log(
"Error opening url \"" + page.reason_url
+ "\": " + page.reason
);
phantom.exit( 1 );
} else {
console.log( "Successful page open!" );
phantom.exit( 0 );
}
}
);
Debugging Function
If you read further down the blog, he has some more suggested event handlers to add. I adapted them into a function that you can use to inject the event handlers into your page object (instead of having them defined in your main code)
// this method injects some debugging event handlers
// into a PhantomJS page object.
// usage:
// var page = require('webpage').create();
// var system = require('system');
// addDebugEvents(page,system);
function addDebugEvents(page, system) {
page.onResourceError = function (resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};
page.onResourceRequested = function (request) {
system.stderr.writeLine('= onResourceRequested()');
system.stderr.writeLine(' request: ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
system.stderr.writeLine('= onResourceReceived()');
system.stderr.writeLine(' id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response));
};
page.onLoadStarted = function () {
system.stderr.writeLine('= onLoadStarted()');
var currentUrl = page.evaluate(function () {
return window.location.href;
});
system.stderr.writeLine(' leaving url: ' + currentUrl);
};
page.onLoadFinished = function (status) {
system.stderr.writeLine('= onLoadFinished()');
system.stderr.writeLine(' status: ' + status);
};
page.onNavigationRequested = function (url, type, willNavigate, main) {
system.stderr.writeLine('= onNavigationRequested');
system.stderr.writeLine(' destination_url: ' + url);
system.stderr.writeLine(' type (cause): ' + type);
system.stderr.writeLine(' will navigate: ' + willNavigate);
system.stderr.writeLine(' from page\'s main frame: ' + main);
};
page.onResourceError = function (resourceError) {
system.stderr.writeLine('= onResourceError()');
system.stderr.writeLine(' - unable to load url: "' + resourceError.url + '"');
system.stderr.writeLine(' - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString);
};
page.onError = function (msg, trace) {
system.stderr.writeLine('= onError()');
var msgStack = [' ERROR: ' + msg];
if (trace) {
msgStack.push(' TRACE:');
trace.forEach(function (t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
});
}
system.stderr.writeLine(msgStack.join('\n'));
};
}
You should change the URL
from
http:\\localhost:53794/file.html
to
http://localhost:53794/file.html

Categories