Dino
Dino

Reputation: 402

Get total row count of an csv file with jQuery/Javascript

I have .csv files that are updated periodically and I need to output the total row count in an .html file.

I'm not that bad when it comes to jQuery, but this I never did and really would use some help form someone that has any experience with this.

Upvotes: 2

Views: 9073

Answers (1)

yesterdaysfoe
yesterdaysfoe

Reputation: 808

  1. Read the csv file

    var csvContents = getCsvContents(myFile);

  2. Split the lines

    var lines = csvContents.split('\n');

  3. Get the count

    lines.length

Example:

 var myFileUri = "file.csv";
 alertCsvCount(myFileUri);

 function alertCsvCount(myFile){
   //use jquery get
   $.get(myFile, function(data) {
      var lineCount = data.split('\n').length;
      //do what you want to do in lineCount
      alert(lineCount);
   })
 }

Upvotes: 4

Related Questions