Url: https://stackoverflow.com/questions/34049127/create-editable-powershell-gui-for-csv-data
answered Sep 10, 2018 at 22:47
[
](https://stackoverflow.com/users/10261541/brian-s)
Brian SBrian S
413
set-strictmode -Version 2.0 | |
function EditCSV($title, $Instructions, $csvPath, $x = 100, $y=100, $Width=600, $Height=400, $SaveChangesToFile=$true, $ReturnStatusOrArray='Status') { | |
#Windows Assemblies | |
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null | |
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null | |
#LoadCSV | |
#Variables MUST have script scope to allow form to see them | |
$script:Updated = $false | |
$script:CsvData = New-Object System.Collections.ArrayList | |
$script:CsvData.AddRange((import-csv $csvPath)) | |
#Helper Functions | |
function paint($form, $ctrl, $TablIndex, $name, $Text, $x, $y, $Width, $Height){ | |
try{$form.Controls.Add($ctrl) }catch{} | |
try{$ctrl.TabIndex = $TablIndex }catch{} | |
try{$ctrl.Text = $Text }catch{} | |
try{$ctrl.name = $name }catch{} | |
try{$ctrl.Location = System_Drawing_Point $x $y }catch{} | |
try{$ctrl.size = System_Drawing_Size $Width $Height }catch{} | |
try{$ctrl.DataBindings.DefaultDataSourceUpdateMode = 0 }catch{} | |
$ctrl | |
} | |
function System_Drawing_Point($x, $Y) {$_ = New-Object System.Drawing.Point; $_.x = $X; $_.Y = $Y; $_} | |
function System_Drawing_Size( $Width, $Height){$_ = New-Object System.Drawing.Size; $_.Width = $Width; $_.Height = $Height; $_} | |
#Paint Form | |
$form1 = paint $null (New-Object System.Windows.Forms.Form) $null 'form1' $Title $x $y $Width $Height | |
$form1.add_Load({ | |
$dataGrid1.DataSource = $script:CsvData; | |
$form1.refresh() | |
}) | |
$label1 = paint $form1 (New-Object System.Windows.Forms.Label) $null "label1" "$Instructions" 12 13 ($width-100) 23 | |
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,2,3,0) | |
$label1.ForeColor = [System.Drawing.Color]::FromArgb(255,0,102,204) | |
$buttonSave = paint $form1 (New-Object System.Windows.Forms.Button) 1 "button1" "Save" ($width-200) ($Height-75) 75 23 | |
$buttonSave.UseVisualStyleBackColor = $True | |
$buttonSave.add_Click({ | |
$script:Updated = $true | |
$Form1.Close() | |
}) | |
$buttonClose = paint $form1 (New-Object System.Windows.Forms.Button) 2 'button2' 'Close' ($width-105) ($Height-75) 75 23 | |
$buttonClose.UseVisualStyleBackColor = $True | |
$buttonClose.add_Click({ | |
$Form1.Close() | |
}) | |
$dataGrid1 = paint $form1 (New-Object System.Windows.Forms.DataGrid) 0 "dataGrid0" $Null 12 40 ($width-40) ($Height-125) | |
$dataGrid1.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0) | |
#Show and Wait till complete | |
$form1.ShowDialog()| Out-Null | |
#Save CSV | |
if( $SaveChangesToFile -eq $true -and $script:Updated ){ | |
$script:CsvData| export-csv -NoTypeInformation -path $csvPath | |
} | |
if( $ReturnStatusOrArray -eq 'Status'){ | |
return $script:Updated | |
}else{ | |
return $script:CsvData | |
} | |
} | |
## Unit Test | |
cls | |
function script:Indent-ConsoleOutput($output, $indent = ' '){ | |
if(!($output -eq $null)){ | |
if(!( $indent -is [string])){ | |
$indent = ''.PadRight($indent) | |
} | |
$width = (Get-Host).UI.RawUI.BufferSize.Width - $indent.length | |
($output| out-string).trim().replace( "`r", "").split("`n").trimend()| %{ | |
for($i=0; $i -le $_.length; $i+=$width){ | |
if(($i+$width) -le $_.length){ | |
"$indent"+$_.substring($i, $width) | |
}else{ | |
"$indent"+$_.substring($i, $_.length - $i) | |
} | |
} | |
} | |
} | |
} | |
Write-Host '## Before '.PadRight(120, '#') | |
$filePath = 'C:\temp\Text.csv' | |
$d = (dir c: |select-object -property Directory, Mode, LastWriteTime, Length, Name)[0..5] | |
$d |export-csv -path $filePath -NoTypeInformation | |
Indent-ConsoleOutput (import-csv $filePath |format-table) 4 | |
Write-Host '## Edit - Save to File '.PadRight(120, '#') | |
Indent-ConsoleOutput (EditCSV 'Example of PS Editable Grid' '[SAVE] - To Save Changes' $filePath ) 4 | |
Write-Host '## After '.PadRight(120, '#') | |
Indent-ConsoleOutput (import-csv $filePath |format-table) 4 | |
Write-Host '## Edit - Return Array '.PadRight(120, '#') | |
Indent-ConsoleOutput (EditCSV 'Example of PS Editable Grid' '[SAVE] - To Save Changes' $filePath -SaveChangesToFile $false -ReturnStatusOrArray 'Array'|format-table) 4 | |
Indent-ConsoleOutput (import-csv $filePath |format-table) 4 |
Url: https://stackoverflow.com/questions/34049127/create-editable-powershell-gui-for-csv-data
answered Sep 10, 2018 at 22:47
[
](https://stackoverflow.com/users/10261541/brian-s)
Brian SBrian S
413