View Full Version : having an issue with one line of code

12-30-2007, 04:41 PM

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.

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.

Here is the code that I am using

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

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


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)

Excel.Quit(); // Close Excel with the Quit method on the Application object.
<FORM method="GET" name="ajaxGet" action="">
<INPUT type="BUTTON" value="Submit" ONCLICK="submitForm()">
<INPUT type="text" name="dynamic" value="">

12-30-2007, 05:04 PM
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.

12-30-2007, 05:29 PM
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