0

我有一个包含以下信息的 csv 文件

ParentID,DivID,DataElement,Content,Class
Dash_Reg,Summer_Active,Classes.Active,Active Classes:,class1
Dash_Reg,Summer_Active,Classes.Inactive,Inactive Classes:,class1
Summer_Active,Pass_classes,Classes.passed,Classes Passed:,class1
Summer_Active,Failed_classes,failed.classes,Failed Classes:,class1
Dash_FinAid,Award_Amount,Amount.Awarded,Amount Awarded:,class1
Award_Amount,Refund_Status,Refund.Status,Refund Status:,class1

这个想法是让 PowerShell 遍历 csv 并从中创建正确的 HTML 代码,以创建正确的嵌套。这应该创建以下 HTML 代码:

<div id="Dash_Reg>
<div id="Summer_Active">
<p>Active Classes: <span id="Classes.Active"></span></p>
<p>Inactive Classes: <span id="Classes.Inactive"></span></p>
<div id="Pass_classes">
<p>Passed Classes: <span id="Classes.passed"></span></p>
</div>
<div id="Failed_Classes>
<p>Failed Classes: <span id="failed.classes"></span></p>
</div>
</div>
</div>
<div id="Dash_FinAid>
<div id="Award_Amount>
<p>Amount Awarded: <span id="Amount.Awarded></span></p>
<div id="Refund_Status>
<p>Refund Status: <span id="Refund.Status"></span></p>
</div>
</div>

我写的脚本如下:

import-csv values.csv|group-object -property ParentID|export-csv test.csv
new-item paws.html -type file
$variableset = import-csv test.csv
foreach ($row in $variableset){
new-item (($row.Name) + ".html") -type file
add-content (($row.Name) + ".html") ("<div id='" + ($row.Name) + "'>")
}
$csv = import-csv values.csv
foreach ($row in $csv){
$dataelement = $row.DataElement
$DivID = $row.DivID
$content = $row.content
$parentid = $row.ParentID
$class = $row.Class
add-content (($parentid) + ".html") ("<div id='" + ($DivID) + "'>")
add-content (($parentid) + ".html") ("<div class='" + ($class) + "'>" + ($content)  +<span id='" + ($dataelement) + "'></span></div></div>")
}
add-content paws.html "<html><head><title>PAWS</title><link href='http://rsprodscd.gsu.edu/student-dashboard/assets/css/bootstrap.css' rel='stylesheet'></head><body>"
foreach ($row in $variableset){
$name = $row.Name
add-content (($row.Name) + ".html") "</div>"
get-content (($row.Name) + ".html")|add-content paws.html
rm (($row.Name) + ".html")
}
add-content paws.html "</div>"
add-content paws.html "</body>"
add-content paws.html "</html>"

这将产生以下 HTML 代码:

<html><head><title>PAWS</title><link href='http://rsprodscd.gsu.edu/student-dashboard/assets/css/bootstrap.css' rel='stylesheet'></head><body>
<div id='Dash_Reg'>
<div id='Summer_Active'>
<div class='class1'>Active Classes:<span id='Classes.Active'></span></div></div>
<div id='Summer_Active'>
<div class='class1'>Inactive Classes:<span id='Classes.Inactive'></span></div></div>
</div>
<div id='Summer_Active'>
<div id='Pass_classes'>
<div class='class1'>Classes Passed:<span id='Classes.passed'></span></div></div>
<div id='Failed_classes'>
<div class='class1'>Failed Classes:<span id='failed.classes'></span></div></div>
</div>
<div id='Dash_FinAid'>
<div id='Award_Amount'>
<div class='class1'>Amount Awarded:<span id='Amount.Awarded'></span></div></div>
</div>
<div id='Award_Amount'>
<div id='Refund_Status'>
<div class='class1'>Refund Status:<span id='Refund.Status'></span></div></div>
</div>
</div>
</body>
</html>   

我想要做的是检查 ParentID 是否作为 DivID 在 CSV 中存在,以及它是否确实将该 HTML 代码嵌套在拱形父 ID 下。让我知道这是否有意义。

4

2 回答 2

0

所以这样做的方法是构建一个函数并递归地遍历 csv 文件:

$csv= import-csv values.csv
new-item paws.html -type file
function build_structure
{
                foreach ($row in $csv){                 
if ($row.parentid -eq $args[0]){
add-content paws.html ("<div class='"+$row.divid+"'>")
build_structure($row.divid)
add-content paws.html ("<p class='" + ($row.class) + "'>" + ($row.content) + "<span id='" + ($row.dataelement) + "'></span></p>")
   add-content paws.html ("</div>")                            
}
}
}

build_structure("")
于 2013-06-25T13:09:50.123 回答
0

这会从给定的数据集中产生所需的输出。如果您的输入在文件中,则需要将 convertfrom-csv 更改为 import-csv。所需的输出不会将类 (class1) 显示为输出的一部分。如果需要,您可以通过更改输出的行轻松添加<p>。最后,所需的输出也缺少 a </div>

请注意,此代码不处理您有两个具有相同名称的 DivID,但它们的父级具有不同名称的情况。解决这个问题的最简单方法是确保所有 DivID 都是唯一的。

$csvData = @"
ParentID,DivID,DataElement,Content,Class
Dash_Reg,Summer_Active,Classes.Active,Active Classes:,class1
Dash_Reg,Summer_Active,Classes.Inactive,Inactive Classes:,class1
Summer_Active,Pass_classes,Classes.passed,Classes Passed:,class1
Summer_Active,Failed_classes,failed.classes,Failed Classes:,class1
Dash_FinAid,Award_Amount,Amount.Awarded,Amount Awarded:,class1
Award_Amount,Refund_Status,Refund.Status,Refund Status:,class1
"@

$data = ConvertFrom-Csv $csvData

$divStack = new-object collections.stack

foreach ($item in $data) {
    #
    # Determine if Div or Parent Div is already open
    while ($divStack.count -ne 0 -and 
           $divStack.peek() -ne $item.DivID) {

        #
        # Div is not open. Determine if Parent Div is open
        if ($divStack.peek() -eq $item.ParentID) {
            #
            # Parent Div is open, now open Div
            $divStack.push( $item.DivID)
            "<div id=`"$($item.DivID)`">"
            break;
        } else {
            #
            # Div and Parent Div aren't open, try next one on stack
            $null = $divStack.pop()
            "</div>"
        }
    }

    #
    # Div and Parent Div weren't open. Open them
    if ($divStack.count -eq 0) {
        $divStack.push( $item.ParentID)
        $divStack.push( $item.DivID)
        "<div id=`"$($item.ParentID)`">"
        "<div id=`"$($item.DivID)`">"
    }

    "<p>$($item.Content) <span id=`"$($item.DataElement)`"></span></p>"
}
#
# Close all open Divs
while ($divStack.count) {
    $null = $divStack.pop()
    "</div>"
}
于 2013-06-22T22:54:00.907 回答