生成HTML片段的Powershell

时间:2013-06-12 13:56:19

标签: html excel powershell dashboard

我目前正在尝试编写一个Powershell脚本,它将读取Excel文件并生成HTML片段。我正在使用它为仪表板生成HTML,并且无论是否由于嵌套而在电子表格上放置了新的div,都不需要更改脚本。我真的不知道从哪里开始。我的原始脚本看起来如下所示,但是在将新列或div放入文件时,必须对其进行编辑,这就是为什么我无法执行CSV文件的原因?

new-item registration.html -type file
add-content registration.html "<html><head></head><body><div id='registration'>"
new-item bill.html -type file
add-content bill.html "<html><head></head><body><div id='bill'>"
new-item financial.html -type file
add-content financial.html "<html><head></head><body><div id='financial'>"
$csv=(import-csv elements.csv)
foreach($row in $csv){
$name=(get-date).ticks
$registration= $row.registration
$bill= $row.bill
$financial= $row.financial
add-content registration.html "<%="
add-content registration.html $registration
add-content registration.html "%>"
add-content bill.html "<%="
add-content bill.html $bill
add-content bill.html "%>"
add-content financial.html "<%="
add-content financial.html $financial
add-content financial.html "%>"
}
add-content registration.html "</div></body></html>"
add-content bill.html "</div></body></html>"
add-content financial.html "</div></body></html>"

1 个答案:

答案 0 :(得分:0)

我依赖很多东西,基本上你需要弄清楚每个文件的逻辑并相应地生成内容。根据您已有的内容,我可以提出一个更短的方法:

add-content registration.html "<html><head></head><body><div id='registration'>"
add-content bill.html "<html><head></head><body><div id='bill'>"
add-content financial.html "<html><head></head><body><div id='financial'>"

import-csv elements.csv | foreach{
    add-content registration.html ('<%={0}%>' -f $_.registration)
    add-content bill.html ('<%={0}%>' -f $_.bill)
    add-content financial.html ('<%={0}%>' -f $_.financial)
}

add-content registration.html,bill.html,financial.html "</div></body></html>"

你可以进一步概括上述内容,我没有测试它,但它应该可以工作。

'registration','bill','financial' | ForEach-Object {

    $page = $_

    add-content "$page.html" "<html><head></head><body><div id='$page'>"

    import-csv elements.csv | foreach{
        add-content "$page.html" ('<%={0}%>' -f $_."$page")
    }

    add-content "$page.html" "</div></body></html>"
}