mercredi 9 septembre 2015

Javascript for-loop errors - retrieving data from Google sheet using tabletop.js

I am retrieving data from a Google sheet using tabletop.js.

Depending on which user is viewing the page (they have a user id), it returns data from a specific row of the spreadsheet (the row with their id in it).

I have used if / else if to retrieve data from the sheet successfully - by manually searching the first three rows of the spreadsheet to return one piece of data.

As there will be a large/unknown amount of rows in the spreadsheet eventually, I am trying to use a loop to search through all the rows automatically. However, this just returns ,,,,,,,, - so hasn't worked.

Can anyone assist me with my loop?

To manually retrieve data from my first 3 rows, I have used the code:

 var player = GetPlayer();
    var myName  = lmsAPI.GetStudentName();

    var array  = myName.split(',');
    var Name = array[1] + ' ' + array[0];

    player.SetVar("name", Name);

    var ID = lmsAPI.GetStudentID();

    player.SetVar("id", ID);

    player.SetVar("quizid",ID);

    var quizId = ID

    var public_spreadsheet_url = 'http://ift.tt/1XNgAB2';

        Tabletop.init( { key: public_spreadsheet_url,
                         callback: showInfo,
                         simpleSheet: true } );


      function showInfo(data) {
    if (quizId == ("id",data[0].id)){
        player.SetVar("badge1",data[0].badge1)
        }  

    else if (quizId == ("id",data[1].id)){
        player.SetVar("badge1",data[1].badge1)
        } 

    else if (quizId == ("id",data[2].id)){
        player.SetVar("badge1",data[2].badge1)
        } 

    else
    {
      player.SetVar("badge1","false")
    };
    }

So this checks the id of the person viewing the page against the id column of row 0, 1 and 2 of the spreadsheet and returns whatever is in the "badge1" column for that person.

So, I have then tried a loop:

var player = GetPlayer();
var myName  = lmsAPI.GetStudentName();

var array  = myName.split(',');
var Name = array[1] + ' ' + array[0];

player.SetVar("name", Name);

var ID = lmsAPI.GetStudentID();

player.SetVar("id", ID);

player.SetVar("quizid",ID);

var quizId = ID

var public_spreadsheet_url = 'http://ift.tt/1XNgAB2';

        Tabletop.init( { key: public_spreadsheet_url,
                         callback: showInfo,
                         simpleSheet: true } );


      function showInfo(data) {
for (var i = 0; i < data.length; i++) { 
(quizId == ("id",data[i].id));
{
        player.SetVar("badge1",data[i].badge1)
        } 
};

}

But this does not work.

I am attempting to search through all rows in the spreadsheet automatically - but I'm not sure what I am missing.

Any help would be appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire