mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
New examples incl. Pester to XLSx
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectComparisonWithNull','',Justification='Intentional use to select non null array items')]
|
||||||
|
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Position=0)]
|
[Parameter(Position=0)]
|
||||||
[string]$XLFile,
|
[string]$XLFile,
|
||||||
@@ -49,6 +50,7 @@ if (-not $UseExisting) {
|
|||||||
$InvokePesterParams.Remove('WorkSheetName')
|
$InvokePesterParams.Remove('WorkSheetName')
|
||||||
Invoke-Pester @InvokePesterParams
|
Invoke-Pester @InvokePesterParams
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-not (Test-Path -Path $InvokePesterParams['OutputFile'])) {
|
if (-not (Test-Path -Path $InvokePesterParams['OutputFile'])) {
|
||||||
throw "Could not output file $($InvokePesterParams['OutputFile'])"; return
|
throw "Could not output file $($InvokePesterParams['OutputFile'])"; return
|
||||||
}
|
}
|
||||||
@@ -98,7 +100,7 @@ $testResults = foreach ($test in $resultXML.'test-suite'.results.'test-suite') {
|
|||||||
Duration = $_.time
|
Duration = $_.time
|
||||||
File = $testPs1File; Group = $Describe
|
File = $testPs1File; Group = $Describe
|
||||||
SubGroup = $Context ; Name =($_.Description -replace '\s{2,}', ' ')
|
SubGroup = $Context ; Name =($_.Description -replace '\s{2,}', ' ')
|
||||||
Result = $_.result ; FullDesc = '=Group&" "&SubGroup&" "&Test'})
|
Result = $_.result ; FullDesc = '=Group&" "&SubGroup&" "&Name'})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,7 +126,6 @@ if (-not $testResults) {Write-Warning 'No Results found' ; return}
|
|||||||
$clearSheet = -not $Append
|
$clearSheet = -not $Append
|
||||||
$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet:$clearSheet -Append:$append -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange
|
$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet:$clearSheet -Append:$append -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange
|
||||||
$ws = $excel.Workbook.Worksheets[$WorkSheetName]
|
$ws = $excel.Workbook.Worksheets[$WorkSheetName]
|
||||||
|
|
||||||
<# Worksheet should look like ..
|
<# Worksheet should look like ..
|
||||||
|A |B |C D |E |F |G |H |I |J |K |L |M
|
|A |B |C D |E |F |G |H |I |J |K |L |M
|
||||||
1|Machine |OS |Date Time |Executed |Success |Duration |File |Group |SubGroup |Name |Result |FullDescription
|
1|Machine |OS |Date Time |Executed |Success |Duration |File |Group |SubGroup |Name |Result |FullDescription
|
||||||
|
|||||||
21
Examples/PsGallery.ps1
Normal file
21
Examples/PsGallery.ps1
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
$top1000 = foreach ($p in 1..50) {
|
||||||
|
$c = Invoke-WebRequest -Uri "https://www.powershellgallery.com/packages" -method Post -Body "q=&sortOrder=package-download-count&page=$p"
|
||||||
|
[regex]::Matches($c.Content,'<table class="width-hundred-percent">.*?</table>', [System.Text.RegularExpressions.RegexOptions]::Singleline) | foreach {
|
||||||
|
$name = [regex]::Match($_, "(?<=<h1><a href=.*?>).*(?=</a></h1>)").value
|
||||||
|
$n = [regex]::replace($_,'^.*By:\s*<li role="menuitem">','', [System.Text.RegularExpressions.RegexOptions]::Singleline)
|
||||||
|
$n = [regex]::replace($n,'</div>.*$','', [System.Text.RegularExpressions.RegexOptions]::Singleline)
|
||||||
|
$by = [regex]::match($n,'(?<=">).*(?=</a>)').value
|
||||||
|
$qty = [regex]::match($n,'\S*(?= downloads)').value
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Name = $name
|
||||||
|
by = $by
|
||||||
|
Downloads = $qty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
del "~\Documents\gallery.xlsx"
|
||||||
|
$pivotdef = New-PivotTableDefinition -PivotTableName 'Summary' -PivotRows by -PivotData @{name="Count"
|
||||||
|
Downloads="Sum"} -PivotDataToColumn -Activate -ChartType ColumnClustered -PivotNumberFormat '#,###'
|
||||||
|
$top1000 | export-excel -path '~\Documents\gallery.xlsx' -Numberformat '#,###' -PivotTableDefinition $pivotdef -TableName 'TopDownloads' -Show
|
||||||
@@ -1,62 +1,78 @@
|
|||||||
$excelpath = "$env:temp\subtotal.xlsx"
|
$Data = ConvertFrom-Csv @'
|
||||||
$SheetName = 'Sheet1'
|
|
||||||
$SubtotalFieldName = 'Net'
|
|
||||||
$GroupByFieldName = 'City'
|
|
||||||
$SubtotalFormula = '=SUBTOTAL(3,D{0}:D{1})' # {0} and {1} are placeholders for the first and last row. D is the column to total in
|
|
||||||
# 1=AVERAGE; 2=COUNT; 3=COUNTA; 4=MAX; 5=MIN; 6=PRODUCT; 7=STDEV; 8=STDEVP; 9=SUM; 10=VAR; 11=VARP add 100 to ignore hidden values
|
|
||||||
|
|
||||||
Remove-Item -Path $excelpath -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
$Data = ConvertFrom-Csv @'
|
|
||||||
Product, City, Gross, Net
|
Product, City, Gross, Net
|
||||||
Apple, London , 300, 250
|
Apple, London , 300, 250
|
||||||
Orange, London , 400, 350
|
Orange, London , 400, 350
|
||||||
Banana, London , 300, 200
|
Banana, London , 300, 200
|
||||||
|
Grape, Munich, 100, 100
|
||||||
Orange, Paris, 600, 500
|
Orange, Paris, 600, 500
|
||||||
Banana, Paris, 300, 200
|
Banana, Paris, 300, 200
|
||||||
Apple, New York, 1200,700
|
Apple, New York, 1200,700
|
||||||
'@
|
'@
|
||||||
|
$ExcelPath = "$env:temp\subtotal.xlsx"
|
||||||
|
$SheetName = 'Sheet1'
|
||||||
|
Remove-Item -Path $ExcelPath -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
|
||||||
|
$GroupByFieldName = 'City'
|
||||||
|
$TotalSingleRows = $false
|
||||||
|
$GrandTotal = $false
|
||||||
|
$SubtotalRowHeight = 0 #If non zero will set subtotals to this height
|
||||||
|
$Subtotals =@{ 'Net' = {"=SUBTOTAL(3,D{0}:D{1})" -f $from, $to}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
$SubtotalFieldName = 'Net'
|
||||||
|
|
||||||
|
$SubtotalFormula = '=SUBTOTAL(3,D{0}:D{1})' # {0} and {1} are placeholders for the first and last row. D is the column to total in
|
||||||
|
# 1=AVERAGE; 2=COUNT; 3=COUNTA; 4=MAX; 5=MIN; 6=PRODUCT; 7=STDEV; 8=STDEVP; 9=SUM; 10=VAR; 11=VARP add 100 to ignore hidden values
|
||||||
|
|
||||||
#at each change in the Group by field, insert a subtotal (count) formula in the title column & send to excel - list those rows and make them half height after export
|
#at each change in the Group by field, insert a subtotal (count) formula in the title column & send to excel - list those rows and make them half height after export
|
||||||
$currentRow = 2
|
$currentRow = 2
|
||||||
$lastChangeRow = 2
|
$lastChangeRow = 2
|
||||||
$insertedRows = @()
|
$insertedRows = @()
|
||||||
$hideRows = @()
|
#$hideRows = @()
|
||||||
$lastValue = $Data[0].$GroupByFieldName
|
$lastValue = $Data[0].$GroupByFieldName
|
||||||
$excel = $Data | ForEach-Object -Process {
|
$excel = $Data | ForEach-Object -Process {
|
||||||
if ($_.$GroupByFieldName -ne $lastvalue) {
|
if ($_.$GroupByFieldName -ne $lastvalue) {
|
||||||
$Formula = $SubtotalFormula -f $lastChangeRow, ($currentrow - 1)
|
if ($lastChangeRow -lt ($currentrow - 1) -or $totalSingleRows) {
|
||||||
if ($lastChangeRow -eq ($currentrow - 1)) {$hideRows += $CurrentRow }
|
$formula = $SubtotalFormula -f $lastChangeRow, ($currentrow - 1)
|
||||||
else {$insertedRows += $CurrentRow }
|
$insertedRows += $currentRow
|
||||||
[pscustomobject]@{$SubtotalFieldName=$Formula}
|
[pscustomobject]@{$SubtotalFieldName = $formula}
|
||||||
$currentRow += 1
|
$currentRow += 1
|
||||||
$lastChangeRow = $currentRow
|
}
|
||||||
$lastValue = $_.$GroupByFieldName
|
$lastChangeRow = $currentRow
|
||||||
|
$lastValue = $_.$GroupByFieldName
|
||||||
}
|
}
|
||||||
$_
|
$_
|
||||||
$currentRow += 1
|
$currentRow += 1
|
||||||
} -end {
|
} -end {
|
||||||
$Formula = $SubtotalFormula -f $lastChangeRow, ($currentrow - 1)
|
$formula = $SubtotalFormula -f $lastChangeRow, ($currentrow - 1)
|
||||||
[pscustomobject]@{$SubtotalFieldName=$Formula}
|
[pscustomobject]@{$SubtotalFieldName=$formula}
|
||||||
} | Export-Excel -Path $excelpath -PassThru -AutoSize -AutoFilter -BoldTopRow -WorksheetName $SheetName
|
if ($GrandTotal) {
|
||||||
|
$formula = $SubtotalFormula -f $lastChangeRow, ($currentrow - 1)
|
||||||
|
[pscustomobject]@{$SubtotalFieldName=$formula}
|
||||||
|
}
|
||||||
|
} | Export-Excel -Path $ExcelPath -PassThru -AutoSize -AutoFilter -BoldTopRow -WorksheetName $sheetName
|
||||||
|
|
||||||
#We kept a lists of the total rows Since 1 rows won't get expand/collapse hide them.
|
#We kept a lists of the total rows. Since single rows won't get expanded/collapsed hide them.
|
||||||
#foreach ($r in $insertedrows) { $excel.WorkItems.Row($r).Height = 8}
|
if ($subtotalrowHeight) {
|
||||||
foreach ($r in $hideRows) { $excel.$SheetName.Row($r).hidden = $true}
|
foreach ($r in $insertedrows) { $excel.WorkItems.Row($r).Height = $SubtotalRowHeight}
|
||||||
|
}
|
||||||
|
#foreach ($r in $hideRows) { $excel.$SheetName.Row($r).hidden = $true}
|
||||||
$range = $excel.$SheetName.Dimension.Address
|
$range = $excel.$SheetName.Dimension.Address
|
||||||
$SheetIndex = $excel.Sheet1.Index
|
$sheetIndex = $excel.Sheet1.Index
|
||||||
Close-ExcelPackage -ExcelPackage $excel
|
Close-ExcelPackage -ExcelPackage $excel
|
||||||
|
|
||||||
try { $excelApp = New-Object -ComObject "Excel.Application" }
|
try { $excelApp = New-Object -ComObject "Excel.Application" }
|
||||||
catch { Write-Warning "Could not start Excel application - which usually means it is not installed." ; return }
|
catch { Write-Warning "Could not start Excel application - which usually means it is not installed." ; return }
|
||||||
|
|
||||||
try { $excelWorkBook = $excelApp.Workbooks.Open($excelpath) }
|
try { $excelWorkBook = $excelApp.Workbooks.Open($ExcelPath) }
|
||||||
catch { Write-Warning -Message "Could not Open $excelpath." ; return }
|
catch { Write-Warning -Message "Could not Open $ExcelPath." ; return }
|
||||||
$ws = $excelWorkBook.Worksheets.Item($SheetIndex)
|
$ws = $excelWorkBook.Worksheets.Item($sheetIndex)
|
||||||
$null = $ws.Range($range).Select()
|
$null = $ws.Range($range).Select()
|
||||||
$null = $excelapp.Selection.AutoOutline()
|
$null = $excelapp.Selection.AutoOutline()
|
||||||
$excelWorkBook.Save()
|
$excelWorkBook.Save()
|
||||||
$excelWorkBook.Close()
|
$excelWorkBook.Close()
|
||||||
$excelApp.Quit()
|
$excelApp.Quit()
|
||||||
|
|
||||||
Start $excelpath
|
Start-Process $ExcelPath
|
||||||
Reference in New Issue
Block a user