简体   繁体   中英

Powershell that generates HTML Fragments

I am currently trying to write a Powershell Script that will read an Excel File and generate HTML fragments. I am using it to generate HTML for a dashboard and the idea is that the script will not have to be changed no matter what new divs are put on the spread sheet due to nesting. I really have no idea where to start. My original script looks like the one below, but it would have to be edited any time a new column or div was put into the file which is why I can not do a CSV file?

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

I depends on many things and basically you'll need to figure out the logic for each file and generate the content accordingly. Based on what you already have, I can suggest a shorter way:

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

You can generalize the above even more, I didn't test it but imo it should work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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