【问题标题】:Powershell that generates HTML Fragments生成 HTML 片段的 Powershell
【发布时间】:2023-03-09 23:32:01
【问题描述】:

我目前正在尝试编写一个可以读取 Excel 文件并生成 HTML 片段的 Powershell 脚本。我正在使用它为仪表板生成 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>"

【问题讨论】:

    标签: html excel powershell dashboard


    【解决方案1】:

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

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

    【讨论】:

    • 除非我将您的代码放在错误的位置,否则它不起作用。这个想法是根据 csv 文件中的项目数生成 html 片段。每个项目都有自己的 div。
    • 他们在 foreach 循环中移动了 div 创建行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多