2013-06-12 31 views
0

我目前正在嘗試編寫一個將讀取Excel文件並生成HTML片段的Powershell腳本。我使用它來爲儀表板生成HTML,並且想法是無論嵌套在電子表格中放置了哪些新的div,都不必更改腳本。我真的不知道從哪裏開始。我原來的腳本看起來像下面的腳本,但是每次將新的列或div放入文件時都必須對其進行編輯,這就是爲什麼我無法創建CSV文件?生成HTML片段的Powershell

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

回答

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

除非我把你的代碼放在錯誤的地方,它不起作用。這個想法是根據csv文件中的項目數生成html片段。每個項目都有自己的div。 –

+0

他們在foreach循環中移動div創建線。 –