使用代码填充多个列

时间:2016-03-21 10:51:41

标签: vbscript

我使用vb脚本代码填充表: -

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

但是我想生成多列,我当前的输出是: -

universespace->Milkyway 
universespace->Earth Layer 
scienceSubject->Solar System 
scienceItem->Earth 
scienceItem->Moon 
scienceItem->Mars 

我希望它像: -

+--------------+--------------+--------------+-----------+
|universespace1|universespace2|scienceSubject|scienceItem| 
+--------------+--------------+--------------+-----------+
| Milkyway     | Earth Layer  | Solar System | Earth     |
+--------------+--------------+--------------+-----------+
| Milkyway     | Earth Layer  | Solar System | Moon      |
+--------------+--------------+--------------+-----------+
| Milkyway     | Earth Layer  | Solar System | Mars      |
+--------------+--------------+--------------+-----------+

我尝试了其他各种方法: -

Set table = document.createElement("table")
Set tbody = document.createElement("tbody")
i = 0
For Each node In objMSXML.selectNodes(sXPath)   
    Set tr = document.createElement("tr")
     Select Case True
     Case "namespace" = node.parentNode.nodeName 
        Set th = document.createElement("th")
        th.innerText =   node.parentNode.nodeName
        tr.appendChild th
      Case "namespace" = node.parentNode.nodeName
        Set th1 = document.createElement("th")
        th1.innerText =   node.parentNode.nodeName
        tr.appendChild th1
      Case "querySubject" = node.parentNode.nodeName
        Set th2 = document.createElement("th")
        th2.innerText =   node.parentNode.nodeName
        tr.appendChild th2
      Case "queryItem" = node.parentNode.nodeName
        Set th3 = document.createElement("th")
        th3.innerText =   node.parentNode.nodeName
        tr.appendChild th3
     End Select
    'td.innerText = i & node.parentNode.nodeName & "->" & i & node.text
    'tr.appendChild td
    tbody.appendChild tr

仍无法以所需格式获得输出。它全部都是单列而不是不同的列。

答案here无法帮助我创建包含列的表格。它只显示一个包含一行和一列的表,即表格单元格。

1 个答案:

答案 0 :(得分:0)

我可以创建表格。学习编码所以总是问愚蠢的问题。 : - )

Set table = document.createElement("table")
Set tbody = document.createElement("tbody")
i = 0
Set tr = document.createElement("tr")
set th = document.createElement("th")
th.innerHtml = " Namespace1  "
tr.appendChild th
set th = document.createElement("th")
th.innerHtml = "  Namespace2  "
tr.appendChild th
set th = document.createElement("th")
th.innerHtml = "  Query Subject  "
tr.appendChild th
set th = document.createElement("th")
th.innerHtml = "  Query Item  "
tr.appendChild th
Set tr1 = document.createElement("tr")
For Each node In objMSXML.selectNodes(sXPath)   
    Set td = document.createElement("td")
    td.innerText = i & node.text
    tr1.appendChild td
    tbody.appendChild tr
    tbody.appendChild tr1
    i = i +1
Next
document.body.appendChild list
table.appendChild tbody
document.body.appendChild table