Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

need help with csv to html table ps script

$
0
0

I have a csv file (test.csv) similar to this:

Title;Status;Type;Category;Contact name;Link
Title1;In progress;Project;Management;Jack Jones;http://www.test.com/
Title2;In progress;Project;Management;Lisa Simpson;http://www.microsoft.com/
Title3;Delayed;Project;Management;Mack Doe;http://www.bing.com/
Title4;Done;Project;Management;Mandy Thompson;http://www.project.com/

It's easy to convert this to HTML table using this powershell command:

Import-Csv -delimiter ';' -Path .\test.csv  | ConvertTo-Html -Head $htmlformat -Body $bodyformat | Out-File .\csv_html_test.html

That's not enough for me though. Before converting csv to html table I want the first column of the csv appear as a html link using the Link column link. This is an example for the first row of the csv:

<a href="http://www.test.com/">Title1</a>;In progress;Project;Management;Jack Jones

After modifying the rows the whole 'Link' column can be removed.

There's my entire script below, but it's not working as expected. Any tips?

Function Set-Links {
	[CmdletBinding()]
		Param(
			[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
			[string]$Line,
			[char]$pos,
			[string]$rightPart,
			[string]$leftPart,
			[string]$link
		)
		Begin {
		}
		Process {
			$Line = $Line.Split(";")
			$pos = $Line[0].IndexOf("=")
			$leftPart = $Line[0].Substring(0, $pos)
			$rightPart = $Line[0].Substring($pos+1)
			$pos = $Line[5].IndexOf("}")
			$link=$Line[5].Substring(0, $pos)
			$pos = $link.IndexOf("=")
			$link=$link.Substring($pos+1)
            $Line[0]=$leftPart + '=' + '<a href"' + $link + '">' + $rightPart + '</a>'
			$Line = [string]::Join(';', $Line)
			Return $Line
		}
}


$htmlformat  = '<title>Table</title>'
$htmlformat += '<style type="text/css">'
$htmlformat += 'BODY{background-color:#663300;color:#FFCC00;font-family:Arial Narrow,sans-serif;font-size:17px;}'
$htmlformat += 'TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;}'
$htmlformat += 'TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#663333}'
$htmlformat += 'TD{border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:#660033}'
$htmlformat += '</style>'
$bodyformat = '<h1>Table</h1>'
Import-Csv -delimiter ';' -Path .\test.csv  | Set-Links | ConvertTo-Html -Head $htmlformat -Body $bodyformat | Out-File .\csv_html_test.html
Invoke-Expression .\csv_html_test.html
Any suggestions how to make this work?


Viewing all articles
Browse latest Browse all 21975

Trending Articles