如何使用vbscript识别hta中复选框的状态(选中的值)

时间:2016-06-14 09:34:53

标签: excel vbscript hta

以下是读取excel数据并将该数据导入hta表的代码, 并将编辑后的数据从hta写入excel, 在我的htaExcel Sample Data中,如果我选中所需的复选框并单击Click To Write按钮,则数据将以“是”或“否”存储在Excel工作表中。

<!DOCTYPE html>
<head>
<HTA:application
Applicationname="MyApp"
BORDER="thin"
BORDERSTYLE="Normal"
Caption="yes"
ICON="D:\VBScriptTrainee\Ampeross-Qetto-Icon-developer.ico"
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SINGLEINSTANCE="no"
NAVIGABLE="yes"
SYSMENU="yes"
VERSION="2.0"
WINDOWSTATE="maximize"/>

<title>Read Excel</title>
<link rel="stylesheet" href="D:\VBScriptTrainee\bootstrap-3.3.6-dist\css\bootstrap.min.css">

<style type="text/css">
body
{
background-color:#1abc9c;
}
P
{
font:bold 18px arial;
}
</style>
<script language="vbscript">

Public mySpan,selectexcel,objExcel,objWorkbook,rowCount,colCount,rowData,textData,sp,b,tempcol,checkData,i,j

On error resume next

Sub ReadExcelData()

  Set fileexcel =document.getelementbyid("file_id")
  file2=fileexcel.value
  'selectexcel      = inputbox("Enter the path","path here","D:\VBScriptTrainee\Sample.xlsx")
  Set objExcel      = CreateObject("Excel.Application")
  Set objWorkbook   = objExcel.Workbooks.open(file2)
  objExcel.visible  = True

  rowCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
  colCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Columns.Count

  Dim mySpan

  'colCount=colCount+1  
  tempcol=colCount
  'MsgBox(tempcol)
  'MsgBox(colCount)
  rowData="<table border=2>"  

  For intRow=1 to rowCount step 1

   'rowData=null
    rowData = rowData & "<tr>"
    For intCol=1 to colCount step 1


        'if ((intRow =1) and (intCol=tempcol)) then
             'objExcel.Cells(intRow,tempcol).Value = "Flag"
             'intRow =IntRow + 1 
        'End if
         If ((intRow <> 1) and (intCol = tempcol)) then
             'objExcel.Cells(1,tempcol).Value = "Flag"

                     rowData = rowData & "<td>" & "<input type='checkbox' id='flag_id' name='flag' />" & "</td>"
                     'checkData = cStr(rowData)


         End If





            If (intRow=1) Then

                rowData= rowData & "<td>" & "<b><font color=red>" & objExcel.Cells(intRow,intCol).Value & "</font></b>"  & "</td>"          
            Else    

                    rowData = rowData & "<td><div contentEditable='True'>"  & objExcel.Cells(intRow,intCol).Value  & "</div></td>"

            End If        
    Next
                    rowData = rowData & "</tr>"

  Next
                    rowData =rowData & "</table>"

             Set mySpan=document.getelementbyid("Span_id_two")
             mySpan.innerHTML = rowData & "<br>"

             'Set myExcel=document.getelementbyid("selectfile")
             'myExcel.innerHTML = selectexcel




             MsgBox "Data readed successfully",vbInformation


              document.getElementById("file_id").accept = ".xlsx"
End sub

Sub WriteExcelData()  

       mwresult = Msgbox ("Are you Sure to Write ?",vbOKCancel)

        If (mwresult=1) then

           Set mySpan=document.getelementbyid("Span_id_two")
           textData=mySpan.innerHTML

           Set tab=document.getElementsByTagName("table")(0)  

           mytable = document.getElementsByTagName("table")(0).rows.length      
Msgbox("myTable (rows Length) " & mytable)         
           mytable1= document.getElementsByTagName("table")(0).rows(0).cells.length  
Msgbox("myTable1 (rows Cells Length) " & mytable1)         

                For n = 0 to (mytable-1)  

                    For j = 0 To (mytable1-1)  

                        objExcel.Cells (n + 1, j + 1).Value = tab.Rows(n).Cells(j).innerHTML   
                        tableValue = tab.Rows(n).Cells(j).innerHTML 

                          If(tableValue="<INPUT id=flag_id type=checkbox CHECKED name=flag>") then

                                objExcel.Cells (n+1,mytable1).Value="Yes"

                          End If

                          If(tableValue="<INPUT id=flag_id type=checkbox name=flag>") then

                                objExcel.Cells (n+1,mytable1).Value="No"

                          End If                          

                    Next  

                Next   

                find ="<*>"
                rplace =" "

                objExcel.Cells.Replace find,rplace

                MsgBox "Data Exported Successfully",vbInformation                            

                objExcel.ActiveWorkbook.Save
                Set objExcel=nothing

        End If

                If(mwresult=2) then
                     MsgBox "You Selected Cancel",vbInformation
                End If

End sub

</script>

</head>

<body>


<center>

   <img src="D:\VBScriptTrainee\Excel.png" height=200 width=300/>

</center>


<center>
<h1 align="center" style="color:blue"><img src="D:\VBScriptTrainee\icon-developer-icon.png" height=100px width=100px/>
Read Excel**</h1><br>
<a style="a.link:red;a.hover:green;a.visited:blue;" href="D:\VBScriptTrainee\Hta_Rules.hta">Rules*</a><br><br>
<input class="btn btn-primary text-primary" type="file" id="file_id" name="file_id" accept=".xlsx">
<br>

<button class="btn btn-success" name="Read" value="Click To Read" onclick="ReadExcelData()">Click To Read</button>
<button class="btn btn-danger" name="Write" value="Click To Write" onclick="WriteExcelData()">Click To Write</button>



<br>
<br>
<p id="selectfile"></p>
<span ID="Span_id_two" Style="Color:blue;" name="text_name">
</span>
</div>
</div>
</div>
</center>
<marquee direction="Right" style="color:red;">***Note : Select only excel files***</marquee>

</body>
</html>

但是,我已经通过提供html格式识别出所选的检查值数据,如此

来自上面的编码

If(tableValue="<INPUT id=flag_id type=checkbox CHECKED name=flag>") then

    objExcel.Cells (n+1,mytable1).Value="Yes"

End If

If(tableValue="<INPUT id=flag_id type=checkbox name=flag>") then

    objExcel.Cells (n+1,mytable1).Value="No"

End If

是否有任何替代方式来标识复选框的状态(是否选中复选框)。

这是我的excel表..

ExcelSheet

2 个答案:

答案 0 :(得分:6)

toCelcius的函数定义语法错误

 function toCelsius(f) { //removed var
   return (5/9) * (f-32);
 }

您的浏览器可以使用调试工具(如控制台)轻松找到这些问题。

答案 1 :(得分:2)

在JavaScript中定义函数时,您不必包含某种&#34;类型&#34;喜欢强类型语言。因此,在var参数列表中添加toCelsius会导致错误。将其更改为:

function toCelsius(f)