jason
jason

Reputation: 4439

google script array splitting

i got a quick one for someone who can help. I've downloaded some data from yahoo. I want to split the data into a N x 7 array. (is that the correct term?). I want it to look like this:

[[2013-01-29,64.25,65.03,64.00,64.24,4883100,64.24],[2013-01-28,64.51,64.87,63.27,64.59,7591300,64.59],...]

but now, as you can see, it's not in that format. Novice to javascript. Please help.

function function() {
var ticker='YUM';
var startMonth=0; var startDate=1; var startYear=2013;
var endMonth=0; var endDate=25; var endYear=2013;
var fetchString="http://ichart.finance.yahoo.com/table.csv?s="+ticker+"&a="+startMonth+"&b="+startDate+"&c="+startYear+"&d="+endMonth+"e="+endDate+"&f="+endYear+"&g=d";
var response = UrlFetchApp.fetch(fetchString);
a=response.getContentText();
var allData = a.slice(a.indexOf("2013"));
}

Upvotes: 0

Views: 3191

Answers (1)

JSDBroughton
JSDBroughton

Reputation: 4034

Assuming you don't want the column headers, this is a one line change:

var allData = a.match(/(.*?)\n/g)       // convert each line to a row
               .splice(1)               // remove headers row
               .map(function(row){ 
                  return row.replace(/\n/,'').split(','); 
               });                      // convert row string to array

Upvotes: 1

Related Questions