From 282d650072a8deecec1ab10d4dfad08ea542f3f8 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Mon, 2 Nov 2020 18:01:23 +0000 Subject: [PATCH] De-skipped some tests. Fixed a tiny bug in Add-Conditional formatting. --- Examples/import-by-columns.ps1 | 52 ++ Public/Add-ConditionalFormatting.ps1 | 2 +- __tests__/Copy-ExcelWorksheet.Tests.ps1 | 7 +- __tests__/Export-Excel.Tests.ps1 | 162 ++--- __tests__/Get-ExcelColumnName.Test.ps1 | 2 +- __tests__/ImportExcelHeaderName.tests.ps1 | 2 +- __tests__/Join-Worksheet.tests.ps1 | 8 +- ...angePassing.ps1 => RangePassing.Tests.ps1} | 25 +- .../Set-Row_Set-Column-SetFormat.tests.ps1 | 52 +- __tests__/TestResultsPS7.0.3.xml | 639 ++++++++++++++++++ 10 files changed, 799 insertions(+), 152 deletions(-) create mode 100644 Examples/import-by-columns.ps1 rename __tests__/{RangePassing.ps1 => RangePassing.Tests.ps1} (94%) create mode 100644 __tests__/TestResultsPS7.0.3.xml diff --git a/Examples/import-by-columns.ps1 b/Examples/import-by-columns.ps1 new file mode 100644 index 0000000..e70015d --- /dev/null +++ b/Examples/import-by-columns.ps1 @@ -0,0 +1,52 @@ + +Function Import-Bycolumns { + Param( + [Parameter(Mandatory=$true)] + [OfficeOpenXml.ExcelPackage]$ExcelPackage, + [Int]$StartRow = 1, + [String]$WorksheetName, + [Int]$EndRow , + [Int]$StartColumn = 1, + [Int]$EndColumn + ) + Function Get-RowNames { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = "Name would be incorrect, and command is not exported")] + param( + [Parameter(Mandatory)] + [Int[]]$Rows, + [Parameter(Mandatory)] + [Int]$StartColumn + ) + foreach ($R in $Rows) { + #allow "False" or "0" to be headings + $Worksheet.Cells[$R, $StartColumn] | Where-Object {-not [string]::IsNullOrEmpty($_.Value) } | Select-Object @{N = 'Row'; E = { $R } }, Value + } + } + + if (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] } + elseif (-not ($Worksheet = $ExcelPackage.Workbook.Worksheets[$WorkSheetName])) { + throw "Worksheet '$WorksheetName' not found, the workbook only contains the worksheets '$($ExcelPackage.Workbook.Worksheets)'. If you only wish to select the first worksheet, please remove the '-WorksheetName' parameter." ; return + } + + if (-not $EndRow ) { $EndRow = $Worksheet.Dimension.End.Row } + if (-not $EndColumn) { $EndColumn = $Worksheet.Dimension.End.Column } + + $Rows = $Startrow .. $EndRow ; + $Columns = (1 + $StartColumn)..$EndColumn + + if ((-not $rows) -or (-not ($PropertyNames = Get-RowNames -Rows $Rows -StartColumn $StartColumn))) { + throw "No headers found in left coulmn '$Startcolumn'. "; return + } + if (-not $Columns) { + Write-Warning "Worksheet '$WorksheetName' in workbook contains no data in the rows after left column '$StartColumn'" + } + else { + foreach ($c in $Columns) { + $NewColumn = [Ordered]@{ } + foreach ($p in $PropertyNames) { + $NewColumn[$p.Value] = $Worksheet.Cells[$p.row,$c].text + } + [PSCustomObject]$NewColumn + } + } +} diff --git a/Public/Add-ConditionalFormatting.ps1 b/Public/Add-ConditionalFormatting.ps1 index 6f8a9c3..4464cab 100644 --- a/Public/Add-ConditionalFormatting.ps1 +++ b/Public/Add-ConditionalFormatting.ps1 @@ -69,7 +69,7 @@ $Address = "$($Address.Row):$($Address.Row)" } elseif ($Address -is [OfficeOpenXml.ExcelColumn]) { - $Address = (New-Object 'OfficeOpenXml.ExcelAddress' @(1, $address.ColumnMin, 1, $address.ColumnMax).Address) -replace '1','' + $Address = (New-Object 'OfficeOpenXml.ExcelAddress' @(1, $address.ColumnMin, 1, $address.ColumnMax)).Address -replace '1','' if ($Address -notmatch ':') {$Address = "$Address`:$Address"} } if ( $Address -is [string] -and $Address -match "!") {$Address = $Address -replace '^.*!',''} diff --git a/__tests__/Copy-ExcelWorksheet.Tests.ps1 b/__tests__/Copy-ExcelWorksheet.Tests.ps1 index b50c8aa..2ae3ac7 100644 --- a/__tests__/Copy-ExcelWorksheet.Tests.ps1 +++ b/__tests__/Copy-ExcelWorksheet.Tests.ps1 @@ -34,13 +34,13 @@ Describe "Copy-Worksheet" { StrTrailSpace = '123 ' Link1 = [uri]"https://github.com/dfinke/ImportExcel" Link2 = "https://github.com/dfinke/ImportExcel" # Links are not copied correctly, hopefully this will be fixed at some future date - } | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -WorkSheetname MixedTypes -Path $path2 + } | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -WorkSheetname MixedTypes -Path $path2 } Context "Simplest copy" { BeforeAll { $path1 = "TestDrive:\Test1.xlsx" $path2 = "TestDrive:\Test2.xlsx" - + Copy-ExcelWorksheet -SourceWorkbook $path1 -DestinationWorkbook $path2 $excel = Open-ExcelPackage -Path $path2 $ws = $excel.Workbook.Worksheets["Processes"] @@ -51,8 +51,9 @@ Describe "Copy-Worksheet" { $ws.Dimension.Address | Should -Be $ProcRange } } - Context "Mixed types using a package object" -Skip { + Context "Mixed types using a package object" { BeforeAll { + $excel = Open-ExcelPackage -Path $path2 Copy-ExcelWorksheet -SourceWorkbook $excel -DestinationWorkbook $excel -DestinationWorkSheet "CopyOfMixedTypes" Close-ExcelPackage -ExcelPackage $excel $excel = Open-ExcelPackage -Path $path2 diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index 723ad1d..1a1ac3e 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -116,7 +116,7 @@ Remove-item -Path $path -ErrorAction SilentlyContinue #Test -DisplayPropertySet $Processes | Export-Excel $path -DisplayPropertySet - + $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] $ws.Name | Should -Be "sheet1" @@ -233,7 +233,7 @@ BeforeEach { $Excel = Open-ExcelPackage -Path $path - $ws = $Excel.Workbook.Worksheets[1] + $ws = $Excel.Workbook.Worksheets[1] } it "Created a new file " { @@ -340,7 +340,7 @@ Link = [uri]"https://github.com/dfinke/ImportExcel" } | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path -NoHeader } - + BeforeEach { $Excel = Open-ExcelPackage -Path $path $ws = $Excel.Workbook.Worksheets[1] @@ -382,7 +382,7 @@ } } - Context "#Example 5 # Adding a single conditional format " -Skip { + Context "#Example 5 # Adding a single conditional format "{ BeforeEach { #Test New-ConditionalText builds correctly $ct = New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor ([System.Drawing.Color]::DarkRed) -BackgroundColor ([System.Drawing.Color]::LightPink) @@ -392,9 +392,6 @@ #Test -ConditionalText with a single conditional spec. 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 - } #ToDo need to test applying conitional formatting to a pre-existing worksheet and removing = from formula $Excel = Open-ExcelPackage -Path $path @@ -429,7 +426,7 @@ ) $ws = $Excel.Workbook.Worksheets[1] } - + AfterAll { Close-ExcelPackage -ExcelPackage $Excel } @@ -487,7 +484,7 @@ } } - Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" -Skip { + Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" { BeforeAll { $path = "TestDrive:\test.xlsx" #Test -passthru and -worksheetName creating a new, named, sheet in an existing file. @@ -503,24 +500,15 @@ #test adding pivot chart using the already open sheet $warnvar = $null Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -ShowCategory -ShowPercent -NoLegend -WarningAction SilentlyContinue -WarningVariable warnvar - } - BeforeEach { $Excel = Open-ExcelPackage $path - #Test appending data extends pivot chart (with a warning) . - $warnVar = $null - Get-Process | Select-Object -Last 20 -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 "Added the named sheet and pivot table to the workbook " { $excel.ProcessesPivotTable | Should -Not -BeNullOrEmpty - $PTws | Should -Not -BeNullOrEmpty - $PTws.PivotTables.Count | Should -Be 1 + $excel.ProcessesPivotTable.PivotTables.Count | Should -Be 1 $Excel.Workbook.Worksheets["Processes"] | Should -Not -BeNullOrEmpty - $Excel.Workbook.Worksheets.Count | Should -BeGreaterThan 2 + $Excel.Workbook.Worksheets.Count | Should -BeGreaterOrEqual 2 $excel.Workbook.Worksheets["Processes"].Dimension.rows | Should -Be 21 #20 data + 1 header } it "Selected the Pivottable page " { @@ -544,25 +532,35 @@ it "Generated a message on re-processing the Pivot table " { $warnVar | Should -Not -BeNullOrEmpty } - it "Appended to the Worksheet and Extended the Pivot table " { + it "Appended to the Worksheet and Extended the Pivot table (with a warning) " { + + #Test appending data extends pivot chart (with a warning) . + $warnVar = $null + Get-Process | Select-Object -Last 20 -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] + $Excel.Workbook.Worksheets.Count | Should -Be $wCount $excel.Workbook.Worksheets["Processes"].Dimension.rows | Should -Be 41 #appended 20 rows to the previous total $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | - Should be "A1:E41" - } - it "Generated a message on extending the Pivot table " { + Should -Be "A1:E41" + $warnVar | Should -Not -BeNullOrEmpty } } - Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" -Skip { + Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" { BeforeAll { $path = "TestDrive:\test.xlsx" #Test the -CopySource and -Movexxxx parameters for Add-Worksheet + $Excel = Get-Process | Select-Object -first 20 -Property Name, cpu, pm, handles, company | + Export-Excel $path -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -NoTotalsInPivot -PivotDataToColumn -Activate + $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 - $null = Add-Worksheet -ExcelPackage $Excel -WorkSheetname "NewSheet" -MoveAfter "*" -CopySource ($excel.Workbook.Worksheets["Sheet1"]) # Now its NewSheet, Sheet1, ProcessesPivotTable, Processes + $null = Add-Worksheet -ExcelPackage $Excel -WorkSheetname "Processes" -MoveToEnd # order now ProcessesPivotTable, Processes + $null = Add-Worksheet -ExcelPackage $Excel -WorkSheetname "NewSheet" -MoveAfter "*" -CopySource ($excel.Workbook.Worksheets["Processes"]) # Now its NewSheet, ProcessesPivotTable, Processes $null = Add-Worksheet -ExcelPackage $Excel -WorkSheetname "Sheet1" -MoveAfter "Processes" # 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 "NearDone" -MoveBefore 5 # Now its Another, NewSheet, ProcessesPivotTable, Processes, NearDone ,Sheet1 @@ -582,12 +580,9 @@ $excel.Workbook.Worksheets[7].Name | Should -Be "Sheet1" } - it "Cloned 'Sheet1' to 'NewSheet' " { + it "Cloned 'Processes' to 'NewSheet' " { $newWs = $excel.Workbook.Worksheets["NewSheet"] - $newWs.Dimension.Address | Should -Be ($excel.Workbook.Worksheets["Sheet1"].Dimension.Address) - $newWs.ConditionalFormatting.Count | Should -Be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting.Count) - $newWs.ConditionalFormatting[0].Address.Address | Should -Be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Address.Address) - $newWs.ConditionalFormatting[0].Formula | Should -Be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula) + $newWs.Dimension.Address | Should -Be ($excel.Workbook.Worksheets["Processes"].Dimension.Address) } } @@ -665,7 +660,7 @@ $dataWs.cells["C2"].Style.Numberformat.Format | Should -Be "General" $dataWs.cells["C12"].Style.Numberformat.Format | Should -Be "0.00" } - + it "Created a new sheet and explicitly extended named range and autofilter " { #Test extending autofilter and range when explicitly specified in the append @@ -688,82 +683,6 @@ # $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count' } -PivotTotals Columns -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend -PivotColumns CanPauseAndContinue # $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count' } -PivotTotalS Rows -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' - # $ptDef.PT1.PivotTotals | Should -Be 'Columns' - # } - # Remove-Item -Path $path - # #Catch warning - # $warnvar = $null - # #Test create two data pages; as part of adding the second give both their own pivot table, test -autosize switch - # Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningVariable warnvar -WarningAction SilentlyContinue - # 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 ([System.Drawing.Color]::AliceBlue) -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef - # $Excel = Open-ExcelPackage $path - # $ws1 = $Excel.Workbook.Worksheets["Sheet1"] - # $ws2 = $Excel.Workbook.Worksheets["Sheet2"] - - # if ($isWindows) { - # it "Set Column widths (with autosize) " { - # $ws1.Column(2).Width | Should -Not -Be $ws1.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 -BeGreaterThan 0 - # $ws1.tables.Count | Should -Be 1 - # $ws2.tables.Count | Should -Be 1 - # $ws1.Tables[0].Address.Start.Row | Should -Be 1 - # $ws2.Tables[0].Address.Start.Row | Should -Be 2 #Title in row 1 - # $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.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 pivot tables linked to the right data. " { - # $PT1.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.name | - # Should be "All_services" - # $PT2.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.name | - # Should be "Processes" - # } - # it "Set the other pivot tables and chart options from the definitions. " { - # $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.ColumGrandTotals | Should -Be $true - # $pt1.RowGrandTotals | Should -Be $false - # $pt2.ColumGrandTotals | Should -Be $false - # $pt2.RowGrandTotals | Should -Be $true - # $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.Title.Text | Should -Be 'Services by status' - # $pc2.DataLabel.ShowPercent | Should -Be $true - # } - # } - Context "#Example 13 # Formatting and another way to do a pivot. " { BeforeAll { $path = "TestDrive:\test.xlsx" @@ -837,7 +756,7 @@ it "Froze the panes " { $sheet.view.Panes.Count | Should -Be 3 } - + it "Created the pivot table " { $ptsheet1 = $Excel.Workbook.Worksheets["Pt_procs"] $ptsheet1 | Should -Not -BeNullOrEmpty @@ -876,8 +795,8 @@ $data[1].PM | Should -Not -BeNullOrEmpty $data[1].VirtualMemorySize | Should -Not -BeNullOrEmpty } - - it "Created the Excel chart definition " { + + it "Created an Excel chart definition and used it " { $c = New-ExcelChartDefinition -Title Stats -ChartType LineMarkersStacked -XRange "Processes[Name]" -YRange "Processes[PM]", "Processes[VirtualMemorySize]" -SeriesHeader 'PM', 'VMSize' $c | Should -Not -BeNullOrEmpty $c.ChartType.gettype().name | Should -Be "eChartType" @@ -891,14 +810,11 @@ $c.Nolegend | Should -Not -Be $true $c.ShowCategory | Should -Not -Be $true $c.ShowPercent | Should -Not -Be $true - } - it "Used the Excel chart definition with Export-Excel " -Skip { - #Test creating a chart using -ExcelChartDefinition. $data | Export-Excel $path -AutoSize -TableName Processes -ExcelChartDefinition $c $excel = Open-ExcelPackage -Path $path $drawings = $excel.Workbook.Worksheets[1].drawings - + $drawings.count | Should -Be 1 $drawings[0].ChartType | Should -Be "LineMarkersStacked" $drawings[0].Series.count | Should -Be 2 @@ -907,8 +823,8 @@ $drawings[0].Series[1].Series | Should -Be "'Sheet1'!Processes[VirtualMemorySize]" $drawings[0].Series[1].XSeries | Should -Be "'Sheet1'!Processes[Name]" $drawings[0].Title.text | Should -Be "Stats" - - Close-ExcelPackage $excel + + Close-ExcelPackage $excel } } @@ -1001,7 +917,7 @@ $ct.ConditionalType | Should -Be "ContainsText" $ct.Text | Should -Be "Microsoft" } - + BeforeEach { #Test -ConditionalFormat & -ConditionalText Export-Excel -Path $path -ConditionalFormat $cf -ConditionalText $ct @@ -1068,9 +984,9 @@ } } - Context " # Parameters and ParameterSets" -Skip { + Context " # Parameters and ParameterSets" { BeforeAll { - $Path = Join-Path (Resolve-Path 'TestDrive:').ProviderPath "test.xlsx" + $Path = Join-Path (Resolve-Path 'TestDrive:').ProviderPath "test.xlsx" Remove-Item -Path $Path -ErrorAction SilentlyContinue $Processes = Get-Process | Select-Object -first 10 -Property Name, cpu, pm, handles, company } @@ -1086,8 +1002,8 @@ } it "throws when the ExcelPackage is specified with either -path or -Now".PadRight(87) { $ExcelPackage = Export-Excel -Path $Path -PassThru - { Export-Excel -ExcelPackage $ExcelPackage -Path $Path } | Should -Throw 'Parameter set cannot be resolved using the specified named parameters' - { Export-Excel -ExcelPackage $ExcelPackage -Now } | Should -Throw 'Parameter set cannot be resolved using the specified named parameters' + { Export-Excel -ExcelPackage $ExcelPackage -Path $Path } | Should -Throw + { Export-Excel -ExcelPackage $ExcelPackage -Now } | Should -Throw $Processes | Export-Excel -ExcelPackage $ExcelPackage Remove-Item -Path $Path diff --git a/__tests__/Get-ExcelColumnName.Test.ps1 b/__tests__/Get-ExcelColumnName.Test.ps1 index 070650e..ed3bc9e 100644 --- a/__tests__/Get-ExcelColumnName.Test.ps1 +++ b/__tests__/Get-ExcelColumnName.Test.ps1 @@ -19,7 +19,7 @@ $map = @{ (Get-ExcelColumnName 26).columnName | Should -Be 'Z' (Get-ExcelColumnName 27).columnName | Should -Be 'AA' -(Get-ExcelColumnName 28).columnNamee | Should -Be 'AB' +(Get-ExcelColumnName 28).columnName | Should -Be 'AB' (Get-ExcelColumnName 30).columnName | Should -Be 'AD' (Get-ExcelColumnName 48).columnName | Should -Be 'AV' diff --git a/__tests__/ImportExcelHeaderName.tests.ps1 b/__tests__/ImportExcelHeaderName.tests.ps1 index 30c6cbf..0594a88 100644 --- a/__tests__/ImportExcelHeaderName.tests.ps1 +++ b/__tests__/ImportExcelHeaderName.tests.ps1 @@ -204,7 +204,7 @@ Describe "Import-Excel on a sheet with no headings" { } It "Should handle data correctly if there is only a single row" { - $actual = Import-Excel $xlfileHeaderOnly + $actual = Import-Excel $xlfileHeaderOnly -WarningAction SilentlyContinue $names = $actual.psobject.properties.Name $names | Should -Be $null $actual.Count | Should -Be 0 diff --git a/__tests__/Join-Worksheet.tests.ps1 b/__tests__/Join-Worksheet.tests.ps1 index 6f3d34a..e567a19 100644 --- a/__tests__/Join-Worksheet.tests.ps1 +++ b/__tests__/Join-Worksheet.tests.ps1 @@ -92,11 +92,12 @@ Describe "Join Worksheet part 1" { } } } -$path = "TestDrive:\Test.xlsx" -Remove-item -Path $path -ErrorAction SilentlyContinue -#switched to CIM objects so test runs on V6 + Describe "Join Worksheet part 2" { BeforeEach { + $path = "TestDrive:\Test.xlsx" + Remove-item -Path $path -ErrorAction SilentlyContinue + #switched to CIM objects so test runs on V6+ Get-CimInstance -ClassName win32_logicaldisk | Select-Object -Property DeviceId, VolumeName, Size, Freespace | Export-Excel -Path $path -WorkSheetname Volumes -NumberFormat "0,000" @@ -129,4 +130,3 @@ Describe "Join Worksheet part 2" { } } } - diff --git a/__tests__/RangePassing.ps1 b/__tests__/RangePassing.Tests.ps1 similarity index 94% rename from __tests__/RangePassing.ps1 rename to __tests__/RangePassing.Tests.ps1 index b2bb041..eada178 100644 --- a/__tests__/RangePassing.ps1 +++ b/__tests__/RangePassing.Tests.ps1 @@ -1,11 +1,12 @@ [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases','',Justification='Testing for presence of alias')] param() -$path = "TestDrive:\test.xlsx" + describe "Consistent passing of ranges." { + BeforeAll { $path = "TestDrive:\test.xlsx" } Context "Conditional Formatting" { - Remove-Item -path $path -ErrorAction SilentlyContinue - $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -AutoNameRange -Title "Services on $Env:COMPUTERNAME" it "accepts named ranges, cells['name'], worksheet + Name, worksheet + column " { + Remove-Item -path $path -ErrorAction SilentlyContinue + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -AutoNameRange -Title "Services on $Env:COMPUTERNAME" {Add-ConditionalFormatting $excel.Services.Names["Status"] -StrikeThru -RuleType ContainsText -ConditionValue "Stopped" } | Should -Not -Throw $excel.Services.ConditionalFormatting.Count | Should -Be 1 {Add-ConditionalFormatting $excel.Services.Cells["Name"] -Italic -RuleType ContainsText -ConditionValue "SVC" } | Should -Not -Throw @@ -23,11 +24,12 @@ describe "Consistent passing of ranges." { {Add-ConditionalFormatting "Status" -Worksheet $excel.Services ` -ForeGroundColor ([System.Drawing.Color]::Green) -RuleType ContainsText -ConditionValue "Running"} | Should -Not -Throw $excel.Services.ConditionalFormatting.Count | Should -Be 4 + Close-ExcelPackage -NoSave $excel } - Close-ExcelPackage -NoSave $excel - $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" + it "accepts table, table.Address and worksheet + 'C:C' " { - {Add-ConditionalFormatting $excel.Services.Tables[0] ` + $excel = Get-Service | Export-Excel -Path $path -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" + {Add-ConditionalFormatting $excel.Services.Tables[0] ` -Italic -RuleType ContainsText -ConditionValue "Svc" } | Should -Not -Throw $excel.Services.ConditionalFormatting.Count | Should -Be 1 {Add-ConditionalFormatting $excel.Services.Tables["ServiceTable"].Address ` @@ -36,8 +38,8 @@ describe "Consistent passing of ranges." { {Add-ConditionalFormatting -Worksheet $excel.Services -Address "a:a" ` -RuleType ContainsText -ConditionValue "stopped" -ForeGroundColor ([System.Drawing.Color]::Red) } | Should -Not -Throw $excel.Services.ConditionalFormatting.Count | Should -Be 3 + Close-ExcelPackage -NoSave $excel } - Close-ExcelPackage -NoSave $excel } Context "Formating (Set-ExcelRange or its alias Set-Format) " { @@ -54,8 +56,9 @@ describe "Consistent passing of ranges." { $excel.Services.cells["B3"].Style.Font.Strike | Should -Be $true {Set-ExcelRange -Worksheet $excel.Services -Range "A5:B6" -FontSize 8 } | Should -Not -Throw $excel.Services.cells["A5"].Style.Font.Size | Should -Be 8 + Close-ExcelPackage -NoSave $excel } - Close-ExcelPackage -NoSave $excel + it "Accepts Table, Table.Address , worksheet + Name, Column," { $excel = Get-Service | Export-Excel -Path test2.xlsx -WorksheetName Services -PassThru -AutoNameRange -AutoSize -DisplayPropertySet -TableName servicetable -Title "Services on $Env:COMPUTERNAME" {Set-ExcelRange $excel.Services.Tables[0] -Italic } | Should -Not -Throw @@ -66,8 +69,9 @@ describe "Consistent passing of ranges." { $excel.Services.cells["B4"].Style.Font.Bold | Should -Be $true {$excel.Services.Column(3) | Set-ExcelRange -FontColor ([System.Drawing.Color]::Red) } | Should -Not -Throw $excel.Services.cells["C4"].Style.Font.Color.Rgb | Should -Be "FFFF0000" + Close-ExcelPackage -NoSave $excel } - Close-ExcelPackage -NoSave $excel + } Context "PivotTables" { @@ -109,8 +113,5 @@ describe "Consistent passing of ranges." { $excel.Workbook.Worksheets["pt2"] | Should -Not -BeNullOrEmpty Close-ExcelPackage -NoSave $excel } - - - } } \ No newline at end of file diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index 1aa3450..56bf571 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -1,7 +1,7 @@  Describe "Number format expansion and setting" { - BeforeEach { + BeforeAll { $path = "TestDrive:\test.xlsx" $data = ConvertFrom-Csv -InputObject @" @@ -12,7 +12,7 @@ Describe "Number format expansion and setting" { 12010,Drill,20,8 12011,Crowbar,7,23.48 "@ - + $DriverData = convertFrom-CSv @" Name,Wikipage,DateOfBirth Fernando Alonso,/wiki/Fernando_Alonso,1981-07-29 @@ -21,7 +21,7 @@ Describe "Number format expansion and setting" { Lewis Hamilton,/wiki/Lewis_Hamilton,1985-01-07 Nico Rosberg,/wiki/Nico_Rosberg,1985-06-27 Sebastian Vettel,/wiki/Sebastian_Vettel,1987-07-03 -"@ | ForEach-Object { $_.DateOfBirth = [datetime]$_.DateofBirth; $_ } +"@ | ForEach-Object { $_.DateOfBirth = [datetime]$_.DateofBirth; $_ } } Context "Expand-NumberFormat function" { @@ -119,8 +119,29 @@ Describe "Number format expansion and setting" { } } -Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" -Skip { +Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" { BeforeAll { + $path = "TestDrive:\test.xlsx" + + $data = ConvertFrom-Csv -InputObject @" + ID,Product,Quantity,Price + 12001,Nails,37,3.99 + 12002,Hammer,5,12.10 + 12003,Saw,12,15.37 + 12010,Drill,20,8 + 12011,Crowbar,7,23.48 +"@ + + $DriverData = convertFrom-CSv @" + Name,Wikipage,DateOfBirth + Fernando Alonso,/wiki/Fernando_Alonso,1981-07-29 + Jenson Button,/wiki/Jenson_Button,1980-01-19 + Kimi Räikkönen,/wiki/Kimi_R%C3%A4ikk%C3%B6nen,1979-10-17 + Lewis Hamilton,/wiki/Lewis_Hamilton,1985-01-07 + Nico Rosberg,/wiki/Nico_Rosberg,1985-06-27 + Sebastian Vettel,/wiki/Sebastian_Vettel,1987-07-03 +"@ | ForEach-Object { $_.DateOfBirth = [datetime]$_.DateofBirth; $_ } + Remove-Item -Path $path -ErrorAction SilentlyContinue $excel = $data | Export-Excel -Path $path -AutoNameRange -PassThru $ws = $excel.Workbook.Worksheets["Sheet1"] @@ -275,7 +296,7 @@ Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" -Skip { Describe "Conditional Formatting" { BeforeAll { - #Remove-Item $path + $path = "TestDrive:\test.xlsx" $data = Get-Process | Where-Object company | Select-Object company, name, pm, handles, *mem* $cfmt = New-ConditionalFormattingIconSet -Range "c:c" -ConditionalFormat ThreeIconSet -IconType Arrows $data | Export-Excel -path $Path -AutoSize -ConditionalFormat $cfmt @@ -348,9 +369,26 @@ Sold,ID } } -Describe "Table Formatting" -Skip { +Describe "Table Formatting" { BeforeAll { - #Remove-Item $path + $path = "TestDrive:\test.xlsx" + $data2 = 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 + 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 + 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 +"@ $excel = $data2 | Export-excel -path $path -WorksheetName Hardware -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru $ws = $excel.Workbook.Worksheets[1] #test showfilter & TotalSettings diff --git a/__tests__/TestResultsPS7.0.3.xml b/__tests__/TestResultsPS7.0.3.xml new file mode 100644 index 0000000..c757daf --- /dev/null +++ b/__tests__/TestResultsPS7.0.3.xml @@ -0,0 +1,639 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + UnauthorizedAccessException: Access to the path 'C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__' is denied. +InvalidOperationException: Error saving file C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__ +MethodInvocationException: Exception calling "Save" with "0" argument(s): "Error saving file C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__" + at Export-Excel<End>, C:\Users\mcp\Documents\GitHub\ImportExcel\Public\Export-Excel.ps1:672 +at <ScriptBlock>, C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__\Join-Worksheet.tests.ps1:100 + + + + + UnauthorizedAccessException: Access to the path 'C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__' is denied. +InvalidOperationException: Error saving file C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__ +MethodInvocationException: Exception calling "Save" with "0" argument(s): "Error saving file C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__" + at Export-Excel<End>, C:\Users\mcp\Documents\GitHub\ImportExcel\Public\Export-Excel.ps1:672 +at <ScriptBlock>, C:\Users\mcp\Documents\GitHub\ImportExcel\__tests__\Join-Worksheet.tests.ps1:100 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + This test should run but it did not. Most likely a setup in some parent block failed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + This test should run but it did not. Most likely a setup in some parent block failed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file