unable to read empty cells reading excel file using js-xlsx - excel

I have an excel file I am reading using js-xlsx. The code is working fine except where the cells are empty. These cells are ignored. How do I get these cells also when creating my JSON object?
I went through some of the question on SO as well as some other forums for the same problem but nothing satisfactory.
Any help would be welcome. My code is:
reader.addEventListener('load', function(){
var data = this.result;
var wb = XLSX.read(data, {type: 'binary', sheetStubs:true});
// console.log(headers);
wb.SheetNames.forEach(function(sheetName){
//pulling out column headers for tablecreation
var headers = get_header_row(wb.Sheets[sheetName]);
createTableInDB(headers);
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_json(wb.Sheets[sheetName]);
//console.log(XL_row_object);
for(var i=0; i<XL_row_object.length; i++){
var json_object = XL_row_object[i];
if(json_object !== null){
var dataobject = {
"tablename": tname,
"dbname": dbname,
"info": json_object,
"uname": uname
}
dataobject = $.toJSON(dataobject);
$.ajax({
type: "POST",
url: "insertIntoTable.php",
async: false,
data:"pInsertData=" + dataobject,
success: function(msg){
console.log(msg);
}
});
//console.log(json_object);
}
}
});
});
reader.readAsBinaryString(document.querySelector('input').files[0]);
The file is uploaded through an input in HTML.
Thanks in Advance

Just pass default value in sheet_to_json method:
var jsonObj = XLS.utils.sheet_to_json(data.Sheets[data.SheetNames[0]], {
header: 0,
defval: ""
});

The library has an option for that. In your code below:
...
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_json(wb.Sheets[sheetName]);
//console.log(XL_row_object);
...
You should provide the following option, to the options argument {defval: null} as follows:
...
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_json(wb.Sheets[sheetName], {defval: null});
//console.log(XL_row_object);
...
Then, it should work.

Solution 1 .Condition "if(h===undefined)continue;" in "xlsx.core.min.js" comment it out.
or do it properly...
Solution 2 . By passing Condition extra param while running this XLSX.utils.sheet_to_json(wb.Sheets[name] , {blankCell : false}). add a condition on line no. 19150 "if(defval === undefined && blankCell) continue;" in file xlsx.js etc..

Related

Firebase - Cloud Functions: enumerate [Object Object]

I would like to enumerate the members object from the below structure.
I do not have prior experience in node.js, I have tried the following code and it doesn't seem to work.
exports.sendVaultUnlockedPush = functions.database.ref('/Vaults/{id}/action').onUpdate(event => {
let status = event.data.val();
if (status == 'lock') {
var db = admin.database();
var vaultRef = db.ref("/Vaults/" + event.params.id);
vaultRef.once('value').then(function(snapshot) {
var hasMembers = snapshot.child('members');
if (hasMembers) {
var memberRef = db.ref("Vaults/" + event.params.id + "/members");
memberRef.once('value').then(function(memberSnapshot) {
memberSnapshot.forEach(function(element) {
console(element.key + " - " + element.val());
}, this);
});
}
});
}
})
It throws the following error:
Error: Firebase.DataSnapshot.forEach failed: Was called with 2 arguments. Expects no more than 1.
at Error (native)
at z (/user_code/node_modules/firebase-admin/lib/database/database.js:42:1666)
at T.forEach (/user_code/node_modules/firebase-admin/lib/database/database.js:114:179)
at /user_code/index.js:34:36
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I would just like it to return the value of 'members' node. And I have no clue to do the same.
Any help is much appreciated in getting this sorted. Thanks!
According to the docs forEach takes only one parameter:
This should work:
var values;
memberSnapshot.forEach(function(element) {
console(element.key);
values = element.val();
for (var val in values) {
if (values.hasOwnProperty(val)) {
console(val, values[val]);
}
}
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

URLVariables not working in Haxe project

how are you doing? I hope fine.
So I'm trying to send an urlRequest and I can't pass the parameters by url so I'm trying to use the URLVariable, but no matter what I try my php always get null.
var request:URLRequest = new URLRequest(SITE_DOMAIN + "/check_login.php");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.login = emailInput.text;
variables.password = senhaInput.text;
variables.gotogame = "BURACO";
Reflect.setField(variables, "login", emailInput.text);
Reflect.setField(variables, "password", senhaInput.text);
Reflect.setField(variables, "gotogame", "BURACO");
request.data = variables;
request.method = URLRequestMethod.POST;
openfl.Lib.getURL(request);
As you guys can see I'm trying to set the variables in two ways but neither of they are working and I kind of don't know what to do anymore, please help.
Ive used this without problems:
var request:Http = new Http(SERVER + "actions/layout-builder?random=" + Math.random());
request.addParameter("action", "retrieve");
request.addParameter("layoutId", layoutId);
request.onError = function(msg) {
showSimplePopup("Problem loading layout:\n\n" + msg);
}
request.onStatus = function(status:Int) {
}
request.onData = function(response) {
response = StringTools.replace(response, "\r\n", "\n");
layoutCode.text = response;
}
request.request(false);

Cheerio scraping returning only two rows

So I tested my scraping on a static HTML file before adding it to my Node app.
The problem is that it's not returning all the rows.
On the site:
$('#sport tr').length
//Returns 13
In Cheerio:
$('#sport tr').length
//Returns 2
I'm stumped, here is the code I'm using. I've contained the URL as proof, so you can visit it yourself if you wish.
I'm suspecting it's something to do with var $ = cheerio.load(html); however I'm not experienced in Cheerio to say outright that's the problem.
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
var url = 'http://www.olbg.com/football.php';
var json = [];
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
console.log($('#sport tr').length);
var headers = [];
$('#sport tr th').each(function(i, th) {
var text = $(th).text();
if (text.trim() !== "") {
headers[i] = text.replace(/[\t\n\r\s]/mgi, '');
}
});
$('#sport tr').each(function(i, tr) {
// skip if header
if (!$(tr).is('th')) {
var temp = {};
temp["Event"] = $(tr).find('td').eq(0).text().trim();
temp["TopSelection"] = $(tr).find('td').eq(1).text().trim();
temp["BookieOdds"] = $(tr).find('td').eq(2).text().trim();
temp["OLBGRating"] = $(tr).find('td').eq(3).find('img').length;
if (temp["Event"] !== "" || temp["TopSelection"] !== ""){
json.push(temp);
}
}
});
}
// To write to the system we will use the built in 'fs' library.
// In this example we will pass 3 parameters to the writeFile function
// Parameter 1 : output.json - this is what the created filename will be called
// Parameter 2 : JSON.stringify(json, null, 4) - the data to write, here we do an extra step by calling JSON.stringify to make our JSON easier to read
// Parameter 3 : callback function - a callback function to let us know the status of our function
fs.writeFile('output.json', JSON.stringify(json), function(err){
console.log('File successfully written!');
})
// Finally, we'll just send out a message to the browser reminding you that this app does not have a UI.
res.send(json);
});
});
app.listen("8081");
console.log("Magic happens on port 8081");
exports = module.exports = app;
The reason that you're not getting the expected result is because the (table) html on that page is mangled. If you look at the second <td> in the second <tr> of the table#sport, you'll see an "extra" </td>. This causes the <td> that the table#sport is inside to close (and an implicit closing of table#sport) on some parsers because that is the closest open <td>. So that is why the parser reports only 2 <tr>s instead of 13. The other <tr>s you're expecting are now outside of table#sport.
Probably your best bet is to pass the html through an HTML tidying program/script (e.g. this one with the clean option enabled) first before passing it to cheerio. After that, your selector should return the elements you're probably expecting.

Call function from Dust template

I'm developing a simple web site where I need to retrieve a list of objects from a database. I wanted to try nodejs so, after days of reading and tests, I finally decided to use this configuration:
Server technology: Nodejs + Express
Template engine: Dust
Database/Data source: Parse
I wired all these stuff and it seems working well, but I have now the first problem: I need to call a function from a Dust template, this is the code:
{>layout/}
{<content}
<ul>
{#photos}
<li>{photo.get("name")}{~n}</li>
{/photos}
</ul>
{/content}
but it doesn't work because it prints out {photo.get("name")} (literally) instead of printing the name of each photo. The query with Parse works correctly as I can see the loaded objects via console.log().
I'm new both with nodejs and dust so I'm not sure the problem is related only to dust. Any idea?
I have no any other solution except creation of a helper:
var dust = require('dustjs-linkedin');
dust.helpers.exec = function(chunk, context, bodies, params) {
var args = JSON.parse(params.args.replace(/'/g, '"'));
var object = context.stack.head;
params.func.split('.').some(function(property) {
if (typeof(object[property]) === "function") {
var result = object[property].apply(object, args);
chunk.write(result);
return true;
} else {
object = object[property];
return false;
}
})
return chunk;
};
Suppose we have following data:
app.get('/dust-test', function(req, res) {
function Photo(name) {
var props = {'name': name};
this.get = function(prop) {
return props[prop];
}
}
var photos = ['foo', 'bar', 'nanana'].map(function(name) {
return new Photo(name);
})
res.render("dust-test", {
photo: new Photo('me'),
photos: photos
});
});
Usage:
<li>{#exec func="photo.get" args="['name']" /}</li>
{#photo}
<li>{#exec func="get" args="['name']" /}</li>
{/photo}
<ul>
{#photos}
<li>{#exec func="get" args="['name']" /}{~n}</li>
{/photos}
</ul>
Where args - is an array of arguments in json format (single quotes are used)

Node request throwing: Error: Invalid URI "www.urlworksinbrowser.com" or options.uri is a required argument

I'm using Node v0.10.11 on Ubuntu 12.04. I can't figure out what I'm missing to make streams of URLs work with the request module.
This program is trying to go to a mailing list site, find the download links for each month, then download the pages for each month.
Mikael's readme says "The first argument can be either an url or an options object. The only required option is URI, all others are optional.
uri || url - fully qualified uri or a parsed url object from url.parse()"
If I call url.parse(www.targeturl.com), I get
Error: options.uri is a required argument
If I don't use url.parse, I get
Error: Invalid URI "www.freelists.org/archive/si-list/06-2013"
(this link works perfectly fine in my browsers)
I've cut the code down to 42 lines. Any advice welcome
var request = require('request'),
url = require('url'),
stream = require('stream'),
cheerio = require('cheerio'), // a reduced jQuery style DOM library
Transform = require('stream').Transform
var DomStripStream = function(target) {
this.target = target;
stream.Transform.call(this,{objectMode: true});
}
DomStripStream.prototype = Object.create(
Transform.prototype, {constructor: {value: DomStripStream}}
)
DomStripStream.prototype.write = function () {
this._transform.apply(this, arguments);
};
DomStripStream.prototype.end = function () {
this._transform.apply(this, arguments);
this.emit("end");
};
DomStripStream.prototype._transform = function(chunk, encoding, callback) {
chunk = chunk ? chunk.toString() : "";
$ = cheerio.load(chunk);
domLinks = $(this.target);
$(domLinks).each(function (i, link) {
currLink = 'www.freelists.org' + $(link).attr('href')
// currLink = url.parse(currLink)
request(currLink, function (error, response, body) {
console.log(error);
})
});
}
var fs = require("fs"),
output = fs.createWriteStream("out.txt"),
mainPage = new DomStripStream('td a')
request('http://www.freelists.org/archive/si-list').
pipe(mainPage).
pipe(output);
add http:// or https:// in the url

Resources