sync export changes from master into refactor

This commit is contained in:
jhoneill
2019-11-21 20:35:17 +00:00
parent e42f23cd7c
commit 5595aee407

View File

@@ -2,7 +2,7 @@
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([OfficeOpenXml.ExcelPackage])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
Param(
param(
[Parameter(ParameterSetName = 'Default', Position = 0)]
[String]$Path,
[Parameter(Mandatory = $true, ParameterSetName = "Package")]
@@ -111,7 +111,7 @@
if (-not $PSBoundParameters.ContainsKey("TableName") -and
-not $PSBoundParameters.ContainsKey("TableStyle") -and
-not $AutoFilter) {
$TableName = 'Table1'
$TableName = '' # later rely on distinction between NULL and ""
}
}
if ($ExcelPackage) {
@@ -137,6 +137,7 @@
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
#using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | ForEach-Object -Begin {$Script:header = @()} -Process {$Script:header += $_ }
$NoHeader = $true
#if we did not get AutoNameRange, but headers have ranges of the same name make autoNameRange True, otherwise make it false
if (-not $AutoNameRange) {
$AutoNameRange = $true ; foreach ($h in $header) {if ($ws.names.name -notcontains $h) {$AutoNameRange = $false} }
@@ -156,7 +157,7 @@
$existingTable = $ws.Tables.Where({$_.address.address -eq $ws.dimension.address},'First', 1)
if ($null -eq $TableName -and $existingTable) {
$TableName = $existingTable.Name
$TableStyle = $existingTable.Tablestyle
$TableStyle = $existingTable.StyleName -replace "^TableStyle",""
$AutoFilter = $false
}
#if we did not get $autofilter but a filter range is set and it covers the right area, set autofilter to true
@@ -204,10 +205,10 @@
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]) {
if ($Append) {
if ($Append -and $ws.dimension) {
$row ++
$null = $ws.Cells[$row,$StartColumn].LoadFromDataTable($InputObject, $false )
if ($TableName) {
if ($TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle
}
}
@@ -349,7 +350,6 @@
else {
$ws.Cells[$Row, $ColumnIndex].Value = $v
}
}
}
catch {Write-Warning -Message "Could not insert the '$Name' property at Row $Row, Column $ColumnIndex"}
@@ -360,7 +360,6 @@
}
}
catch {throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
}}
end {
@@ -428,6 +427,7 @@
}
catch {Write-Warning -Message "Failed adding autofilter to worksheet '$WorksheetName': $_"}
}
if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$params = $item.value
@@ -646,7 +646,6 @@
$ws.Protection.SetPassword($Password)
Write-Verbose -Message 'Set password on workbook'
}
catch {throw "Failed setting password for worksheet '$WorksheetName': $_"}
}
@@ -679,6 +678,5 @@
if ($Show) { Invoke-Item $Path }
}
}
}