簡體   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