Added JoinWorksheet tests - addition to export-exceltests

This commit is contained in:
jhoneill
2018-07-12 23:58:20 +01:00
parent 2efb2c99f4
commit 87c2ff610d
2 changed files with 176 additions and 73 deletions

View File

@@ -7,62 +7,62 @@ Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { Write-Warning -Message "You need to close Excel before running the tests." ; return}
Describe ExportExcel {
# Context "#Example 1 # Creates and opens a file with the right number of rows and columns" {
# $path = "$env:TEMP\Test.xlsx"
# Remove-item -Path $path -ErrorAction SilentlyContinue
# $processes = Get-Process
# $propertyNames = $Processes[0].psobject.properties.name
# $rowcount = $Processes.Count
# $Processes | Export-Excel $path -show
Context "#Example 1 # Creates and opens a file with the right number of rows and columns" {
$path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue
$processes = Get-Process
$propertyNames = $Processes[0].psobject.properties.name
$rowcount = $Processes.Count
$Processes | Export-Excel $path -show
# it "Created a new file " {
# Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true
# }
it "Created a new file " {
Test-Path -Path $path -ErrorAction SilentlyContinue | Should be $true
}
# it "Started Excel to display the file " {
# Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should not benullorempty
# }
it "Started Excel to display the file " {
Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should not benullorempty
}
# Start-Sleep -Seconds 5 ;
Start-Sleep -Seconds 5 ;
# #Open-ExcelPackage with -Create is tested in Export-Excel
# #This is a test of using it with -KillExcel
# #TODO Need to test opening pre-existing file with no -create switch (and graceful failure when file does not exist) somewhere else
# $Excel = Open-ExcelPackage -Path $path -KillExcel
# it "Killed Excel when Open-Excelpackage was told to " {
# Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should benullorempty
# }
#Open-ExcelPackage with -Create is tested in Export-Excel
#This is a test of using it with -KillExcel
#TODO Need to test opening pre-existing file with no -create switch (and graceful failure when file does not exist) somewhere else
$Excel = Open-ExcelPackage -Path $path -KillExcel
it "Killed Excel when Open-Excelpackage was told to " {
Get-process -Name Excel, xlim -ErrorAction SilentlyContinue | Should benullorempty
}
# it "Created 1 worksheet " {
# $Excel.Workbook.Worksheets.count | Should be 1
# }
it "Created 1 worksheet " {
$Excel.Workbook.Worksheets.count | Should be 1
}
# $ws = $Excel.Workbook.Worksheets[1]
# it "Created the worksheet with the expected name, number of rows and number of columns " {
# $ws.Name | Should be "sheet1"
# $ws.Dimension.Columns | Should be $propertyNames.Count
# $ws.Dimension.Rows | Should be ($rowcount + 1)
# }
$ws = $Excel.Workbook.Worksheets[1]
it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | Should be "sheet1"
$ws.Dimension.Columns | Should be $propertyNames.Count
$ws.Dimension.Rows | Should be ($rowcount + 1)
}
# $headingNames = $ws.cells["1:1"].Value
# it "Created the worksheet with the correct header names " {
# foreach ($p in $propertyNames) {
# $headingnames -contains $p | Should be $true
# }
# }
$headingNames = $ws.cells["1:1"].Value
it "Created the worksheet with the correct header names " {
foreach ($p in $propertyNames) {
$headingnames -contains $p | Should be $true
}
}
# it "Formatted the process StartTime field as 'local short date' " {
# $STHeader = $ws.cells["1:1"].where( {$_.Value -eq "StartTime"})[0]
# $STCell = $STHeader.Address -replace '1$', '2'
# $ws.cells[$stcell].Style.Numberformat.NumFmtID | Should be 22
# }
it "Formatted the process StartTime field as 'local short date' " {
$STHeader = $ws.cells["1:1"].where( {$_.Value -eq "StartTime"})[0]
$STCell = $STHeader.Address -replace '1$', '2'
$ws.cells[$stcell].Style.Numberformat.NumFmtID | Should be 22
}
# it "Formatted the process ID field as 'General' " {
# $IDHeader = $ws.cells["1:1"].where( {$_.Value -eq "ID"})[0]
# $IDCell = $IDHeader.Address -replace '1$', '2'
# $ws.cells[$IDcell].Style.Numberformat.NumFmtID | Should be 0
# }
# }
it "Formatted the process ID field as 'General' " {
$IDHeader = $ws.cells["1:1"].where( {$_.Value -eq "ID"})[0]
$IDCell = $IDHeader.Address -replace '1$', '2'
$ws.cells[$IDcell].Style.Numberformat.NumFmtID | Should be 0
}
}
Context " # NoAliasOrScriptPropeties -ExcludeProperty and -DisplayPropertySet work" {
$path = "$env:TEMP\Test.xlsx"
@@ -151,24 +151,24 @@ Describe ExportExcel {
$path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue
[PSCustOmobject][Ordered]@{
Date = Get-Date
Formula1 = '=SUM(F2:G2)'
String1 = 'My String'
Float = [math]::pi
IPAddress = '10.10.25.5'
StrLeadZero = '07670'
StrComma = '0,26'
StrEngThousand = '1,234.56'
StrEuroThousand = '1.555,83'
StrDot = '1.2'
StrNegInt = '-31'
Date = Get-Date
Formula1 = '=SUM(F2:G2)'
String1 = 'My String'
Float = [math]::pi
IPAddress = '10.10.25.5'
StrLeadZero = '07670'
StrComma = '0,26'
StrEngThousand = '1,234.56'
StrEuroThousand = '1.555,83'
StrDot = '1.2'
StrNegInt = '-31'
StrTrailingNeg = '31-'
StrParens = '(123)'
strLocalCurrency = ('{0}123.45' -f (Get-Culture).NumberFormat.CurrencySymbol )
strOtherCurrency = ('{0}123.45' -f $OtherCurrencySymbol )
StrE164Phone = '+32 (444) 444 4444'
StrAltPhone1 = '+32 4 4444 444'
StrAltPhone2 = '+3244444444'
strOtherCurrency = ('{0}123.45' -f $OtherCurrencySymbol )
StrE164Phone = '+32 (444) 444 4444'
StrAltPhone1 = '+32 4 4444 444'
StrAltPhone2 = '+3244444444'
StrLeadSpace = ' 123'
StrTrailSpace = '123 '
Link1 = [uri]"https://github.com/dfinke/ImportExcel" #2,15
@@ -211,27 +211,27 @@ Describe ExportExcel {
$ws.Cells[2, 22].Hyperlink | Should be "https://github.com/dfinke/ImportExcel"
}
it "Processed thousands according to local settings (Cells H2 and I2) " {
if ((Get-Culture).NumberFormat.NumberGroupSeparator = ",") {
if ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ",") {
($ws.Cells[2, 8].Value -is [valuetype] ) | Should be $true
$ws.Cells[2, 9].Value.GetType().name | Should be 'String'
}
elseif ((Get-Culture).NumberFormat.NumberGroupSeparator = ".") {
elseif ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ".") {
($ws.Cells[2, 9].Value -is [valuetype] ) | Should be $true
$ws.Cells[2, 8].Value.GetType().name | Should be 'String'
}
}
}
it "Processed local currency as a number and other currency as a string (N2 & O2) " {
($ws.Cells[2, 14].Value -is [valuetype] ) | Should be $true
$ws.Cells[2, 15].Value.GetType().name | Should be 'String'
}
}
it "Processed numbers with spaces between digits as strings (P2 & Q2) " {
$ws.Cells[2, 16].Value.GetType().name | Should be 'String'
$ws.Cells[2, 16].Value.GetType().name | Should be 'String'
$ws.Cells[2, 17].Value.GetType().name | Should be 'String'
}
}
it "Processed numbers leading or trailing speaces as Numbers (S2 & T2) " {
($ws.Cells[2, 19].Value -is [valuetype] ) | Should be $true
($ws.Cells[2, 20].Value -is [valuetype] ) | Should be $true
}
}
}
Context "# # Setting cells for different data types with -noHeader" {
@@ -724,7 +724,7 @@ Describe ExportExcel {
$r = Get-ChildItem -path C:\WINDOWS\system32 -File
"Biggest files" | Export-Excel -Path $path -StartRow 1 -StartColumn 7
$r | Sort-Object length -Descending | Select-Object -First 14 Name, @{n="Size";e={$_.Length}} |
$r | Sort-Object length -Descending | Select-Object -First 14 Name, @{n="Size";e={$_.Length}} |
Export-Excel -Path $path -TableName FileSize -StartRow 2 -StartColumn 7 -TableStyle Medium2
$r.extension | Group-Object | Sort-Object -Property count -Descending | Select-Object -First 12 Name, Count |
@@ -732,12 +732,12 @@ Describe ExportExcel {
$r | Group-Object -Property extension | Select-Object Name, @{n="Size"; e={($_.group | Measure-Object -property length -sum).sum}} |
Sort-Object -Property size -Descending | Select-Object -First 10 |
Export-Excel -Path $path -TableName ExtCount -Title "Biggest extensions" -TitleSize 11 -StartColumn 4 -AutoSize
Export-Excel -Path $path -TableName ExtCount -Title "Biggest extensions" -TitleSize 11 -StartColumn 4 -AutoSize
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets[1]
it "Created 3 tables " {
$ws.tables.count | should be 3
$ws.tables.count | should be 3
}
it "Created the FileSize table in the right place with the right size and style " {
$ws.Tables["FileSize"].Address.Address | should be "G2:H16" #Insert at row 2, Column 7, 14 rows x 2 columns of data
@@ -749,7 +749,7 @@ Describe ExportExcel {
it "Created the ExtCount table in the right place with the right size " {
$ws.Tables["ExtCount"].Address.Address | should be "D2:E12" #title, then 10 rows x 2 columns of data
}
}
}

View File

@@ -0,0 +1,103 @@
$data1 = ConvertFrom-Csv -InputObject @"
ID,Product,Quantity,Price,Total
12001,Nails,37,3.99,147.63
12002,Hammer,5,12.10,60.5
12003,Saw,12,15.37,184.44
12010,Drill,20,8,160
12011,Crowbar,7,23.48,164.36
"@
$data2 = ConvertFrom-Csv -InputObject @"
ID,Product,Quantity,Price,Total
12001,Nails,53,3.99,211.47
12002,Hammer,6,12.10,72.60
12003,Saw,10,15.37,153.70
12010,Drill,10,8,80
12012,Pliers,2,14.99,29.98
"@
$data3 = ConvertFrom-Csv -InputObject @"
ID,Product,Quantity,Price,Total
12001,Nails,20,3.99,79.80
12002,Hammer,2,12.10,24.20
12010,Drill,11,8,88
12012,Pliers,3,14.99,44.97
"@
Describe "Join Worksheet" {
BeforeAll {
$path = "$Env:TEMP\test.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
$data1 | Export-Excel -Path $path -WorkSheetname Oxford
$data2 | Export-Excel -Path $path -WorkSheetname Abingdon
$data3 | Export-Excel -Path $path -WorkSheetname Banbury
$ptdef = New-PivotTableDefinition -PivotTableName "Summary" -PivotRows "Store" -PivotColumns "Product" -PivotData @{"Total"="SUM"} -IncludePivotChart -ChartTitle "Sales Breakdown" -ChartType ColumnStacked -ChartColumn 10
Join-Worksheet -Path $path -WorkSheetName "Total" -Clearsheet -FromLabel "Store" -TableName "Summary" -TableStyle Light1 -AutoSize -BoldTopRow -FreezePane 2,1 -Title "Store Sales Summary" -TitleBold -TitleSize 14 -PivotTableDefinition $ptdef
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets["Total"]
$pt = $excel.Workbook.Worksheets["Summary"].pivottables[0]
$pc = $excel.Workbook.Worksheets["Summary"].Drawings[0]
}
Context "Merge 3 blocks" {
it "Created sheet of the right size with a title and a table " {
$ws.Dimension.Address | Should be "A1:F16"
$ws.Tables[0].Address.Address | Should be "A2:F16"
$ws.cells["A1"].Value | Should be "Store Sales Summary"
$ws.cells["A1"].Style.Font.Size | Should be 14
$ws.Tables[0].StyleName | Should be "TableStyleLight1"
$ws.cells["A2:F2"].Style.Font.Bold | Should be $True
}
it "Added a from column with the right heading " {
$ws.cells["F2" ].Value | Should be "Store"
$ws.cells["F3" ].Value | Should be "Oxford"
$ws.cells["F8" ].Value | Should be "Abingdon"
$ws.cells["F13"].Value | Should be "Banbury"
}
it "Filled in the data " {
$ws.cells["C3" ].Value | Should be $data1[0].quantity
$ws.cells["C8" ].Value | Should be $data2[0].quantity
$ws.cells["C13"].Value | Should be $data3[0].quantity
}
it "Created the pivot table " {
$pt | Should not beNullOrEmpty
$pt.StyleName | Should be "PivotStyleMedium9"
$pt.RowFields[0].Name | Should be "Store"
$pt.ColumnFields[0].name | Should be "Product"
$pt.DataFields[0].name | Should be "Sum of Total"
$pc.ChartType | Should be "ColumnStacked"
$pc.Title.text | Should be "Sales Breakdown"
}
}
$path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue
Get-WmiObject -Class win32_logicaldisk |
Select-Object -Property DeviceId,VolumeName, Size,Freespace |
Export-Excel -Path $path -WorkSheetname Volumes -NumberFormat "0,000"
Get-NetAdapter |
Select-Object -Property Name,InterfaceDescription,MacAddress,LinkSpeed |
Export-Excel -Path $path -WorkSheetname NetAdapters
Join-Worksheet -Path $path -HideSource -WorkSheetName Summary -NoHeader -LabelBlocks -AutoSize -Title "Summary" -TitleBold -TitleSize 22
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets["Summary"]
Context "3 Unlinked blocks" {
it "Hid the source worksheets " {
$excel.Workbook.Worksheets[1].Hidden.tostring() | should be "Hidden"
$excel.Workbook.Worksheets[2].Hidden.tostring() | should be "Hidden"
}
it "Created the Summary sheet with title, and block labels, and copied the correct data " {
$ws.Cells["A1"].Value | should be "Summary"
$ws.Cells["A2"].Value | should be $excel.Workbook.Worksheets[1].name
$ws.Cells["A3"].Value | should be $excel.Workbook.Worksheets[1].Cells["A1"].value
$ws.Cells["A4"].Value | should be $excel.Workbook.Worksheets[1].Cells["A2"].value
$ws.Cells["B4"].Value | should be $excel.Workbook.Worksheets[1].Cells["B2"].value
$nextRow = $excel.Workbook.Worksheets[1].Dimension.Rows + 3
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].name
$nextRow ++
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["A1"].value
$nextRow ++
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["A2"].value
$ws.Cells["B$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["B2"].value
}
}
}