Excel - remove first blank row - javascript

I have a really big excel file with first row blank, second row with header and rest rows containing data.
I need to select only part of data from that file.
Because it should be done on client side in javascript I decided to use AlaSql library (alasql.org), where I can precisely select data I need with sql language.
Fe: first I count number of rows than select some of data (with headers) using range:
var sql = "SELECT value COUNT(*) FROM FILE(?,{headers:true})"; //count rows
alasql(sql,[event],function(numberofrows){
var sql2 = "select column1, column2 from FILE(?, headers:true,range:'A2:Z"+numberofrowss"'})";
//I need only two columns I can use here where/having/limit etc. conditions
alasql(sql2,[event],function(res){
... // other steps
}}
Is there more effective way to do the job (not counting rows first)?
Maybe it can be used something similar to range defined like 'A2:ZZ'?
Using range A2:A10000000 consumes all memory...
Any suggestions? Other library? Maybe there is a way to remove blank row first?

Are you tried to import data without range?
I never seen this library before (now thank you) and just used sample file with data like you described and examples from lib docs and got correct results:
alasql('SELECT header1, header2 FROM XLS("https://dl.dropboxusercontent.com/u/802266/a.xls",{headers:true})',[],function(data){
console.log(data);
});
result:
0: Object
header1: "aaa1"
header2: "aaa2"
1: Object
header1: "sss1"
header2: "sss2"
2: Object
header1: "xxx1"
header2: "xxx2"
`Working example: http://jsfiddle.net/xdfyxupL/
Also if you still need rows count, you can use ROWNUM

Related

How to add multiple values in one query to a PostgreSQL array?

So if I have a Javascript array of strings, how do I append each element to a row in the Postgres text[] column? This is what I'm currently using, but it's not yielding the desired result (which is just making the text array in the column of {"1","2","3"}.
let addNumbers = 'UPDATE numTable SET column1 = array_append(columnName, ($1)) WHERE uid = ($2)'
let x = ["1","2","3"]
I'm querying to the db with
query(addNumbers, [x, uid]);
I've tried using array_append but this results in the column getting a value of
{"{\"1\",\"2\",\"3\"}"}
I'd prefer not to iterate and append each one individually because this'll obviously increase the number of database calls--any help is much appreciated!

How can I INSERT new values and values from an already existing table?

I'm currently learning MySQL and have learned the INSERTO INTO statement and the INSERT INTO SELECT statement.
The INSERT INTO statement works like this:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
While the INSERT INTO SELECT works like this:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
What I'm trying to do is add new values while also adding values that I already have stored in another table.
con.query("INSERT INTO vehicles (vehicleType, vehicleModel, vehicleOwner, vehicleSpawnX, vehicleSpawnY, vehicleSpawnZ)")
From the query above, I already have the vehicleOwner value stored in another table, while the other ones I've just gotten.
How can I do this? Add a VALUES statement before SELECT?
I'm using SQL Workbench 8.0 and JavaScript. Also, all the values are NOT NULL, so I can't make two different queries, unless on the first one I add temporary values that I'll update on the second one.
What I want to replace:
vehicleType -> "players"
vehicleModel -> vehicleCreate.model
vehicleOwner -> playerID FROM players table
vehicleSpawnX -> pos.x
vehicleSpawnY -> pos.y
vehicleSpawnZ -> pos.z
Thanks!
It's not possible... But you can select data and store that on variables, then you will store it in another table
You would construct a query. Your data model is a bit hard to follow since you give no examples of the data or of what you really want to do.
So let me give a simpler example. Say, you have a table with two columns for two players and you want to put the ids into a table -- but using their names. The query would look like:
insert into pairs (playerId1, playerId2)
select p1.playerId, p2.playerId
from players p1 join
players p2
on p1.name = ? and p2.name = ?;
The ? are for parameter placeholders. I assume you know to aways use parameters when passing values into a query.

Dynamically adding / removing columns

I use datatables 1.10 for a while now, and I have come to a point in my development where I have a table in my db that stores a json object. Furthermore that table also contains an attribute called groupName. As the name suggests, the entries of that table are "grouped" by that attribute, and in that group, the json object has the same attributes.
Here an example of how my database table looks like:
+---------------------------------------------------------------------------+
|id|groupname |json |
+---------------------------------------------------------------------------+
|01|group1 |{"key1":"value1","key2":"value2"} |
|02|group1 |{"key1":"foo","key2":"bar"} |
|03|group2 |{"kex1":"foo1","keyab":"bar2","key1":"foo"}|
+---------------------------------------------------------------------------+
My page that contains the datatabase, and also has a dropdown menu populated with the group names which filters the the datatable for the particular group. The jsonObject of that filtered group is being displayed as a raw json string.
What I'm trying to do now is to parse my jsonObject, get the keys and populate them in the datatable as columns. The values of the jsonObject are displayed in the columns accordingly.
Where or how do I reinitialize the datatable, providing it with the proper column names?
Can you please share some example of your JSON object.
Thanks!
Any ways what you can do is to to take the keys of the json object and put them into array then using loop you can make a string of columns. Then you can provide an id to of the table and append the string into the table.
let keys = Object.keys(YOUR_JSON_OBJECT);
let text = "";
for(let i =0;i<keys.length;i++){
text+= ["<tr>","<td>",YOUR_JSON_OBJECT.its_key,"</td>","</tr>"].join("");
}
$('table thead').html(text);
See if this works!

Pivot JSON Object With JQuery

I have a data set that I need to pivot in order to represent in a grid on screen.
I wanted to do this in the database, but the issue is the number of columns that can be returned by the query is dynamic and fluctuates based on the search criteria.
The dataset from the query looks like this:
And I need to represent it on the screen like this:
Essentially each sample ID needs to be its own line, with the test method ID's and results on the line with it.
My plan is to do the pivot on the front end, using a JQuery library if possible.
I wanted to use this https://www.npmjs.com/package/json-to-pivot-json but i do not belive it will work because of their limitation: Only supports single column, row and value for pivoting for now.
Does anyone have any libraries to recommend, or examples a pivot being done with a large dataset like I have?
All the examples I have found are very basic doing only 3 columns.
I was unable to find any library that would do a pivot for me in JQuery.
So, I created my own pivot function in Java to build a distinct list of the data, and do the pivot in a single pass through.
public List<JSONObject> getDistinctResultsAndPivot(List<LabInquiryDetail> results){
List<JSONObject> pivotData = new ArrayList<>();
String prevSampeId = "";
for (LabInquiryDetail result : results) {
/*
* If sample IDs do not match, it is either the first record in the result set,
* or a new record we have not looked at yet.
*/
if(!Objects.equals(prevSampeId, result.getSample_id())){
JSONObject row = new JSONObject();
row.put("equipmentId", result.getEquipment_id());
row.put("lot", result.getLot());
row.put("wrkOrdId", result.getWrk_ord_i());
row.put("opcodeDesc", result.getOpcode_desc());
row.put("sampleId", result.getSample_id());
row.put("sampleStatus", result.getSample_status());
row.put("sCreatedDate", result.getS_created_date());
row.put("sModifiedDate", result.getS_modified_date());
row.put("sCollectionDate", result.getS_collection_date());
row.put(result.getTm_id(), result.getResult());
pivotData.add(row);
}
/*
* Because results are already sorted by sample ID in ascending order,
* if sample IDs match, we can get the last row in our pivot list, as it is the row we need to update
* with new test method record.
*/
else if (Objects.equals(prevSampeId, result.getSample_id())){
JSONObject row = pivotData.get(pivotData.size() - 1);
row.put(result.getTm_id(), result.getResult());
}
prevSampeId = result.getSample_id();
}
return pivotData;
}

jQuery DataTables 1.10: Which column is it sorted on right now?

Using DataTables 1.10,
I have a DataTable with a default sort and the user can resort by some of the other columns.
How do I detect the column by which the table is currently sorted?
Some context which may not be relevant to answering the question: What I'm really trying to do is "export" the table to a non-interactive HTML table. This DataTable is generated programmatically and then turned into a DataTable, so after some searching for export options it looks like it will be easier to essentially regenerate the original table than to actually export. But I need the regenerated table to have the rows in the same order as the current sort.
The current sort state sortInfo can be retrieved like this:
var apiObject = $("#myPlainTable).DataTable( dtOptions );
// ...
var sortInfo = apiObject.settings().order()
More specifically, the column and direction are encoded like this:
var sortCol = sortInfo[0][0]; // counting from left, starting with 0
var sortDir = sortInfo[0][1]; // either "asc" or "desc"
Caveats:
The sortInfo object will have the above format after the user changes the sorting; if you specify the initial sort by setting dtOptions.order using a different format, then the sortInfo object will have the original value you specified until the user changes the sorting. (For example, DataTables will accept [1,'asc'] in addition to the above [[1,'asc']]; I didn't test what happens if you pass a value DataTables can't use.)
This describes the default case where you sort by one column only, not using the multi-column sort feature.
When you are using dataTables 1.10.x already, why not use the API? By that it is easy :
table.rows().data()
returns an array of arrays containing the current content of the table, i.e the rows as they are currently sorted. So if you want to export or clone the content of a dataTable to a static table, you can actually do it very simple :
$("#clone").click(function() {
var cloneTable = '';
table.rows().data().each(function(row) {
cloneTable += '<tr><td>' + row.join('</td><td>') + '</td></tr>';
})
$('#cloneTable tbody').html(cloneTable);
})
demo -> http://jsfiddle.net/zuxm2e68/
If sending to the server you check the
order[i][column]
order[i][dir]
for column and direction. See here
Since you are using 1.10, you can use the following:
var table = $('.myTable').dataTable({ /* Your options */ });
var sortArray = table.api().settings().aaSorting; // gives array
If you are using API already via $('.myTable').DataTable({...}), you can omit the .api().

Categories