From 13a09a8a9916c21b60aca5285f1df41796127929 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 30 Jun 2018 21:32:09 -0400 Subject: [PATCH] updated path for psd1 --- .../ConvertFromExcelToSQLInsert.tests.ps1 | 2 +- __tests__/Export-Excel.Tests.ps1 | 414 +++++++++--------- 2 files changed, 208 insertions(+), 208 deletions(-) diff --git a/__tests__/ConvertFromExcelToSQLInsert.tests.ps1 b/__tests__/ConvertFromExcelToSQLInsert.tests.ps1 index 1b6a31b..ec9355b 100644 --- a/__tests__/ConvertFromExcelToSQLInsert.tests.ps1 +++ b/__tests__/ConvertFromExcelToSQLInsert.tests.ps1 @@ -1,4 +1,4 @@ -Import-Module .\ImportExcel.psd1 -Force +Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force $xlFile = ".\testSQL.xlsx" diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index 1a19d6b..4e27a13 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -1,56 +1,56 @@ #Requires -Modules Pester -$here = Split-Path -Parent $MyInvocation.MyCommand.Path - -Import-Module $here -Force -Verbose +# $here = Split-Path -Parent $MyInvocation.MyCommand.Path +# Import-Module $here -Force -Verbose +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} +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 + + 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 - + $Processes | Export-Excel $path -show + it "Created a new file " { - Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true + 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 - } - - 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 + 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 } - 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.Dimension.Rows | should be ($rowcount + 1) } - - $headingNames = $ws.cells["1:1"].Value + + $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' @@ -65,89 +65,89 @@ Describe ExportExcel { } Context " # NoAliasOrScriptPropeties -ExcludeProperty and -DisplayPropertySet work" { - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue $processes = Get-Process $propertyNames = $Processes[0].psobject.properties.where( {$_.MemberType -eq 'Property'}).name $rowcount = $Processes.Count - #TestCreating a range with a name which needs illegal chars removing + #TestCreating a range with a name which needs illegal chars removing $warnVar = $null $Processes | Export-Excel $path -NoAliasOrScriptPropeties -RangeName "No Spaces" -WarningVariable warnvar -WarningAction SilentlyContinue - - $Excel = Open-ExcelPackage -Path $path + + $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] it "Created a new file with alias & Script Properties removed. " { $ws.Name | should be "sheet1" $ws.Dimension.Columns | should be $propertyNames.Count - $ws.Dimension.Rows | should be ($rowcount + 1 ) # +1 for the header. + $ws.Dimension.Rows | should be ($rowcount + 1 ) # +1 for the header. } it "Created a Range - even though the name given was invalid. " { $ws.Names["No_spaces"] | should not beNullOrEmpty $ws.Names["No_spaces"].End.Column | should be $propertyNames.Count - $ws.names["No_spaces"].End.Row | should be ($rowcount + 1 ) # +1 for the header. - $warnVar.Count | should be 1 + $ws.names["No_spaces"].End.Row | should be ($rowcount + 1 ) # +1 for the header. + $warnVar.Count | should be 1 } - #This time use clearsheet instead of deleting the file + #This time use clearsheet instead of deleting the file $Processes | Export-Excel $path -NoAliasOrScriptPropeties -ExcludeProperty SafeHandle, modules, MainModule, StartTime, Threads -ClearSheet - + $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] it "Created a new file with a further 5 properties excluded and cleared the old sheet " { $ws.Name | should be "sheet1" - $ws.Dimension.Columns | should be ($propertyNames.Count - 5) - $ws.Dimension.Rows | should be ($rowcount + 1) # +1 for the header + $ws.Dimension.Columns | should be ($propertyNames.Count - 5) + $ws.Dimension.Rows | should be ($rowcount + 1) # +1 for the header } $propertyNames = $Processes[0].psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames - Remove-item -Path $path -ErrorAction SilentlyContinue + Remove-item -Path $path -ErrorAction SilentlyContinue $Processes | Export-Excel $path -DisplayPropertySet - - $Excel = Open-ExcelPackage -Path $path + + $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] it "Created a new file with just the members of the Display Property Set " { $ws.Name | should be "sheet1" $ws.Dimension.Columns | should be $propertyNames.Count - $ws.Dimension.Rows | should be ($rowcount + 1) + $ws.Dimension.Rows | should be ($rowcount + 1) } - } + } - Context "#Example 2 # Exports a list of numbers and applies number format " { - - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue - #testing -ReturnRange switch + Context "#Example 2 # Exports a list of numbers and applies number format " { + + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue + #testing -ReturnRange switch $returnedRange = Write-Output -1 668 34 777 860 -0.5 119 -0.1 234 788 | Export-Excel -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00' -Path $path -ReturnRange it "Created a new file and returned the expected range " { - Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true + Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true $returnedRange | should be "A1:A10" } - - $Excel = Open-ExcelPackage -Path $path - it "Created 1 worksheet " { - $Excel.Workbook.Worksheets.count | should be 1 + + $Excel = Open-ExcelPackage -Path $path + 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 1 - $ws.Dimension.Rows | should be 10 + $ws.Dimension.Rows | should be 10 } - + it "Set the default style for the sheet as expected " { $ws.cells.Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00' } - + it "Set the default style and value for Cell A1 as expected " { $ws.cells[1,1].Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00' $ws.cells[1,1].Value | should be -1 } } - Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" { - - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue + Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" { + + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue [PSCustOmobject][Ordered]@{ Date = Get-Date Formula1 = '=SUM(F2:G2)' @@ -165,28 +165,28 @@ Describe ExportExcel { Link = [uri]"https://github.com/dfinke/ImportExcel" } | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path it "Created a new file " { - Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true + Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true } - - $Excel = Open-ExcelPackage -Path $path - it "Created 1 worksheet " { - $Excel.Workbook.Worksheets.count | should be 1 + + $Excel = Open-ExcelPackage -Path $path + 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 14 - $ws.Dimension.Rows | should be 2 + $ws.Dimension.Rows | should be 2 } - + it "Set a date in Cell A2 " { $ws.Cells[2,1].Value.Gettype().name | should be 'DateTime' } it "Set a formula in Cell B2 " { $ws.Cells[2,2].Formula | should be '=SUM(F2:G2)' - } + } it "Set strings in Cells E2 and F2 " { $ws.Cells[2,5].Value.GetType().name | should be 'String' @@ -195,18 +195,18 @@ Describe ExportExcel { it "Set a number in Cell I2 " { ($ws.Cells[2,9].Value -is [valuetype] ) | should be $true - } - + } + it "Set a hyperlink in Cell N2 " { $ws.Cells[2,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel" - } + } } - Context "# # Setting cells for different data types with -noHeader" { - - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue + Context "# # Setting cells for different data types with -noHeader" { + + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue [PSCustOmobject][Ordered]@{ Date = Get-Date Formula1 = '=SUM(F1:G1)' @@ -224,28 +224,28 @@ Describe ExportExcel { Link = [uri]"https://github.com/dfinke/ImportExcel" } | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path -NoHeader it "Created a new file " { - Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true + Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true } - - $Excel = Open-ExcelPackage -Path $path - it "Created 1 worksheet " { - $Excel.Workbook.Worksheets.count | should be 1 + + $Excel = Open-ExcelPackage -Path $path + 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 14 - $ws.Dimension.Rows | should be 1 + $ws.Dimension.Rows | should be 1 } - + it "Set a date in Cell A1 " { $ws.Cells[1,1].Value.Gettype().name | should be 'DateTime' } it "Set a formula in Cell B1 " { $ws.Cells[1,2].Formula | should be '=SUM(F1:G1)' - } + } it "Set strings in Cells E1 and F1 " { $ws.Cells[1,5].Value.GetType().name | should be 'String' @@ -254,58 +254,58 @@ Describe ExportExcel { it "Set a number in Cell I1 " { ($ws.Cells[1,9].Value -is [valuetype] ) | should be $true - } - + } + it "Set a hyperlink in Cell N1 " { $ws.Cells[1,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel" - } + } } - Context "#Example 5 # Adding a single conditional format " { - ### TODO New-ConditionalText doesn't a lot of options in Add-ConditionalFormat. - # It would be good to pull the logic out of Export-Excel and have EE call Add-ConditionalFormat. + Context "#Example 5 # Adding a single conditional format " { + ### TODO New-ConditionalText doesn't a lot of options in Add-ConditionalFormat. + # It would be good to pull the logic out of Export-Excel and have EE call Add-ConditionalFormat. $ct = New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor DarkRed -BackgroundColor LightPink it "Created a Conditional format description " { $ct.BackgroundColor -is [System.Drawing.Color] | should be $true $ct.ConditionalTextColor -is [System.Drawing.Color] | should be $true - $ct.ConditionalType -in [enum]::GetNames( [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType] ) | + $ct.ConditionalType -in [enum]::GetNames( [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType] ) | should be $true } - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue - Write-Output 489 668 299 777 860 151 119 497 234 788 | Export-Excel -Path $path -ConditionalText $ct - + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue + Write-Output 489 668 299 777 860 151 119 497 234 788 | Export-Excel -Path $path -ConditionalText $ct + it "Created a new file " { - Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true + Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true } - + #ToDo need to test applying conitional formatting to a pre-existing worksheet - $Excel = Open-ExcelPackage -Path $path + $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] it "Added one block of conditional formating for the data range " { $ws.ConditionalFormatting.Count | should be 1 - $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) + $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) } - - $cf = $ws.ConditionalFormatting[0] + + $cf = $ws.ConditionalFormatting[0] it "Set the conditional formatting properties correctly " { $cf.Formula | should be $ct.Text $cf.Type.ToString() | should be $ct.ConditionalType #$cf.Style.Fill.BackgroundColor | should be $ct.BackgroundColor # $cf.Style.Font.Color | should be $ct.ConditionalTextColor - have to compare r.g.b - + } } Context "#Example 6 # Adding multiple conditional formats using short form syntax. " { - #this is a test of adding more than one conditional block and using the minimal syntax for new-ConditionalText = - $path = "$env:TEMP\Test.xlsx" - Remove-item -Path $path -ErrorAction SilentlyContinue - - #Testing -Passthrough + #this is a test of adding more than one conditional block and using the minimal syntax for new-ConditionalText = + $path = "$env:TEMP\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue + + #Testing -Passthrough $Excel = Get-Service | Select-Object Name, Status, DisplayName, ServiceName | Export-Excel $path -PassThru -ConditionalText $( New-ConditionalText Stop DarkRed LightPink @@ -314,15 +314,15 @@ Describe ExportExcel { $ws = $Excel.Workbook.Worksheets[1] it "Added two blocks of conditional formating for the data range " { $ws.ConditionalFormatting.Count | should be 2 - $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) - $ws.ConditionalFormatting[1].Address | should be ($ws.Dimension.Address) + $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) + $ws.ConditionalFormatting[1].Address | should be ($ws.Dimension.Address) } it "Set the conditional formatting properties correctly " { $ws.ConditionalFormatting[0].Text | should be "Stop" $ws.ConditionalFormatting[1].Text | should be "Running" $ws.ConditionalFormatting[0].Type | should be "ContainsText" $ws.ConditionalFormatting[1].Type | should be "ContainsText" - #Add RGB Comparison + #Add RGB Comparison } Close-ExcelPackage -ExcelPackage $Excel } @@ -349,7 +349,7 @@ Describe ExportExcel { } $Array = $Obj1, $Obj2, $Obj3 - $newarray = $Array | Update-FirstObjectProperties + $newarray = $Array | Update-FirstObjectProperties it "Outputs as many objects as it input " { $newarray.Count | should be $Array.Count } @@ -359,66 +359,66 @@ Describe ExportExcel { $newarray[0].Member2 | should be 'Second' $newarray[0].Member3 | should beNullOrEmpty $newarray[0].Member4 | should beNullOrEmpty - } - } + } + } Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" { - $path = "$env:TEMP\Test.xlsx" - #This time we are not deleting the XLSX file so this should create a new, named, sheet. + $path = "$env:TEMP\Test.xlsx" + #This time we are not deleting the XLSX file so this should create a new, named, sheet. $Excel = Get-Process | Select-Object -first 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -PassThru - #Testing -passthru and adding the Pivot as a second step. Want to save and re-open it ... - Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM - + #Testing -passthru and adding the Pivot as a second step. Want to save and re-open it ... + Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM + $Excel = Open-ExcelPackage $path - $PTws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] + $PTws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] $wCount = $Excel.Workbook.Worksheets.Count it "Added the named sheet and pivot table to the workbook " { - $PTws | should not beNullOrEmpty - $PTws.PivotTables.Count | should be 1 - $Excel.Workbook.Worksheets["Processes"] | should not beNullOrEmpty - $Excel.Workbook.Worksheets.Count | should beGreaterThan 2 - $excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 51 #50 data + 1 header + $PTws | should not beNullOrEmpty + $PTws.PivotTables.Count | should be 1 + $Excel.Workbook.Worksheets["Processes"] | should not beNullOrEmpty + $Excel.Workbook.Worksheets.Count | should beGreaterThan 2 + $excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 51 #50 data + 1 header } $pt = $PTws.PivotTables[0] it "Built the expected Pivot table " { - $pt.RowFields.Count | should be 1 - $pt.RowFields[0].Name | should be "Company" + $pt.RowFields.Count | should be 1 + $pt.RowFields[0].Name | should be "Company" $pt.DataFields.Count | should be 1 $pt.DataFields[0].Function | should be "Count" $pt.DataFields[0].Field.Name | should be "PM" - $PTws.Drawings.Count | should be 0 + $PTws.Drawings.Count | should be 0 } - #using the already open sheet add the pivot chart + #using the already open sheet add the pivot chart $warnvar = $null Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path it "Added a chart to the pivot table without rebuilding " { - $ws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] + $ws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] $Excel.Workbook.Worksheets.Count | should be $wCount - $ws.Drawings.count | should be 1 + $ws.Drawings.count | should be 1 $ws.Drawings[0].ChartType.ToString() | should be "PieExploded3D" } it "Generated a message on re-processing the Pivot table " { $warnVar | Should not beNullOrEmpty } - $warnVar = $null + $warnVar = $null Get-Process | Select-Object -Last 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -Append -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path $pt = $Excel.Workbook.Worksheets["ProcessesPivotTable"].PivotTables[0] it "Appended to the Worksheet and Extended the Pivot table " { $Excel.Workbook.Worksheets.Count | should be $wCount $excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 101 #appended 50 rows to the previous total - $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | - should be "A1:E101" - } + $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | + should be "A1:E101" + } it "Generated a message on extending the Pivot table " { $warnVar | Should not beNullOrEmpty - } - } - + } + } + Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" { - $path = "$env:TEMP\Test.xlsx" - + $path = "$env:TEMP\Test.xlsx" + $Excel = Open-ExcelPackage $path #At this point Sheets should be in the order Sheet1, Processes, ProcessesPivotTable $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Processes" -MoveToEnd # order now Sheet1, ProcessesPivotTable, Processes @@ -426,12 +426,12 @@ Describe ExportExcel { $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Sheet1" -MoveAfter "*" # Now its NewSheet, ProcessesPivotTable, Processes, Sheet1 $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Another" -MoveToStart # Now its Another, NewSheet, ProcessesPivotTable, Processes, Sheet1 $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "OneLast" -MoveBefore "ProcessesPivotTable" # Now its Another, NewSheet, Onelast, ProcessesPivotTable, Processes, Sheet1 - Close-ExcelPackage $Excel + Close-ExcelPackage $Excel $Excel = Open-ExcelPackage $path it "Got the Sheets in the right order " { - $excel.Workbook.Worksheets[1].Name | should be "Another" + $excel.Workbook.Worksheets[1].Name | should be "Another" $excel.Workbook.Worksheets[2].Name | should be "NewSheet" $excel.Workbook.Worksheets[3].Name | should be "Onelast" $excel.Workbook.Worksheets[4].Name | should be "ProcessesPivotTable" @@ -447,14 +447,14 @@ Describe ExportExcel { $newWs.ConditionalFormatting[0].Formula | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula) } - } + } Context " # Create and append with Start row and Start Column, inc ranges and Pivot table" { - $path = "$env:TEMP\Test.xlsx" - #Catch warning + $path = "$env:TEMP\Test.xlsx" + #Catch warning $warnVar = $null - #Test Append with no existing sheet. Test adding a named pivot table from a command line parameter - get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append + #Test Append with no existing sheet. Test adding a named pivot table from a command line parameter + get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append get-process | Select-Object -last 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar $Excel = Open-ExcelPackage $path $dataWs = $Excel.Workbook.Worksheets["withOffset"] @@ -464,12 +464,12 @@ Describe ExportExcel { $dataWs.Cells[2,2].Value | Should beNullOrEmpty $dataWs.Cells[3,3].Value | Should not beNullOrEmpty $dataWs.Cells[3,3].Style.Font.Bold | Should be $true - $dataWs.Dimension.End.Row | Should be 23 + $dataWs.Dimension.End.Row | Should be 23 $dataWs.names[0].end.row | Should be 23 $dataWs.names[0].name | Should be 'Name' $dataWs.names.Count | Should be 6 - $dataWs.cells[$dataws.Dimension].AutoFilter | Should be true - $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | + $dataWs.cells[$dataws.Dimension].AutoFilter | Should be true + $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | Should be "C3:G23" } it "Generated a message on extending the Pivot table " { @@ -478,36 +478,36 @@ Describe ExportExcel { } Context "#Example 11 # Create and append with title, inc ranges and Pivot table" { - $path = "$env:TEMP\Test.xlsx" + $path = "$env:TEMP\Test.xlsx" $ptDef = [ordered]@{} - $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend - $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue + $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend + $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue it "Built a pivot definition using New-PivotTableDefinition " { - $ptDef.PT1.SourceWorkSheet | Should be 'Sheet1' - $ptDef.PT1.PivotRows | Should be 'Status' - $ptDef.PT1.PivotData.Status | Should be 'Count' - $ptDef.PT1.PivotFilter | Should be 'StartType' - $ptDef.PT1.IncludePivotChart | Should be $true - $ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D' - } - Remove-Item -Path $path - #Catch warning - $warnvar = $null + $ptDef.PT1.SourceWorkSheet | Should be 'Sheet1' + $ptDef.PT1.PivotRows | Should be 'Status' + $ptDef.PT1.PivotData.Status | Should be 'Count' + $ptDef.PT1.PivotFilter | Should be 'StartType' + $ptDef.PT1.IncludePivotChart | Should be $true + $ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D' + } + Remove-Item -Path $path + #Catch warning + $warnvar = $null Get-Service | Select-Object -Property Status, Name, DisplayName, StartType | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar - Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor AliceBlue -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef + Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor AliceBlue -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef $Excel = Open-ExcelPackage $path $ws1 = $Excel.Workbook.Worksheets["Sheet1"] $ws2 = $Excel.Workbook.Worksheets["Sheet2"] - + it "Set Column widths (with autosize) " { $ws1.Column(2).Width | Should not be $ws1.DefaultColWidth - $ws2.Column(1).width | Should not be $ws2.DefaultColWidth + $ws2.Column(1).width | Should not be $ws2.DefaultColWidth } - + it "Added tables to both sheets (handling illegal chars) and a title in sheet 2 " { - $warnvar.count | Should be 1 + $warnvar.count | Should be 1 $ws1.tables.Count | Should be 1 $ws2.tables.Count | Should be 1 $ws1.Tables[0].Address.Start.Row | Should be 1 @@ -515,46 +515,46 @@ Describe ExportExcel { $ws1.Tables[0].Address.End.Address | Should be $ws1.Dimension.End.Address $ws2.Tables[0].Address.End.Address | Should be $ws2.Dimension.End.Address $ws2.Tables[0].Name | Should be "Processes" - $ws2.Tables[0].StyleName | Should be "TableStyleLight1" + $ws2.Tables[0].StyleName | Should be "TableStyleLight1" $ws2.Cells["A1"].Value | Should be "Processes" $ws2.Cells["A1"].Style.Font.Bold | Should be $true $ws2.Cells["A1"].Style.Font.Size | Should be 22 $ws2.Cells["A1"].Style.Fill.PatternType.tostring() | Should be "solid" $ws2.Cells["A1"].Style.Fill.BackgroundColor.Rgb | Should be "fff0f8ff" } - + $ptsheet1 = $Excel.Workbook.Worksheets["Pt1"] $ptsheet2 = $Excel.Workbook.Worksheets["Pt2"] $PT1 = $ptsheet1.PivotTables[0] $PT2 = $ptsheet2.PivotTables[0] $PC1 = $ptsheet1.Drawings[0] $PC2 = $ptsheet2.Drawings[0] - it "Created the correct pivot tables and charts from the definitions. " { + it "Created the correct pivot tables and charts from the definitions. " { - $PT1.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | + $PT1.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | Should be ("A1:" + $ws1.Dimension.End.Address) - $PT2.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | + $PT2.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | Should be ("A2:" + $ws2.Dimension.End.Address) #Title in row 1 - + $pt1.PageFields[0].Name | Should be 'StartType' $pt1.RowFields[0].Name | Should be 'Status' - $pt1.DataFields[0].Field.name | Should be 'Status' - $pt1.DataFields[0].Function | Should be 'Count' + $pt1.DataFields[0].Field.name | Should be 'Status' + $pt1.DataFields[0].Function | Should be 'Count' $pc1.ChartType | Should be 'BarClustered3D' - $pc1.From.Column | Should be 0 #chart 1 at 0,10 chart 2 at 4,0 (default) - $pc2.From.Column | Should be 4 - $pc1.From.Row | Should be 10 - $pc2.From.Row | Should be 0 - $pc1.Legend.Font | should beNullOrEmpty #Best check for legend removed. - $pc2.Legend.Font | should not beNullOrEmpty + $pc1.From.Column | Should be 0 #chart 1 at 0,10 chart 2 at 4,0 (default) + $pc2.From.Column | Should be 4 + $pc1.From.Row | Should be 10 + $pc2.From.Row | Should be 0 + $pc1.Legend.Font | should beNullOrEmpty #Best check for legend removed. + $pc2.Legend.Font | should not beNullOrEmpty $pc1.Title.Text | Should be 'Services by status' - $pc2.DataLabel.ShowPercent | should be $true - } + $pc2.DataLabel.ShowPercent | should be $true + } } - + Context "#Example 13 # Formatting and another way to do a pivot. " { - $path = "$env:TEMP\Test.xlsx" - Remove-Item $path + $path = "$env:TEMP\Test.xlsx" + Remove-Item $path $excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru $sheet = $excel.Workbook.Worksheets["Processes"] $sheet.Column(1) | Set-Format -Bold -AutoFit @@ -566,55 +566,55 @@ Describe ExportExcel { Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit } - Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet "Processes" -PivotRows Company -PivotData @{'Name'='Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend + Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet "Processes" -PivotRows Company -PivotData @{'Name'='Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend Close-ExcelPackage $excel - $excel = Open-ExcelPackage $path + $excel = Open-ExcelPackage $path $sheet = $excel.Workbook.Worksheets["Processes"] - + it "Applied the formating" { $sheet | should not beNullOrEmpty $sheet.Column(1).wdith | should not be $sheet.DefaultColWidth $sheet.Column(7).wdith | should not be $sheet.DefaultColWidth $sheet.Column(1).style.font.bold | should be $true $sheet.Column(2).style.wraptext | should be $true - $sheet.Column(2).width | should be 29 - $sheet.Column(3).style.horizontalalignment | should be 'right' + $sheet.Column(2).width | should be 29 + $sheet.Column(3).style.horizontalalignment | should be 'right' $sheet.Column(4).style.horizontalalignment | should be 'right' - $sheet.Cells["A1"].Style.HorizontalAlignment | should be 'Center' - $sheet.Cells['E2'].Style.HorizontalAlignment | should be 'right' - $sheet.Cells['A1'].Style.Font.Bold | should be $true - $sheet.Cells['D2'].Style.Font.Bold | should be $true - $sheet.Cells['E2'].style.numberformat.format | should be '#,###' - $sheet.Column(3).style.numberformat.format | should be '#,###' - $sheet.Column(4).style.numberformat.format | should be '#,##0.0' + $sheet.Cells["A1"].Style.HorizontalAlignment | should be 'Center' + $sheet.Cells['E2'].Style.HorizontalAlignment | should be 'right' + $sheet.Cells['A1'].Style.Font.Bold | should be $true + $sheet.Cells['D2'].Style.Font.Bold | should be $true + $sheet.Cells['E2'].style.numberformat.format | should be '#,###' + $sheet.Column(3).style.numberformat.format | should be '#,###' + $sheet.Column(4).style.numberformat.format | should be '#,##0.0' $sheet.ConditionalFormatting.Count | should be 2 - $sheet.ConditionalFormatting[0].type | should be 'Databar' + $sheet.ConditionalFormatting[0].type | should be 'Databar' $sheet.ConditionalFormatting[0].Color.name | should be 'ffff0000' - $sheet.ConditionalFormatting[0].Address.Address | should be 'D2:D1048576' - $sheet.ConditionalFormatting[1].type | should be 'GreaterThan' - $sheet.ConditionalFormatting[1].Formula | should be '104857600' + $sheet.ConditionalFormatting[0].Address.Address | should be 'D2:D1048576' + $sheet.ConditionalFormatting[1].type | should be 'GreaterThan' + $sheet.ConditionalFormatting[1].Formula | should be '104857600' $sheet.ConditionalFormatting[1].Style.Font.Color.Color.Name | should be 'ffff0000' } it "Froze the panes" { $sheet.view.Panes.Count | should be 3 } $ptsheet1 = $Excel.Workbook.Worksheets["Pt_procs"] - + it "Created the pivot table" { $ptsheet1 | should not beNullOrEmpty $ptsheet1.PivotTables[0].DataFields[0].Field.Name | should be "Name" $ptsheet1.PivotTables[0].DataFields[0].Function | should be "Count" - $ptsheet1.PivotTables[0].RowFields[0].Name | should be "Company" - $ptsheet1.PivotTables[0].CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | + $ptsheet1.PivotTables[0].RowFields[0].Name | should be "Company" + $ptsheet1.PivotTables[0].CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | Should be $sheet.Dimension.address - } + } } - ## To do - ## More pivot options & other FreezePanes settings ? - ## Charts - ## Style script block - ## Rezip ? + ## To do + ## More pivot options & other FreezePanes settings ? + ## Charts + ## Style script block + ## Rezip ? }