Generating table

痞子三分冷 提交于 2019-12-12 04:08:05

问题


I am trying to push my output from VBScript code in table row and table cell.

My code is:

Set table = document.CreateElement("table") 
i = 0
For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    For Each element In node
        td.innerText = element.parentNode.nodeName & "->" & element.text
        tr.appendChild td
    Next
    table.appendChild tr
    ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
    i = i + 1
Next
document.body.appendChild table

What is wrong with it? it is not working while I am able to push output in list.

Edit I am using this code it prints output as expected but table is not populated.

ObjOutFile.WriteLine thing.path
document.body.appendChild p
Set tbody = document.createElement("tbody") 
For Each node In objMSXML.selectNodes(sXPath)
    Set trow = document.createElement("tr")
    Set tcol = document.createElement("td")
    tcol.innerText = tcol.innerText & node.parentNode.nodeName & "->" & node.text
    ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
    trow.appendChild(tcol)
    tbody.appendChild(trow)
    'ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
Next
document.appendChild(tbody)

The ObjOutFile.writeLine prints like:

C:\Users\abc\Desktop\samp.txt
hamster->AbcPos
hamster->Database Layer
hairyStyle->qskxyz
hairyGirl->qixyz
hairyGirl->abc
hairyGirl->def

回答1:


You have two issues in your code:

  • node is not a collection, so For Each element In node fails. You probably have an On Error Resume Next elswhere in your HTA that is hiding this error, because otherwise you'd be seeing an error message like this:

    Object doesn't support this property or method.

  • You need to append the table rows to a <tbody> element first (see this related question).

Change your code to something like this (and remove the On Error Resume Next):

Set table = document.createElement("table")
Set tbody = document.createElement("tbody")
i = 0
For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    td.innerText = node.parentNode.nodeName & "->" & node.text
    tr.appendChild td
    tbody.appendChild tr
    i = i + 1
Next
table.appendChild tbody
document.body.appendChild table



回答2:


You want a row for each node and a column for each element of the current node. So

For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    For Each element In node
        td.innerText = element.parentNode.nodeName & "->" & element.text
        ...

should be

For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    For Each element In node
        Set td = document.createElement("td")
        td.innerText = element.parentNode.nodeName & "->" & element.text
        ...


来源:https://stackoverflow.com/questions/36081205/generating-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!