Sunday, September 28, 2014

Generate table out of CSV text



The goal here is to have Csv texts be represented in Html tables.
Csv to Table


Javascript
function WriteCsvToTable(csvText, TablePane) {

    /*Variable Declaration*/
    var Rows = csvText.split("\n"), Html = [];

    /*Loop that makes the rows
      Each row is split with the mark of the comma.
      Then Joined to make the row
     */
    for (var ctr = 0; ctr < Rows.length; ctr++)
        Html.push("<tr><td>" + Rows[ctr].split(",").join("</td><td>") + "</td></tr>");

    Html = "<table>" + Html.join("") + "</table>";
    $(TablePane).html(Html);
}
/*Source http://jsfiddle.net/frvQ2/*/


The code above is the Javascript code that occurs during the click event. It is important to know that the Javascript code above requires Jquery. It is assumed that the “csvText” parameter matches the CSV format. The next parameter is a string to be queried with jquery funtions.  It is assumed both parameters matches the format required of them.

The first line executed in the functions is Variable declaration. The first variable is “Rows” which is an array generated from splitting the lines of the csv text entered. The next variable starts as an empty array where table related html elements are to be added.

The loop performs a one liner of code that adds content to the Html array. Each element of the array contains multiple html elements (this naming convention can be confusing). Each array element pushed starts with the "
"
_tags and ends with "
" closing tags. What’s between them is a result of an array and string javascript functions. The current element in the Row array represents the line in the csv text. The current line is split with its commas (,). Each of those commas are replaced with tags. Henceforth, those texts before commas and the line break are now in a table cell.

The Html array resulting from the loop is turned in to string with the table opening and closing tags between the appended string values from the Html array. This generates the final table code to be the content of the pane specified in the TablePane parameter.
HTML
<div class="row">
    <h1>Enter Your CSV Here:</h1>
    <textarea id="csv" class="text">
        Id,Product Name,Price
        1,Sailor Hat,$5
        2,Old Rod,$50
        3,Waffle Maker,$120</textarea>
</div>
<div class="row">
    <a href="#TablePane" onclick="WriteCsvToTable($('#csv').val(), '#TablePane')" class="btn  btn-primary">Generate Table</a>
   
</div>
<div class="row" id="TablePane">

</div>


The HTML code above contains a textbox where a user can enter CSV texts. A button that will call the function that generates a table representing the Csv specified upon click. There is also the panel where the table will be shown.

CSS
#TablePane tr:nth-child(odd) td{
            background:#beeab4;
        }
        #TablePane td {
            border : thin black solid;
            width:1%;
        }
CSS can also be applied for the table’s looks.

The link below has the results of the script with a few UI related modifications. It's a page from an Microsoft Azure website using the free tier. Constant up time is not a guarantee.

Sunday, September 21, 2014

Comma Separated Values (CSV)

Comma Separated Values (CSV) file format is used to present tabular data in a plain text format.  The first line is like a header column informing the field/column names in each record seen on the succeeding rows. Each line after the first in a csv text is a record that is separated from other records with line breaks. Those records have fields or field values separated by a comma (,). The format is quite popular as a means of transferring tabular data between different applications.

CSV example
Id,Product Name,Price
1,Sailor Hat,$5
2,Old Rod,$50
3,Waffle Maker,$120

Many programming languages have ways to parse the format. Java has several libraries for CSV including openscv. Meanwhile, Microsoft's dot Net framework (.Net) has Csv Reader and a built in parser class that can be set for a csv format. PHP even has a function built in for csv fgetcsv().  Technically, any programming language that has I/O support can read and write csv files. It's just plain text and not binaries.

There are other formats for representing data in plain text including JSON and XML. However, it seems that not all JSON or XML content can be read by spreadsheets applications. JSON and xml are not confined to a tabular representation of data. The tabular separation makes csv simple to generate from a database management system or DBMS.

Spreadsheet applications such as Microsoft Excel can read and write csv files natively. Those csv files can also be read and written by the spreadsheet application of LibreOffice. Furthermore, spreadsheet applications are often used to read csv files generated from database management systems. 

Reference
EdoCeo. "Comma Separated Values (CSV) Standard File Format." n.d. Edoceo. 2014 .
Microsoft. "Import or export text (.txt or .csv) files." 2006. Office. 2014 .
Networking Group. "Common Format and MIME Type for CSV Files." October 2005. 2014 .


Csv and Json can represent tabular data so they are interchangeable in some cases
There is a CSV to JSON converter here http://formatdelimeters.azurewebsites.net/ thought it's just an Azure website that can be taken down anytime