0

I have to read an Excel file and show the output on a speedometer. I have taken the input, and here is my HTML and scripting code, the Excel file contains name and value:

<input type="text" id="txtSpeed" name="txtSpeed" value="20" maxlength="2" />
<input type="button" value="Draw" onclick="drawWithInputValue();">
<input type="file" id="file" onchange="checkfile(this);" />
<input type="button" id="btnSubmit" onclick="readdata(1, 2)" value="Submit" />
function checkfile(sender) {
    var validExts = new Array(".xlsx", ".xls", ".csv");
    var fileExt = sender.value;
    fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
    if (validExts.indexOf(fileExt) < 0) {
        alert("Invalid file selected, valid files are of " +
           validExts.toString() + " types.");
        return false;
    }
    else return true;
}

function readdata(x,y) {
    try {
        var excel = new ActiveXObject("Excel.Application");
        excel.Visible = false;
        var excel_file = excel.Workbooks.Open("D:\\Test.xls");
        \\  alert(excel_file.worksheets.count);
        var excel_sheet = excel_file.Worksheets("Sheet1");
        var data = excel_sheet.Cells(x, y).Value;
        //alert(data);
        drawWithexcelValue(data);
    }
    catch (ex) {
        alert(ex);
    }
    //    return data;
}

and for the speedometer here is the code

function drawWithInputValue() {
    var txtSpeed = document.getElementById('txtSpeed');
    alert(txtSpeed.value);
    if (txtSpeed !== null) {
        iTargetSpeed = txtSpeed.value;
        // Sanity checks
        if (isNaN(iTargetSpeed)) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed < 0) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed > 80) {
            iTargetSpeed = 80;
        }
        job = setTimeout("draw()", 5);
    }
}

function drawWithexcelValue(val) {
    var txtSpeed = val;
    if (txtSpeed !== null) {
        iTargetSpeed = txtSpeed.value;
        // Sanity checks
        if (isNaN(iTargetSpeed)) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed < 0) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed > 80) {
            iTargetSpeed = 80;
        }
        job = setTimeout("draw()", 5);
    }
}

So i am getting the file excel file ,and through this excel file the values should be shown on speedometer .But while clicking on submit button the niddle of speedometer goes down to 0..it is not showing the values of excel file on speedometer..

function checkfile(sender) is for the validation function readdata(x,y) is for the reading of excel file function drawWithInputValue() is for the output manually..like ,suppose you have enter 40 value in text name"txtspeed" then on clicking on Draw button it will show the value in speedometer by rotating the niddle

function drawWithexcelValue(val) is for the reading excel file and show its value in the output..

here is the problem .. suppose we have some name and values in the excel file which is as follows name values India 35 china 46 U.S.A 73 so now by clicking on the submit button it has to show 35 first and the 46 and the 73 with the names ..but what is happening according to my code is while clicking the submit button it doesn't show the value from the excel file it just show zero...HELP!!!!!!

user2502227
  • 495
  • 1
  • 10
  • 23
  • ohh!!!!com-on where are all intelligent guys!!!help me solving this please – user2502227 Jun 25 '13 at 20:03
  • 1
    Please ask a question. – Jack Jun 25 '13 at 20:51
  • my code is not showing me correct answer ..please tell me where i ma wrong – user2502227 Jun 25 '13 at 21:12
  • You need to give more details on where the problem is being caused and maybe some specific example of what it is showing wrong. It would only take a little debugging on your part to make it a clear and concise question. – Lance Roberts Jun 25 '13 at 21:33
  • @jack please help brada !!!! – user2502227 Jun 25 '13 at 21:33
  • For example, if you log `val` in `drawWithexcelValue`, is the right value showing up there? If not, then you've got a problem with the read. If so, then you've got a problem with the draw. Bonhomie won't get you answers here. – Chris Baker Jun 25 '13 at 21:34
  • Print the intermediate values with console log and iteratively get to the place where the error is. http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it – Ondra Žižka Jun 25 '13 at 21:34
  • ok my excel file contain cell product and value ..so suppose that product is T and its value is 57 then the speedometer stick will rotate to show 57 ..this is the ideal case .....but in my case it is reading the excel file but instead of showing value 57 it just show 0 – user2502227 Jun 25 '13 at 21:42

1 Answers1

2

A call to .Cells() expects RowIndex followed by ColumnIndex. Common naming practices suggest x refers to the column, and y refers to the row. If that's right, then you are passing in your arguments in the wrong order. It should be:

var data = excel_sheet.Cells(y, x).Value;

I can't help but notice some other issues:

  • You are using backslashes instead of slashes for one of your comments. That will result in a syntax error.
  • You never use the file input - the path to the excel file is hard coded.
  • You are passing a value to drawWithexcelValue(), but then you try to access a value property. Maybe you think you are passing a reference to the cell?
  • You are passing a string to setTimeout() instead of a function reference. Technically, that's legal, but it's old school.
  • Your drawWithXValue() functions are nearly identical. You should create one function that shares common functionality:
function drawWithValue(val) {
    iTargetSpeed = val || 0;
    if (iTargetSpeed < 0) {
        iTargetSpeed = 0;
    } else if (iTargetSpeed > 80) {
        iTargetSpeed = 80;
    }
    job = setTimeout(draw, 5);
}
function drawWithInputValue() {
    drawWithValue(document.getElementById('txtSpeed').value);
}

Or, even a little more concise:

function drawWithValue(val) {
    iTargetSpeed = Math.max(0, Math.min(80, val || 0));
    job = setTimeout(draw, 5);
}
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • i appreciate for your code ..I tried what u said to me but the out put is the same i,e. it is showing wrong out put that is zero..and abut slashes ,i mistakenly written there while editing the code for this .. – user2502227 Jun 26 '13 at 14:06