diff --git a/ExportedCommands/Export-Excel.ps1 b/ExportedCommands/Export-Excel.ps1 index a8c54c5..58e6ed8 100644 --- a/ExportedCommands/Export-Excel.ps1 +++ b/ExportedCommands/Export-Excel.ps1 @@ -13,7 +13,7 @@ [Switch]$Calculate, [Switch]$Show, [String]$WorksheetName = 'Sheet1', - [Alias("P")] + [Alias("PW")] [String]$Password, [switch]$ClearSheet, [switch]$Append, diff --git a/__tests__/InputItemParameter.tests.ps1 b/__tests__/InputItemParameter.tests.ps1 index 06162a6..6eb6f9e 100644 --- a/__tests__/InputItemParameter.tests.ps1 +++ b/__tests__/InputItemParameter.tests.ps1 @@ -2,8 +2,9 @@ Param() Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking Import -asText" { BeforeAll { - $path = "TestDrive:\Results.xlsx" - Remove-Item -Path $path -ErrorAction SilentlyContinue + $path = "TestDrive:\Results.xlsx" + $path2 = "TestDrive:\Results2.xlsx" + Remove-Item -Path $path,$path2 -ErrorAction SilentlyContinue if (Test-path "$PSScriptRoot\Samples\Samples.ps1") {. "$PSScriptRoot\Samples\Samples.ps1"} $results = ((Get-Process) + (Get-Process -id $PID)) | Select-Object -last 10 -Property Name, cpu, pm, handles, StartTime $DataTable = [System.Data.DataTable]::new('Test') @@ -12,6 +13,8 @@ Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking I $null = $DataTable.Columns.Add('PM', [Long]) $null = $DataTable.Columns.Add('Handles', [Int]) $null = $DataTable.Columns.Add('StartTime', [DateTime]) + Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet4 -force -TableName "Data" -WarningVariable WVOne -WarningAction SilentlyContinue + Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue foreach ($r in $results) { $null = $DataTable.Rows.Add($r.name, $r.CPU, $R.PM, $r.Handles, $r.StartTime) } @@ -23,10 +26,21 @@ Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking I Close-ExcelPackage $NowPkg Export-Excel -Path $path -InputObject $results -WorksheetName Sheet1 -RangeName "Whole" Export-Excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange - Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data" - $DataTable.Rows.Clear() - Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet4 -force -TableName "Data" -WarningVariable WVOne -WarningAction SilentlyContinue - Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue + Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data" -WarningVariable WVThree -WarningAction SilentlyContinue + + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet1 -Append + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet1 -Append + + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet2 -Append -TableName "FirstLot" -TableStyle light7 + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet2 -Append + + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet3 -Append + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet3 -Append -TableName "SecondLot" + + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet4 -Append + Send-SQLDataToExcel -Path $path2 -DataTable $DataTable -WorksheetName Sheet4 -Append -TableStyle Dark5 + + $excel = Open-ExcelPackage $path $sheet = $excel.Sheet1 } @@ -97,12 +111,15 @@ Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking I } it "Created a table " { $sheet.Tables.count | should be 1 - $sheet.Tables[0].Name | should be "Data" $sheet.Tables[0].Columns[4].name | should be "StartTime" } it "Formatted date fields with date type " { $sheet.Cells["E11"].Style.Numberformat.NumFmtID | should be 22 } + it "Handled two data tables with the same name " { + $sheet.Tables[0].Name | should be "Data_" + $wvThree[0] | should match "is not unique" + } } $Sheet = $excel.Sheet4 Context "Zero-row Data Table sent with Send-SQLDataToExcel -Force" { @@ -116,10 +133,9 @@ Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking I } it "Applied table formatting " { $sheet.Tables.Count | should be 1 + $sheet.Tables[0].Name | should be "Data" } - it "Handled two data tables with the same name " { - $wvone[1] | should match "is not unique" - } + } $Sheet = $excel.Sheet5 @@ -130,6 +146,39 @@ Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking I } } + Close-ExcelPackage $excel + $excel = Open-ExcelPackage $path2 + Context "Send-SQLDataToExcel -append works correctly" { + it "Works without table settings " { + $excel.sheet1.Dimension.Address | should be "A1:E21" + $excel.sheet1.cells[1,1].value | should be "Name" + $excel.sheet1.cells[12,1].value | should be $excel.sheet1.cells[2,1].value + $excel.sheet1.Tables.count | should be 0 + } + it "Extends an existing table when appending " { + $excel.sheet2.Dimension.Address | should be "A1:E21" + $excel.sheet2.cells[1,2].value | should be "CPU" + $excel.sheet2.cells[13,2].value | should be $excel.sheet2.cells[3,2].value + $excel.sheet2.Tables.count | should be 1 + $excel.sheet2.Tables[0].name | should be "FirstLot" + $excel.sheet2.Tables[0].StyleName | should be "TableStyleLight7" + } + it "Creates a new table by name when appending " { + $excel.sheet3.cells[1,3].value | should be "PM" + $excel.sheet3.cells[14,3].value | should be $excel.sheet3.cells[4,3].value + $excel.sheet3.Tables.count | should be 1 + $excel.sheet3.Tables[0].name | should be "SecondLot" + $excel.sheet3.Tables[0].StyleName | should be "TableStyleMedium6" + } + it "Creates a new table by style when appending " { + $excel.sheet4.cells[1,4].value | should be "Handles" + $excel.sheet4.cells[15,4].value | should be $excel.sheet4.cells[5,4].value + $excel.sheet4.Tables.count | should be 1 + $excel.sheet4.Tables[0].name | should be "Table1" + $excel.sheet4.Tables[0].StyleName | should be "TableStyleDark5" + } + } + Close-ExcelPackage $excel Context "Import As Text returns text values" { $x = import-excel $path -WorksheetName sheet3 -AsText StartTime,hand* | Select-Object -last 1