Monica
Monica

Reputation: 47

Generating a CSV using Javascript

I successfully am generating a CSV using Javascript. I want to have two lines in the final CSV - one header and one data line.

Here is my code for generating the header.

  var csvContent = "data:text/csv;charset=utf-8,participant_id,age,gender,country_live_longest,country_musical_enculturation,lyric_language,favorite_genre,years_formal_training,understood_yes_or_no,media_shown,";
  for (var i = 0; i < randomized.length; i++) {
    csvContent += "arousals_"+ i.toString() +",valences_"+ i.toString() +","
  }
  csvContent = csvContent.slice(0, -1);
  csvContent += '\r\n';

After this it's a simple string append with comma separated values. However, when I check the string getting built it stops being downloadable after the new line or after any spaces.

This is what it looks like in console.

enter image description here

Anything in red is not part of the CSV downloadable file.

What should I do to fix this issue?

Thanks!

Upvotes: 0

Views: 119

Answers (1)

Related Questions