diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index b07b055..4459690 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -422,11 +422,9 @@ [OutputType([OfficeOpenXml.ExcelPackage])] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")] Param( - [Parameter(ParameterSetName = 'Default', Position = 0)] [String]$Path, [Parameter(Mandatory = $true, ParameterSetName = "Package")] - [OfficeOpenXml.ExcelPackage]$ExcelPackage, [Parameter(ValueFromPipeline = $true)] [Alias('TargetData')] @@ -462,8 +460,6 @@ [Switch]$FreezeFirstColumn, [Switch]$FreezeTopRowFirstColumn, [Int[]]$FreezePane, - - [Switch]$AutoFilter, [Switch]$BoldTopRow, [Switch]$NoHeader, @@ -473,11 +469,8 @@ else { $true } })] [String]$RangeName, - - + [Alias('Table')] $TableName, - - [OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6, [Switch]$Barchart, [Switch]$PieChart, @@ -525,12 +518,14 @@ #Open the file, get the worksheet, and decide where in the sheet we are writing, and if there is a number format to apply. try { $script:Header = $null - if ($Append -and $ClearSheet) {throw "You can't use -Append AND -ClearSheet."} + if ($Append -and $ClearSheet) {throw "You can't use -Append AND -ClearSheet." ; return} + #To force -Now not to format as a table, allow $false in -TableName to be "No table" $TableName = if ($null -eq $TableName -or ($TableName -is [bool] -and $false -eq $TableName)) { $null } else {[String]$TableName} - if ($PSBoundParameters.Keys.Count -eq 0 -Or $Now -or (-not $Path -and -not $ExcelPackage) ) { + if ($Now -or (-not $Path -and -not $ExcelPackage) ) { if (-not $PSBoundParameters.ContainsKey("Path")) { $Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp', '.xlsx' } if (-not $PSBoundParameters.ContainsKey("Show")) { $Show = $true } if (-not $PSBoundParameters.ContainsKey("AutoSize")) { $AutoSize = $true } + #"Now" option will create a table, unless something passed in TableName/Table Style. False in TableName will block autocreation if (-not $PSBoundParameters.ContainsKey("TableName") -and -not $PSBoundParameters.ContainsKey("TableStyle") -and -not $AutoFilter) { @@ -622,18 +617,26 @@ catch {throw "Failed preparing to export to worksheet '$WorksheetName' to '$Path': $_"} #region Special case -inputobject passed a dataTable object <# If inputObject was passed via the pipeline it won't be visible until the process block, we will only see it here if it was passed as a parameter - if it was passed it is a data table don't do foreach on it (slow) put the whole table in and set dates on date columns, + if it is a data table don't do foreach on it (slow) - put the whole table in and set dates on date columns, set things up for the end block, and skip the process block #> if ($InputObject -is [System.Data.DataTable]) { + #don't leave caller with a renamed table, save the name and set it back later + $orginalTableName = $InputObject.TableName if ($TableName) { $InputObject.TableName = $TableName - $TableName = $null } while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) { Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it " $InputObject.TableName += "_" } - $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader),$TableStyle ) + if ($TableName -or $PSBoundParameters.ContainsKey("TableStyle")) { + $TableName = $null + $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader),$TableStyle ) + } + else { + $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader) ) + } + $InputObject.TableName = $orginalTableName foreach ($c in $InputObject.Columns.where({$_.datatype -eq [datetime]})) { Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time' } diff --git a/__tests__/InputItemParameter.tests.ps1 b/__tests__/InputItemParameter.tests.ps1 index bb0c0cb..28ddc45 100644 --- a/__tests__/InputItemParameter.tests.ps1 +++ b/__tests__/InputItemParameter.tests.ps1 @@ -1,8 +1,8 @@ -Describe "Exporting with -Inputobject; table handling, Send SQL Data and import as " { +Describe "Exporting with -Inputobject, table handling, Send-SQL-Data. Checking Import -asText" { BeforeAll { $path = "TestDrive:\Results.xlsx" Remove-Item -Path $path -ErrorAction SilentlyContinue - . "$PSScriptRoot\Samples\Samples.ps1" + 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') $null = $DataTable.Columns.Add('Name') @@ -13,11 +13,17 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import foreach ($r in $results) { $null = $DataTable.Rows.Add($r.name, $r.CPU, $R.PM, $r.Handles, $r.StartTime) } - export-excel -Path $path -InputObject $results -WorksheetName Sheet1 -RangeName "Whole" - export-excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange + $NowPkg = Export-Excel -InputObject $DataTable -PassThru + $NowPath1 = $NowPkg.File.FullName + Close-ExcelPackage $NowPkg + $NowPkg = Export-Excel -InputObject $DataTable -PassThru -table:$false + $NowPath2 = $NowPkg.File.FullName + 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 -WarningVariable WVOne -WarningAction SilentlyContinue + 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 $excel = Open-ExcelPackage $path $sheet = $excel.Sheet1 @@ -61,6 +67,23 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import $sheet.Cells["E11"].Style.Numberformat.NumFmtID | should be 22 } } + + Context "'Now' Mode behavior" { + $NowPkg = Open-ExcelPackage $NowPath1 + $sheet = $NowPkg.Sheet1 + it "Formatted data as a table by default " { + $sheet.Tables.Count | should be 1 + } + Close-ExcelPackage -NoSave $NowPkg + Remove-Item $NowPath1 + $NowPkg = Open-ExcelPackage $NowPath2 + $sheet = $NowPkg.Sheet1 + it "Did not data as a table when table:`$false was used " { + $sheet.Tables.Count | should be 0 + } + Close-ExcelPackage -NoSave $NowPkg + Remove-Item $NowPath2 + } $sheet = $excel.Sheet3 Context "Table of processes via Send-SQLDataToExcel" { it "Put the correct data rows and columns into the sheet " { @@ -89,9 +112,13 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import $sheet.cells["A3"].Value | should beNullOrEmpty $wvone[0] | should match "Zero" } + it "Applied table formatting " { + $sheet.Tables.Count | should be 1 + } it "Handled two data tables with the same name " { $wvone[1] | should match "is not unique" } + } $Sheet = $excel.Sheet5 Context "Zero-column Data Table handled by Send-SQLDataToExcel -Force" {