Skip to content

Instantly share code, notes, and snippets.

@JeffMelton
Created August 11, 2017 13:30
Show Gist options
  • Save JeffMelton/7f20aebe3abcfe75e742836e667cd63c to your computer and use it in GitHub Desktop.
Save JeffMelton/7f20aebe3abcfe75e742836e667cd63c to your computer and use it in GitHub Desktop.
converts CSV mileage report from TripLog to Excel for our office manager
param (
[string]$inputFilename = $args[0]
)
Copy-Item -Path $inputFilename -Destination "$inputFilename.bak"
$lineNumber = ( Select-String "$inputFilename" -Pattern "Locations" | Select-Object -ExpandProperty LineNumber ) - 3
$fileContent = ( Get-Content $inputFilename )[0..$lineNumber]
$fileContent | Skip-Object -Index (0..20) > $inputFilename
$newCsv = Import-Csv $inputFilename -Delimiter ',' |
Select-Object 'Date',@{Name='Begin';Expression={$_.'Beginning Odometer'}},@{Name='End';Expression={$_.'Ending Odometer'}},`
@{Name='Mileage';Expression={$_.'Mileage (mi)'}},@{Name='Customer';Expression={$_.'Tags'}}
$total = ($newCsv | Measure-Object 'Mileage' -Sum).Sum
$newRow = New-Object PSObject -Property @{Mileage="$total";End="Total"}
$newCsv | Export-Csv $inputFilename
Export-Csv $inputFilename -InputObject $newRow -Append -Force
$outputPath = "C:\Users\jeff\Downloads\"
$monthName = (Get-Date).AddMonths(-1).ToString("MMMM")
$outputFilename = "$monthName Mileage.xlsx"
$fullOutputPath = "$outputPath$outputFilename"
$newCsv = Import-Csv $inputFilename -Delimiter ','
$newCsv | Export-XLSX -Path $fullOutputPath -AutoFit -WorksheetName "$monthName Mileage"
$excel = New-Excel -Path $fullOutputPath
$totalCell = Search-CellValue -Excel $excel { $_ -eq "Total" }
$excel | Get-Worksheet | Format-Cell -Header -Bold $true
$excel | Get-Worksheet | Format-Cell -StartRow $totalCell.Row -StartColumn $totalCell.Column -EndRow $totalCell.Row -EndColumn ($totalCell.Column + 1) -Bold $true -Underline $true
#$excel | Get-Worksheet | Format-Cell -StartRow 2 -StartColumn 1 -NumberFormat "Datetime"
#$excel | Get-Worksheet | Format-Cell -StartRow 2 -StartColumn 2 -EndRow ($totalCell.Row - 1) -EndColumn ($totalCell.Column + 1) -NumberFormat "single"
#$excel | Get-Worksheet | Format-Cell -StartRow $totalCell.Row -StartColumn ($totalCell.Column + 1) -NumberFormat "decimal"
$excel | Close-Excel -Save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment