繁体   English   中英

生成HTML片段的Powershell

[英]Powershell that generates HTML Fragments

我目前正在尝试编写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>"

我取决于很多事情,基本上,您需要弄清楚每个文件的逻辑并相应地生成内容。 根据您已经拥有的资源,我可以建议一种较短的方法:

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>"

您可以进一步概括以上内容,但我没有对其进行测试,但imo应该可以工作。

'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>"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM