mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Updated scripts
Added -Header parameter and scoped the header variable in the Export-Excel command
This commit is contained in:
13
Examples/Nasa/FireBalls.ps1
Normal file
13
Examples/Nasa/FireBalls.ps1
Normal file
@@ -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
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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]@{}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user