-1

I've seen this how to export javascript array info to csv on client side

which works fine for me but there's a thing that annoys me. in CSV you can seperate the strings in differents cell with a simple comma (,) so you got like:

[["name1", "city_name1", ...], ["name2", "city_name2", ...]]

and name1 and city_name1 are in the same cell. Is there a way to get them in two different cells in Excel with JavaScript?

Community
  • 1
  • 1
  • The snippet you have there is an array of arrays. Is the entire array entered into a single cell, or is the outer array being split up while the inner arrays are being kept together? – Skytiger Oct 06 '15 at 15:22
  • name1 city_name1 and so on are in one cell in the first row and name2 and city_name2 are in the second row also in one cell – KingOfDarkness Oct 06 '15 at 15:24
  • 1
    I'm confused. What I see in your example is an array of rows, not cells. name1 is in row 1 column 1 and city_name1 is in row 1 column 2. What are you trying to do here? – Joshua Dannemann Oct 06 '15 at 15:24
  • I'm trying to put name1 in the first row, first cell, city_name1 in the first row cell2 and name2 second row, first cell and so on – KingOfDarkness Oct 06 '15 at 15:25
  • That still does not make any sense. When you parse a CSV it comes out as an array of arrays. Array[0] is a row Array[0][0] is a cell. Unless you have a field in quotes like "name1, city_name1" and you need to manually parse it, or you want to flatten the array, then you already have what you are asking for. – Joshua Dannemann Oct 06 '15 at 15:32
  • ooh now i got, sorry, it's my first time parsing into CSV. Thanks for the help! – KingOfDarkness Oct 06 '15 at 15:34
  • You're welcome. I will edit my answer. Please accept thereafter. – Joshua Dannemann Oct 06 '15 at 15:34

2 Answers2

0

When you parse a CSV in JavaScript, the array of arrays that results is not an array of cells. It is an array of rows where Array[0] is a row and Array[0][0] is a cell. What you have here is exactly what you are already asking for- name1 in row 1 cell 1, and name2 in row 2 cell 1.

Joshua Dannemann
  • 2,003
  • 1
  • 14
  • 34
0

Why not just set your data up in a hidden html table? The table will pop into the csv complete with your css styles. So if you want name1 and city_name1 in cells 1 and 2 of row 1, and then name2 and city_name2 in cells 1 and 2 of row 2, set the table up like that.

Cruiser
  • 1,618
  • 2
  • 16
  • 20