mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 05:13:49 +00:00
improve table options on export
This commit is contained in:
@@ -422,11 +422,9 @@
|
|||||||
[OutputType([OfficeOpenXml.ExcelPackage])]
|
[OutputType([OfficeOpenXml.ExcelPackage])]
|
||||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
|
||||||
Param(
|
Param(
|
||||||
|
|
||||||
[Parameter(ParameterSetName = 'Default', Position = 0)]
|
[Parameter(ParameterSetName = 'Default', Position = 0)]
|
||||||
[String]$Path,
|
[String]$Path,
|
||||||
[Parameter(Mandatory = $true, ParameterSetName = "Package")]
|
[Parameter(Mandatory = $true, ParameterSetName = "Package")]
|
||||||
|
|
||||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||||
[Parameter(ValueFromPipeline = $true)]
|
[Parameter(ValueFromPipeline = $true)]
|
||||||
[Alias('TargetData')]
|
[Alias('TargetData')]
|
||||||
@@ -462,8 +460,6 @@
|
|||||||
[Switch]$FreezeFirstColumn,
|
[Switch]$FreezeFirstColumn,
|
||||||
[Switch]$FreezeTopRowFirstColumn,
|
[Switch]$FreezeTopRowFirstColumn,
|
||||||
[Int[]]$FreezePane,
|
[Int[]]$FreezePane,
|
||||||
|
|
||||||
|
|
||||||
[Switch]$AutoFilter,
|
[Switch]$AutoFilter,
|
||||||
[Switch]$BoldTopRow,
|
[Switch]$BoldTopRow,
|
||||||
[Switch]$NoHeader,
|
[Switch]$NoHeader,
|
||||||
@@ -473,11 +469,8 @@
|
|||||||
else { $true }
|
else { $true }
|
||||||
})]
|
})]
|
||||||
[String]$RangeName,
|
[String]$RangeName,
|
||||||
|
[Alias('Table')]
|
||||||
|
|
||||||
$TableName,
|
$TableName,
|
||||||
|
|
||||||
|
|
||||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
|
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
|
||||||
[Switch]$Barchart,
|
[Switch]$Barchart,
|
||||||
[Switch]$PieChart,
|
[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.
|
#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 {
|
try {
|
||||||
$script:Header = $null
|
$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}
|
$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("Path")) { $Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp', '.xlsx' }
|
||||||
if (-not $PSBoundParameters.ContainsKey("Show")) { $Show = $true }
|
if (-not $PSBoundParameters.ContainsKey("Show")) { $Show = $true }
|
||||||
if (-not $PSBoundParameters.ContainsKey("AutoSize")) { $AutoSize = $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
|
if (-not $PSBoundParameters.ContainsKey("TableName") -and
|
||||||
-not $PSBoundParameters.ContainsKey("TableStyle") -and
|
-not $PSBoundParameters.ContainsKey("TableStyle") -and
|
||||||
-not $AutoFilter) {
|
-not $AutoFilter) {
|
||||||
@@ -622,18 +617,26 @@
|
|||||||
catch {throw "Failed preparing to export to worksheet '$WorksheetName' to '$Path': $_"}
|
catch {throw "Failed preparing to export to worksheet '$WorksheetName' to '$Path': $_"}
|
||||||
#region Special case -inputobject passed a dataTable object
|
#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 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 #>
|
set things up for the end block, and skip the process block #>
|
||||||
if ($InputObject -is [System.Data.DataTable]) {
|
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) {
|
if ($TableName) {
|
||||||
$InputObject.TableName = $TableName
|
$InputObject.TableName = $TableName
|
||||||
$TableName = $null
|
|
||||||
}
|
}
|
||||||
while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) {
|
while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) {
|
||||||
Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it "
|
Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it "
|
||||||
$InputObject.TableName += "_"
|
$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]})) {
|
foreach ($c in $InputObject.Columns.where({$_.datatype -eq [datetime]})) {
|
||||||
Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time'
|
Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
BeforeAll {
|
||||||
$path = "TestDrive:\Results.xlsx"
|
$path = "TestDrive:\Results.xlsx"
|
||||||
Remove-Item -Path $path -ErrorAction SilentlyContinue
|
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
|
$results = ((Get-Process) + (Get-Process -id $PID)) | Select-Object -last 10 -Property Name, cpu, pm, handles, StartTime
|
||||||
$DataTable = [System.Data.DataTable]::new('Test')
|
$DataTable = [System.Data.DataTable]::new('Test')
|
||||||
$null = $DataTable.Columns.Add('Name')
|
$null = $DataTable.Columns.Add('Name')
|
||||||
@@ -13,11 +13,17 @@ Describe "Exporting with -Inputobject; table handling, Send SQL Data and import
|
|||||||
foreach ($r in $results) {
|
foreach ($r in $results) {
|
||||||
$null = $DataTable.Rows.Add($r.name, $r.CPU, $R.PM, $r.Handles, $r.StartTime)
|
$null = $DataTable.Rows.Add($r.name, $r.CPU, $R.PM, $r.Handles, $r.StartTime)
|
||||||
}
|
}
|
||||||
export-excel -Path $path -InputObject $results -WorksheetName Sheet1 -RangeName "Whole"
|
$NowPkg = Export-Excel -InputObject $DataTable -PassThru
|
||||||
export-excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange
|
$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"
|
Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data"
|
||||||
$DataTable.Rows.Clear()
|
$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
|
Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue
|
||||||
$excel = Open-ExcelPackage $path
|
$excel = Open-ExcelPackage $path
|
||||||
$sheet = $excel.Sheet1
|
$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
|
$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
|
$sheet = $excel.Sheet3
|
||||||
Context "Table of processes via Send-SQLDataToExcel" {
|
Context "Table of processes via Send-SQLDataToExcel" {
|
||||||
it "Put the correct data rows and columns into the sheet " {
|
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
|
$sheet.cells["A3"].Value | should beNullOrEmpty
|
||||||
$wvone[0] | should match "Zero"
|
$wvone[0] | should match "Zero"
|
||||||
}
|
}
|
||||||
|
it "Applied table formatting " {
|
||||||
|
$sheet.Tables.Count | should be 1
|
||||||
|
}
|
||||||
it "Handled two data tables with the same name " {
|
it "Handled two data tables with the same name " {
|
||||||
$wvone[1] | should match "is not unique"
|
$wvone[1] | should match "is not unique"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
$Sheet = $excel.Sheet5
|
$Sheet = $excel.Sheet5
|
||||||
Context "Zero-column Data Table handled by Send-SQLDataToExcel -Force" {
|
Context "Zero-column Data Table handled by Send-SQLDataToExcel -Force" {
|
||||||
|
|||||||
Reference in New Issue
Block a user