From e9b8deccf53b5b0fd78292dede738c1889206f0e Mon Sep 17 00:00:00 2001 From: dfinke Date: Wed, 2 Mar 2016 18:03:00 -0500 Subject: [PATCH] Updated scripts Added -Header parameter and scoped the header variable in the Export-Excel command --- Examples/Nasa/FireBalls.ps1 | 13 +++++++++++++ Export-Excel.ps1 | 12 ++++++------ Get-HtmlTable.ps1 | 11 ++++++----- Import-Html.ps1 | 9 ++++++--- ImportExcel.psd1 | 2 +- PublishToGallery.ps1 | 2 +- 6 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 Examples/Nasa/FireBalls.ps1 diff --git a/Examples/Nasa/FireBalls.ps1 b/Examples/Nasa/FireBalls.ps1 new file mode 100644 index 0000000..d9a0972 --- /dev/null +++ b/Examples/Nasa/FireBalls.ps1 @@ -0,0 +1,13 @@ +$header = echo ` + 'Date/Time - Peak Brightness (UT)' ` + 'Latitude (Deg)' ` + 'Longitude (Deg)' ` + 'Altitude (km)' ` + 'Velocity (km/s)' ` + 'Velocity Components (km/s) vx' ` + 'Velocity Components (km/s) vy' ` + 'Velocity Components (km/s) vz' ` + 'Total Radiated Energy (J)' ` + 'Calculated Total Impact Energy (kt)' + +Import-Html http://neo.jpl.nasa.gov/fireballs/ 5 -Header $header diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 8716389..3b97528 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -134,17 +134,17 @@ function Export-Excel { $Row += 1 } else { - if(!$Header) { + if(!$script:Header) { $ColumnIndex = $StartColumn - $Header = $TargetData.psobject.properties.name + $script:Header = $TargetData.psobject.properties.name if($NoHeader) { # Don't push the headers to the spread sheet $Row -= 1 } else { - foreach ($Name in $Header) { + foreach ($Name in $script:Header) { $ws.Cells[$Row, $ColumnIndex].Value = $name $ColumnIndex += 1 } @@ -154,7 +154,7 @@ function Export-Excel { $Row += 1 $ColumnIndex = $StartColumn - foreach ($Name in $Header) { + foreach ($Name in $script:Header) { $targetCell = $ws.Cells[$Row, $ColumnIndex] @@ -202,7 +202,7 @@ function Export-Excel { $totalColumns=$ws.Dimension.Columns foreach($c in 0..($totalColumns-1)) { - $targetRangeName = "$($Header[$c])" + $targetRangeName = "$($script:Header[$c])" $targetColumn = $c+1 $theCell = $ws.Cells[2,$targetColumn,$totalRows,$targetColumn ] $ws.Names.Add($targetRangeName, $theCell) | Out-Null @@ -225,7 +225,7 @@ function Export-Excel { $csr=$StartRow $csc=$StartColumn $cer=$ws.Dimension.End.Row #-$StartRow+1 - $cec=$Header.Count + $cec=$script:Header.Count $targetRange=$ws.Cells[$csr, $csc, $cer,$cec] diff --git a/Get-HtmlTable.ps1 b/Get-HtmlTable.ps1 index 35c04d8..defbe11 100644 --- a/Get-HtmlTable.ps1 +++ b/Get-HtmlTable.ps1 @@ -2,18 +2,19 @@ function Get-HtmlTable { param( [Parameter(Mandatory=$true)] $url, - $tableIndex=0 + $tableIndex=0, + $Header ) $r = Invoke-WebRequest $url $table = $r.ParsedHtml.getElementsByTagName("table")[$tableIndex] - $propertyNames = @() + $propertyNames=$Header $totalRows=@($table.rows).count - for ($idx = 0; $idx -lt $totalRows; $idx++) { + for ($idx = 1; $idx -lt $totalRows; $idx++) { $row = $table.rows[$idx] - $cells = @($row.cells) + $cells = @($row.cells) if(!$propertyNames) { if($cells[0].tagName -eq 'th') { @@ -22,7 +23,7 @@ function Get-HtmlTable { $propertyNames = @(1..($cells.Count + 2) | % { "P$_" }) } continue - } + } $result = [ordered]@{} diff --git a/Import-Html.ps1 b/Import-Html.ps1 index 83cadd8..32b316e 100644 --- a/Import-Html.ps1 +++ b/Import-Html.ps1 @@ -1,8 +1,10 @@ + function Import-Html { [CmdletBinding()] param( $url, - $index + $index, + $Header ) $xlFile = [System.IO.Path]::GetTempFileName() -replace "tmp","xlsx" @@ -10,6 +12,7 @@ function Import-Html { Write-Verbose "Exporting to Excel file $($xlFile)" - Get-HtmlTable $url $index | - Export-Excel $xlFile -Show -AutoSize + $data = Get-HtmlTable -url $url -tableIndex $index -Header $Header + + $data | Export-Excel $xlFile -Show -AutoSize } \ No newline at end of file diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 7d86a96..3cf7149 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -4,7 +4,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. -ModuleVersion = '2.1.3' +ModuleVersion = '2.1.4' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' diff --git a/PublishToGallery.ps1 b/PublishToGallery.ps1 index e71ce19..1db1b1c 100644 --- a/PublishToGallery.ps1 +++ b/PublishToGallery.ps1 @@ -2,7 +2,7 @@ $p = @{ Name = "ImportExcel" NuGetApiKey = $NuGetApiKey - ReleaseNote = "Added more conditional types" + ReleaseNote = "Add Header param to Import-Html. Scope variabales Export-Excel" } Publish-Module @p \ No newline at end of file