Results 1 to 3 of 3

Thread: having an issue with one line of code

  1. #1
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default having an issue with one line of code

    Hi

    background info:
    The project that I am working on takes an array of stock symbols and uses the req.open("GET",url,true) process to dynamically get content from a url and save the content in an excel spreadsheet.

    Issue:
    The issue that I am having is in the function call handleresponse,
    if I just leave this line of code within the function
    document.ajaxGet.dynamic.value = result.substring(endstring-4,endstring);
    and after the functino I add this line of code
    Book.ActiveSheet.Cells(i + 1,1).Value = mystocks[i];
    Book.ActiveSheet.Cells(i + 1,2).Value = result.substring(endstring-4,endstring
    the script does not work, but if I change the this line of code to
    Book.ActiveSheet.Cells(i + 1,2).Value = 2
    and run the script all the values are being entered into the spreadsheet.

    I am not sure what I am doing wrong here, can some one help.

    Thanks
    Here is the code that I am using



    <html>
    <head>
    <title>Fetch Page</title>
    <script language="JavaScript1.2">
    var mystocks = new Array();
    mystocks[0] = "A";
    mystocks[1] = "AAPL";
    mystocks[2] = "MSFT";


    var Excel, Book; // Declare the variables
    Excel = new ActiveXObject("Excel.Application"); // Create the Excel application object.
    Excel.Visible = false; // Make Excel invisible.
    Book = Excel.Workbooks.Add() // Create a new work book.
    for (i=0;i<mycars.length;i++)
    {
    var req = new ActiveXObject("Microsoft.XMLHTTP");
    req.onreadystatechange = handleResponse
    req.open("GET", "http://quote.barchart.com/texpert.asp?sym=" + mystocks[i], true);
    req.send(null);




    function handleResponse() {
    var charactercount = req.responseText.length
    var startstring = req.responseText.indexOf("Overall Average");
    var newresponse = req.responseText.substring(startstring,charactercount)
    var endstring = newresponse.indexOf("-")
    var result = newresponse.substring(0,endstring)
    //var barhcartdata = document.write(result.substring(endstring-4,endstring))
    if(req.readyState == 4)
    if(req.status == 200)

    document.ajaxGet.dynamic.value = result.substring(endstring-4,endstring);

    //Book.ActiveSheet.Cells(i + 1,2).Value = result.substring(endstring-4,endstring);

    else

    document.ajaxGet.dynamic.value ="Error: " + req.status + " " + req.statusText;
    }

    Book.ActiveSheet.Cells(i + 1,1).Value = mystocks[i];
    Book.ActiveSheet.Cells(i + 1,2).Value = result.substring(endstring-4,endstring)





    }
    Book.SaveAs("C:/TEST.xls");
    Excel.Quit(); // Close Excel with the Quit method on the Application object.
    </script>
    </head>
    <body>
    <FORM method="GET" name="ajaxGet" action="">
    <INPUT type="BUTTON" value="Submit" ONCLICK="submitForm()">
    <INPUT type="text" name="dynamic" value="">
    </FORM>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd throw in an alert in there someplace to determine what the value and type of the expression result.substring(endstring-4,endstring) is at the time it is required to be the number 2. From the way it is constructed, I would expect its value to be a string (if endstring is a valid number and endstring-4,endstring defines a valid range), perhaps empty or not, but not a number, which appears to be what you are after.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Thanks for responding

    I already had added an alert
    and when I just keep it to value 2 or any other value I enter it works fine but when I try change it to Book.ActiveSheet.Cells(i + 1,2).Value = result.substring(endstring-4,endstring)
    it does not work

    I think the extra lines of code to insert into the excel spreadsheet is interfearing with the get method, if I run these two scripts seperatly they run fine it is when I combine them that I am running into an issue

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •