2

I am trying to pull a set of cells from a google spreadsheet into a webpage for reporting. I read a few other posts which suggested using a timeout but I cannot seem to get that working either. If you think that would work, I can add my code that I tried to use for the timeout. Here is my code:

    var days = new Array(); 
days[0] ="G11%3AG11"; /*Today*/
days[1] ="G10%3AG10";
days[2] ="G9%3AG9";
days[3] ="G8%3AG8";
days[4] ="G7%3AG7";
days[5] ="G6%3AG6";
days[6] ="G5%3AG5";
days[7] ="G4%3AG4"; /*One week ago*/

    var dayOfWeek = new Array();    
dayOfWeek[0] ="day0";
dayOfWeek[1] ="day1";
dayOfWeek[2] ="day2";
dayOfWeek[3] ="day3";
dayOfWeek[4] ="day4";
dayOfWeek[5] ="day5";
dayOfWeek[6] ="day6";
dayOfWeek[7] ="day7";

    function getValue(cell, element){
var url = "https://docs.google.com/spreadsheet/pub?key=MyKey&single=true&gid=2&range="+cell+"&output=csv";
temp = new XMLHttpRequest();
        temp.onreadystatechange = function () {
          if (temp.readyState === 4) {
              document.getElementById(element).innerHTML = temp.responseText;
          }
      };
      temp.open("GET", url, true);
      temp.send(null);

}

When I call the function in the main body of the code this is what it looks like:

    for(var a=0;a<days.length;a++){      
     getValue(days[a],dayOfWeek[a]);
    }

When I run it in a for loop I get the same value for each of my id tags. They are all the value from the last item in the array. I am not sure if the issue is with the xmlhttprequest not getting enough time or what, any help would be appreciated.

2
  • your loop declares an index a yet you reference i in the body? Also you should really consider making a single request for data pertaining to each day rather than 7 concurrently as this will inevitably be the start of a bottleneck when you do get your code working. Commented Oct 8, 2013 at 19:11
  • Good catch @Emissary thanks! Eventually we will switch to pulling all 7 days but this was simpler to get the project up and running for the moment. Commented Oct 8, 2013 at 20:04

1 Answer 1

3

You're not properly declaring "temp" as a local variable in the XHR event handler. Add var in front of it. Without that, it's global. That means that each call to "getValue()" overwrites the work of the previous one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.