Added Import-Html support and examples

This commit is contained in:
dfinke
2016-02-22 11:27:56 -05:00
parent 845855a2bc
commit ac71736725
9 changed files with 117 additions and 21 deletions

View File

@@ -0,0 +1,2 @@
Import-Html "http://en.wikipedia.org/wiki/Demographics_of_India" 4

View File

@@ -0,0 +1 @@
Import-Html "http://www.science.co.il/PTelements.asp" 1

View File

@@ -0,0 +1 @@
Import-Html "https://en.wikipedia.org/wiki/List_of_Star_Trek:_The_Original_Series_episodes" 2

38
Get-HtmlTable.ps1 Normal file
View File

@@ -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
}
}

15
Import-Html.ps1 Normal file
View File

@@ -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
}

View File

@@ -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(

View File

@@ -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)
{

View File

@@ -4,11 +4,27 @@ $TargetPath = "$($ModulePath)\$($ModuleName)"
if(!(Test-Path $TargetPath)) { md $TargetPath | out-null}
Get-HtmlTable.ps1
Import-Html.ps1
$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
$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 {
ls $targetFiles |
ForEach {
Copy-Item -Verbose -Path $_.FullName -Destination "$($TargetPath)\$($_.name)"
}
}

View File

@@ -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 PowerShells 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.