Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data organized like a Array is very difficult to track #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

oesp
Copy link

@oesp oesp commented Jun 2, 2012

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

@ManuelColombo
Copy link

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);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants