From CSV to javascript – How to make data fit

Don’t let codes like this be an obstacle when you’ve got a lot of data. There are ways to make them all fit in with a few clicks.

[tweetmeme]By Martin Ove

Here’s a tip, I found out about while doing a project.

Sometimes you have a lot of data, that you want to fit into a certain format. This happened to me while trying to get Google Charts to work with my dataset, which was stored in pure .csv.

The javascript wanted it to look like this: data.addRow([“Name”, Data1, Data2, Data3]);

All I got was this, separated by tabulator: Name     Data1     Data2     Data3

I used Google Spreadsheets to automate the process of putting quotation marks, commas, and space in between the data, check out how below.

The process is extremely universal and might come in handy, if you don’t know it already.

[simage=61,200,n,left,] First of, I had the data, ordered like this in my spreadsheet.

[simage=63,200,n,left,] If I wanted to insert it into the Google Charts javascript, it had to look like this though: data.addRow([“Name”, Data1, Data2, Data3]);

[simage=64,200,n,left,] In the spreadsheet, I put data.addRow([” in one cell, left one cell free, wrote “, in the next, left one free and so on.

[simage=65,200,n,left,] In the empty columns, I assigned the data columns. Here A2 is the cell which contains the name of the first city.

[simage=66,200,n,left,] I dragged the cell downwards, like you would in any other spreadsheet.

[simage=67,200,n,left,] And eureka, all the city names are there. Now, just do the same for the rest of the columns and you get…

[simage=68,200,n,left,] …something like this. I find an empty cell and write =concatenate(

[simage=69,200,n,left,] I then select the cells that I want to concatenate and voila!

[simage=70,200,n,left,] The output becomes the content of all the cells put together, just what I need.

[simage=71,200,n,left,] Next step is to apply it to all the rows, and there you have it.

[simage=72,200,n,left,] The column is easily exportable, as you can just copy/paste it out.

It’s needless to say, that this process can be used every time you need make something fit right in.

Thanks for reading.

One thought on “From CSV to javascript – How to make data fit

  1. Pingback: Google Charts and where to start | The tech in journalism

Leave a Reply

Your email address will not be published. Required fields are marked *