google-spreadsheet-javascript icon indicating copy to clipboard operation
google-spreadsheet-javascript copied to clipboard

data organized like a Array is very difficult to track

Open oesp opened this issue 13 years ago • 1 comments

If someone change the number of columns of the document all data will be difficult to consume. By the way, very simple and cool code. Thanks.

Later I will update the tests

oesp avatar Jun 02 '12 01:06 oesp

I just write a function to convert the columns to an array of objets. I use the first row as label for the objects

function metteInRiga(result){
    // I assume every column has the same number of elements and nothing is empty
    var chiavi = {};
    var elements = [];
    var elNumber = 0;

    // for each column save key value and the label (first row of spreadsheet)
    $.each( result.data, function( key, value ) { 
      chiavi[key] = value[0];
      elNumber = value.length; // here save the number of elements
    }); 

   for (var i = 1; i < elNumber; i++) { // now I scroll down every column
    var element = {};
      $.each(chiavi, function( key, value ) {
                     // now I create an object for each row using the label 
        element[value] = result.data[key][i];
    });
      elements.push(element); // add the object to the final array
  };
  return elements;
}

You can call this function inside

googleSpreadsheet.load(function(result) {
  dati = metteInRiga(result);
});

ManuelColombo avatar Nov 14 '13 09:11 ManuelColombo