diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 53d79f9..b07b055 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -478,7 +478,7 @@ $TableName, - [OfficeOpenXml.Table.TableStyles]$TableStyle, + [OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6, [Switch]$Barchart, [Switch]$PieChart, [Switch]$LineChart , @@ -534,7 +534,7 @@ if (-not $PSBoundParameters.ContainsKey("TableName") -and -not $PSBoundParameters.ContainsKey("TableStyle") -and -not $AutoFilter) { - $TableName = '' + $TableName = 'Table1' } } if ($ExcelPackage) { @@ -625,7 +625,15 @@ 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, set things up for the end block, and skip the process block #> if ($InputObject -is [System.Data.DataTable]) { - $null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, (-not $noHeader) ) + 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 ) foreach ($c in $InputObject.Columns.where({$_.datatype -eq [datetime]})) { Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time' } @@ -808,10 +816,7 @@ #Allow table to be inserted by specifying Name, or Style or both; only process autoFilter if there is no table (they clash). if ($null -ne $TableName) { - if ($PSBoundParameters.ContainsKey('TableStyle')) { - Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle - } - else {Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName} + Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $PSBoundParameters['TableName'] -TableStyle $TableStyle } elseif ($PSBoundParameters.ContainsKey('TableStyle')) { Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName "" -TableStyle $TableStyle diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index 424284f..c7df4cc 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -614,7 +614,7 @@ Describe ExportExcel { $dataWs = $Excel.Workbook.Worksheets["NoOffset"] it "Created a new sheet and auto-extended a table and explicitly extended named ranges " { - $dataWs.Tables["ProcTab"].Address.Address | Should be "A1:E21" + $dataWs.Tables["ProcTab"].Address.Address | Should be "A1:E11" $dataWs.Names["CPU"].Rows | Should be 20 $dataWs.Names["CPU"].Columns | Should be 1 } @@ -657,7 +657,7 @@ Describe ExportExcel { #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 + 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"] @@ -1004,7 +1004,7 @@ Describe ExportExcel { Remove-Item -Path $Path -ErrorAction SilentlyContinue $Processes = Get-Process | Select-Object -first 10 -Property Name, cpu, pm, handles, company - it "Default Set with Path".PadRight(87) { + it "Allows the default parameter set with Path".PadRight(87) { $ExcelPackage = $Processes | Export-Excel -Path $Path -PassThru $Worksheet = $ExcelPackage.Workbook.Worksheets[1] @@ -1013,7 +1013,7 @@ Describe ExportExcel { $Worksheet.Tables | Should BeNullOrEmpty $Worksheet.AutoFilterAddress | Should BeNullOrEmpty } - it "ExcelPackage Set. Path and (ExcelPackage or Now) should throw".PadRight(87) { + 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' @@ -1039,9 +1039,9 @@ Describe ExportExcel { $ExcelPackage = $Processes | Export-Excel -Now -PassThru $Worksheet = $ExcelPackage.Workbook.Worksheets[1] - $ExcelPackage.File | Should BeLike ([IO.Path]::GetTempPath() + '*') - $Worksheet.Tables[0].Name | Should Be 'Table1' - $Worksheet.AutoFilterAddress | Should BeNullOrEmpty + $ExcelPackage.File.FullName | Should BeLike ([IO.Path]::GetTempPath() + '*') + $Worksheet.Tables[0].Name | Should Be 'Table1' + $Worksheet.AutoFilterAddress | Should BeNullOrEmpty if ($isWindows) { $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 } diff --git a/__tests__/InputItemParameter.tests.ps1 b/__tests__/InputItemParameter.tests.ps1 index d1cee88..bb0c0cb 100644 --- a/__tests__/InputItemParameter.tests.ps1 +++ b/__tests__/InputItemParameter.tests.ps1 @@ -15,7 +15,7 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import } 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" + 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 ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue @@ -87,12 +87,15 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import $sheet.cells["A1"].Value | should be "Name" $sheet.cells["E1"].Value | should be "StartTime" $sheet.cells["A3"].Value | should beNullOrEmpty - $wvone | should not beNullOrEmpty + $wvone[0] | should match "Zero" + } + 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" { - it "Put Created a blank Sheet and raised a warning " { + it "Created a blank Sheet and raised a warning " { $sheet.Dimension | should beNullOrEmpty $wvTwo | should not beNullOrEmpty }