0

My problem is simple. I would like to know if there is a way to access a specific column from a csv file which is in MS Excel format. There are about 40 fields or columns in the file. I want to access only 3 of these. Please guide me.

Thanks in advance

This is as far as I got. Can we count the number of columns in the file?

$(document).on("click","#bucket_list a",function(evt){
    //    console.log("ID :: ",evt.target.id);
    var bucketID = evt.target.id;
    evt.preventDefault();
    // alert("Event Fired");
    counter = 0;

    //lists the bucket objects
    s3Client.listObjects(params = {Bucket: bucketID}, function(err, data){
        if (err) {
            document.getElementById('status').innerHTML = 'Could not load objects from ' + bucketID;
        } else {
             //document.getElementById('status').innerHTML = 'Loaded ' + data.Contents.length + ' items from ' + bucketID;
             var listStart = document.createElement("ul");
             document.getElementById('status').appendChild(listStart);
             for(var i=0; i<data.Contents.length;i++){
                fileKey = data.Contents[i].Key;
                if(fileKey.search(str) != -1) {                
                    //var listItems = document.createElement("li");
                    //listItems.innerHTML = data.Contents[i].Key;
                    //listStart.appendChild(listItems);
                    fileList[i] = data.Contents[i].Key;
                    //alert(fileList[i]);
                    counter = counter + 1;
                }
            }
            if(counter == 0){
                alert("This bucket has no CSV files. Please try another bucket!");
            }
            // else{ 
            //     for(var i = 0; i<fileList.length;i++){
            //         alert("File: " + fileList[i]);
            //     }   
            // }
        }

       //to read the contents of the files into textviews
        var file = fileList[0];
        //console.log("Loading:: ", file);
        s3Client.getObject(params={Bucket: bucketID, Key: file},function(err,data){
            if(err != null){ alert("Failed to load object " + err); }
            else{
                //alert("Loaded " + data.ContentLength + " bytes");

                var allDataLines = data.split(/\r\n|\n/);
                var headers = allDataLines[0].split(',');
            }
        });
    });
});

I am unable to figure out how to get only the second column.

Pallavi Prasad
  • 577
  • 2
  • 9
  • 28
  • You will need a server side language to do that. I used PHP Excel reader https://phpexcel.codeplex.com/ – Lau Jun 17 '15 at 09:03

1 Answers1

0

Yes. You have to parse the csv file to get the values for a particular column.Refer the following post on how to read a CSV file in Javascript.

[How to read data From *.CSV file using javascript?

To give you a head start the logic is simple . You just have to split based on "," and do a if statement.

Community
  • 1
  • 1
May13ank
  • 548
  • 2
  • 9
  • 24