Question

I have created a .hta form that connects to an excel file. The data entered is pasted in the spreadsheet. I have two problems with this code and I can't seem to figure out what I am doing wrong.

<html>
<head>
    <script>
        function test() {
            var excel = new ActiveXObject("Excel.Application");
            excel.visible = true;
            var wb = excel.Workbooks.Open("C:\\Users\\Dane\\Desktop\\1.xlsx");
            var optionValue;
            var s = Form1.select1;
            var data = [];
            var sCell = wb.sheets("sheet1").cells(1,1).currentregion.offset(1);

            data.push(Form1.text1.value);
            data.push(Form1.text2.value);
            data.push(Form1.text3.value);
            data.push(Form1.text4.value);
            data.push(s.options[s.selectedIndex].text);
            data.push(Form1.text5.value);
            data.push(Form1.text6.value);
            data.push(Form1.text7.value);


            for(var i=0;i<Form1.option1.length;i++) {
                if(Form1.option1[i].checked){
                    data.push(Form1.option1[i].value);
                    break;
                }
            }

            for(var i=0; i<data.length;i++) {
                sCell.offset(0,i) = data[i];            
            }



            for(var i=0;i<Form1.option2.length;i++) {
                if(Form1.option2[i].checked){
                    data.push(Form1.option2[i].value);
                    break;
                }
            }

            for(var i=0; i<data.length;i++) {
                sCell.offset(0,i) = data[i];            
            }

            wb.close(true);
        }
    </script>
</head>
<body>
<form name="Form1">

    <p>
    Entry 1: <input name="text1" type="text" size="10" /><br />
    Entry 2: <input name="text2" type="text" size="10" /><br />
    Entry 3: <input name="text3" type="text" size="10" /><br />
    Entry 4: <input name="text4" type="text" size="10" /><br />

    Selection 1: <select name="select1">
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
        <option value="4">D</option>
        <option value="5">E</option>
        <option value="6">F</option>
    </select> <br />

    Entry 5: <input name="text5" type="text" size="10" /><br />
    Entry 6: <input name="text6" type="text" size="10" /><br />
    Entry 7: <input name="text7" type="text" size="10" /><br />

    Question 1<br />
    Yes : <input type="radio" name="option1" value="Yes"><br />
    No : <input type="radio" name="option1" value="No"><br />
    N/A : <input type="radio" name="option1" value="N/A"><br />

    Question 2<br />
    Yes : <input type="radio" name="option2" value="Yes"><br />
    No : <input type="radio" name="option2" value="No"><br />
    N/A : <input type="radio" name="option2" value="N/A"><br />


    <input type="button" value="Save to Excel" onclick="test()" />
</form>
</body>

Problems:

  1. The last radio button value is repeated a few times at the end of the spreadsheet.
  2. When the user uses the program a second time, the previous data is overwritten.

Sample to download (make sure to change the file path):

Was it helpful?

Solution

I believe the second problem is caused because this is in your code twice:

for(var i=0; i<data.length;i++) {
            sCell.offset(0,i) = data[i];            
        }

Once you have your array of data, you shouldn't have to cycle through it twice.

For the first problem, I believe it's created by this line:

var sCell = wb.sheets("sheet1").cells(1,1).currentregion.offset(1);

You are saying in essence "select this region here" not "start at this region." Unfortunately, I don't know the code for what you need, but that's my interpretation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top