From ac71736725d12b6c18f878a36d1ad99bc07181bf Mon Sep 17 00:00:00 2001 From: dfinke Date: Mon, 22 Feb 2016 11:27:56 -0500 Subject: [PATCH] Added Import-Html support and examples --- Examples/ImportHtml/DemoGraphics.ps1 | 2 ++ Examples/ImportHtml/PeriodicElements.ps1 | 1 + Examples/ImportHtml/StarTrek.ps1 | 1 + Get-HtmlTable.ps1 | 38 ++++++++++++++++++++++++ Import-Html.ps1 | 15 ++++++++++ ImportExcel.psm1 | 2 ++ Install.ps1 | 19 +++++++++++- InstallModule.ps1 | 32 +++++++++++++++----- README.md | 28 +++++++++-------- 9 files changed, 117 insertions(+), 21 deletions(-) create mode 100644 Examples/ImportHtml/DemoGraphics.ps1 create mode 100644 Examples/ImportHtml/PeriodicElements.ps1 create mode 100644 Examples/ImportHtml/StarTrek.ps1 create mode 100644 Get-HtmlTable.ps1 create mode 100644 Import-Html.ps1 diff --git a/Examples/ImportHtml/DemoGraphics.ps1 b/Examples/ImportHtml/DemoGraphics.ps1 new file mode 100644 index 0000000..011e104 --- /dev/null +++ b/Examples/ImportHtml/DemoGraphics.ps1 @@ -0,0 +1,2 @@ + +Import-Html "http://en.wikipedia.org/wiki/Demographics_of_India" 4 \ No newline at end of file diff --git a/Examples/ImportHtml/PeriodicElements.ps1 b/Examples/ImportHtml/PeriodicElements.ps1 new file mode 100644 index 0000000..50d7cbe --- /dev/null +++ b/Examples/ImportHtml/PeriodicElements.ps1 @@ -0,0 +1 @@ +Import-Html "http://www.science.co.il/PTelements.asp" 1 \ No newline at end of file diff --git a/Examples/ImportHtml/StarTrek.ps1 b/Examples/ImportHtml/StarTrek.ps1 new file mode 100644 index 0000000..a5ca93d --- /dev/null +++ b/Examples/ImportHtml/StarTrek.ps1 @@ -0,0 +1 @@ +Import-Html "https://en.wikipedia.org/wiki/List_of_Star_Trek:_The_Original_Series_episodes" 2 \ No newline at end of file diff --git a/Get-HtmlTable.ps1 b/Get-HtmlTable.ps1 new file mode 100644 index 0000000..8dfb333 --- /dev/null +++ b/Get-HtmlTable.ps1 @@ -0,0 +1,38 @@ +function Get-HtmlTable { + param( + [Parameter(Mandatory=$true)] + $url, + $tableIndex=0 + ) + + $r = (Invoke-WebRequest $url) + $table = $r.ParsedHtml.getElementsByTagName("table")[$tableIndex] + $propertyNames = @() + $totalRows=@($table.rows).count-1 + + for ($idx = 0; $idx -lt $totalRows; $idx++) { + + $row = $table.rows[$idx] + $cells = @($row.cells) + + if(!$propertyNames) { + if($cells[0].tagName -eq 'th') { + $propertyNames = @($cells | foreach {$_.innertext -replace ' ',''}) + } else { + $propertyNames = @(1..($cells.Count + 2) | % { "P$_" }) + } + continue + } + + $result = [ordered]@{} + + for($counter = 0; $counter -lt $cells.Count; $counter++) { + $propertyName = $propertyNames[$counter] + + if(!$propertyName) { $propertyName= '[missing]'} + $result.$propertyName= $cells[$counter].InnerText + } + + [PSCustomObject]$result + } +} \ No newline at end of file diff --git a/Import-Html.ps1 b/Import-Html.ps1 new file mode 100644 index 0000000..6998f02 --- /dev/null +++ b/Import-Html.ps1 @@ -0,0 +1,15 @@ +function Import-Html { + [CmdletBinding()] + param( + $url, + $index + ) + + $xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx" + rm $xlFile -ErrorAction Ignore + + Write-Verbose "Exporting to Excel file $($xlFile)" + + Get-HtmlTable $url $index | + Export-Excel $xlFile -Show -AutoSize +} \ No newline at end of file diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 64cc7a4..45e7b62 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -13,6 +13,8 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\New-PSItem.ps1 . $PSScriptRoot\Pivot.ps1 . $PSScriptRoot\Get-ExcelSheetInfo.ps1 +. $PSScriptRoot\Get-HtmlTable.ps1 +. $PSScriptRoot\Import-Html.ps1 function Import-Excel { param( diff --git a/Install.ps1 b/Install.ps1 index 1e52db1..0ce78d5 100644 --- a/Install.ps1 +++ b/Install.ps1 @@ -1,6 +1,23 @@ param([string]$InstallDirectory) -$fileList = echo EPPlus.dll ImportExcel.psd1 ImportExcel.psm1 Export-Excel.ps1 New-ConditionalFormattingIconSet.ps1 Export-ExcelSheet.ps1 New-ExcelChart.ps1 Invoke-Sum.ps1 InferData.ps1 Get-ExcelColumnName.ps1 Get-XYRange.ps1 Charting.ps1 New-PSItem.ps1 Pivot.ps1 New-ConditionalText.ps1 +$fileList = echo ` + EPPlus.dll ` + ImportExcel.psd1 ` + ImportExcel.psm1 ` + Export-Excel.ps1 ` + New-ConditionalFormattingIconSet.ps1 ` + Export-ExcelSheet.ps1 ` + New-ExcelChart.ps1 ` + Invoke-Sum.ps1 ` + InferData.ps1 ` + Get-ExcelColumnName.ps1 ` + Get-XYRange.ps1 ` + Charting.ps1 ` + New-PSItem.ps1 ` + Pivot.ps1 ` + New-ConditionalText.ps1 ` + Get-HtmlTable.ps1 ` + Import-Html.ps1 if ('' -eq $InstallDirectory) { diff --git a/InstallModule.ps1 b/InstallModule.ps1 index 82f27c5..49ec83f 100644 --- a/InstallModule.ps1 +++ b/InstallModule.ps1 @@ -4,11 +4,27 @@ $TargetPath = "$($ModulePath)\$($ModuleName)" if(!(Test-Path $TargetPath)) { md $TargetPath | out-null} -Get-HtmlTable.ps1 -Import-Html.ps1 - -$FilesToCopy = dir -erroraction ignore *.psm1, *.psd1, *.dll, New-ConditionalFormattingIconSet.ps1, Export-Excel.ps1, Export-ExcelSheet.ps1, New-ExcelChart.ps1, Invoke-Sum.ps1, InferData.ps1, Get-ExcelColumnName.ps1, Get-XYRange.ps1, Charting.ps1, New-PSItem.ps1, Pivot.ps1, Get-ExcelSheetInfo.ps1, New-ConditionalText.ps1 - -$FilesToCopy | ForEach { - Copy-Item -Verbose -Path $_.FullName -Destination "$($TargetPath)\$($_.name)" -} \ No newline at end of file +$targetFiles = echo ` + *.psm1 ` + *.psd1 ` + *.dll ` + New-ConditionalFormattingIconSet.ps1 ` + Export-Excel.ps1 ` + Export-ExcelSheet.ps1 ` + New-ExcelChart.ps1 ` + Invoke-Sum.ps1 ` + InferData.ps1 ` + Get-ExcelColumnName.ps1 ` + Get-XYRange.ps1 ` + Charting.ps1 ` + New-PSItem.ps1 ` + Pivot.ps1 ` + Get-ExcelSheetInfo.ps1 ` + New-ConditionalText.ps1 ` + Get-HtmlTable.ps1 ` + Import-Html.ps1 + +ls $targetFiles | + ForEach { + Copy-Item -Verbose -Path $_.FullName -Destination "$($TargetPath)\$($_.name)" + } \ No newline at end of file diff --git a/README.md b/README.md index d9223cc..326932d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,22 @@ To install in your personal modules folder (e.g. ~\Documents\WindowsPowerShell\M iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfinke/ImportExcel/master/Install.ps1') ``` +Known Issues +- +* Using `-IncludePivotTable`, if that pivot table name exists, you'll get an error. + * Investigating a solution + * *Workaround* delete the Excel file first, then do the export + +What's new + +#### 2/22/2016 +* `Import-Html` leveraged Lee Holmes [Extracting Tables from PowerShell’s Invoke-WebRequest](http://www.leeholmes.com/blog/2015/01/05/extracting-tables-from-powershells-invoke-webrequest/) + + +#### 2/17/2016 +* Added Conditional Text types of `Equal` and `NotEqual` +* Phone #'s like '+33 011 234 34' will be now be handled correctly + ## Try *PassThru* ```powershell @@ -46,18 +62,6 @@ Invoke-Item $file ## Result ![](https://raw.githubusercontent.com/dfinke/ImportExcel/master/images/PassThru.png) -Known Issues -- -* Using `-IncludePivotTable`, if that pivot table name exists, you'll get an error. - * Investigating a solution - * *Workaround* delete the Excel file first, then do the export - -What's new - -#### 2/17/2016 -* Added Conditional Text types of `Equal` and `NotEqual` -* Phone #'s like '+33 011 234 34' will be now be handled correctly - #### 1/18/2016 * Added `Conditional Text Formatting`. [Boe Prox](https://twitter.com/proxb) posted about [HTML Reporting, Part 2: Take Your Reporting a Step Further](https://mcpmag.com/articles/2016/01/14/html-reporting-part-2.aspx) and colorized cells. Great idea, now part of the PowerShell Excel module.