neiiic neiiic - 4 months ago 81
Javascript Question

Add data to new column in csv file with Javascript

I want add data to new column in csv file. I using Javascript. I try with

'\r\n'
but not work. Comma make new row in same column.

for (var i = 0, l = mydata.length; i < l; ++i) {
csvRows.push(mydata[i].join(','));
}
csvRows.push('\r\n'); //here want to go in second column
csvRows.push(mydata[0].join(',')); //this want write in second column first row
var csvString = csvRows.join("%0A");
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvString;
a.target = '_blank';
a.download = 'file.csv';
document.body.appendChild(a);
a.click();


What I must push in
csvRows
to make new column? FIDDLE is similar example.

EDIT + FIX: IF YOU HAVE PROBLEM WITH THIS AND OPENING WITH MICROSOFT EXCELL, TRY IN OTHER PROGRAM!

Answer

Let's try here:

var data = [
  "Col1,Col2,Col3",
  "Col4,Col5,Col6"
];
console.log(data);

enter image description here

But if you wanna add one more column, you need:

var data = [
  "Col1,Col2,Col3",
  "Col4,Col5,Col6"
];
for (var i = 0; i < data.length; i++)
  data[i] += ",NewCol" + (i + 1);
console.log(data);

enter image description here

Comments