矩阵的表格和值增加了三倍

The tables and values of the matrix are tripled

无法将数组矩阵合二为一HTMLtable。出于某种原因,矩阵的 table 和值增加了三倍,我现在不知道为什么。

输出:

Sub writeTable(arrData)
    Dim intCount
    For intCount = LBound(arrData) To UBound(arrData)
        document.write "<!DOCTYPE html>"
        document.write "<html>"
        document.write "<head>"
        document.write "<style>"
        document.write "table, th, td {"
        document.write "border: 1px solid black;"
        document.write "border-collapse: collapse;"
        document.write "}"
        document.write "</style>"
        document.write "</head>"
        document.write "<body>"
        document.write "<p>Test</p>"
        document.write "<table style='width:50px'>"
        document.write "<tr>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(2, intCount) &"</td>"
        document.write "</tr>"
        document.write "<tr>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(1, intCount) &"</td>"
        document.write "<td>"& arrData(2, intCount) &"</td>"
        document.write "</tr>"
        document.write "<tr>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(1, intCount) &"</td>"
        document.write "<td>"& arrData(2, intCount) &"</td>"
        document.write "</tr>"
        document.write "</table>"
        document.write "</body>"
        document.write "</html>"
    Next
End Sub

您发布的代码创建了一个完整的 HTML 文档,数组的每一行都有一个 3 行 table。以下代码段将输出一行:

document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"

重复该片段 3 次会在输出中重复该行 3 次。

document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"

只有你在第二个语句中有一个错字,导致第一个字段在第一行输出两次。

基本上有两种方法可以解决这个问题:

  • 只为 table 行而不是整个文档执行循环,并且在循环体中只输出一行:

    document.write "<!DOCTYPE html>"
    document.write "<html>"
    ...
    document.write "<p>Test</p>"
    document.write "<table style='width:50px'>"
    For intCount = LBound(arrData) To UBound(arrData)
        document.write "<tr>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(0, intCount) &"</td>"
        document.write "<td>"& arrData(2, intCount) &"</td>"
        document.write "</tr>"
    Next
    document.write "</table>"
    document.write "</body>"
    document.write "</html>"
    

    这是推荐的方式。

  • 删除循环并在自己递增行索引的同时重复语句:

    document.write "<td>"& arrData(0, 0) &"</td>"
    document.write "<td>"& arrData(1, 0) &"</td>"
    document.write "<td>"& arrData(2, 0) &"</td>"
    document.write "</tr>"
    document.write "<tr>"
    document.write "<td>"& arrData(0, 1) &"</td>"
    document.write "<td>"& arrData(1, 1) &"</td>"
    document.write "<td>"& arrData(2, 1) &"</td>"
    document.write "</tr>"
    document.write "<tr>"
    document.write "<td>"& arrData(0, 2) &"</td>"
    document.write "<td>"& arrData(1, 2) &"</td>"
    document.write "<td>"& arrData(2, 2) &"</td>"
    

    我不推荐这种方法,因为它无法在不修改代码的情况下处理数组中不同数量的行。