mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-16 00:04:24 +00:00
Cleaned up error checking for worksheet replacement
This commit is contained in:
@@ -88,6 +88,28 @@ function Export-ExcelSheet {
|
|||||||
$xl.Dispose()
|
$xl.Dispose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Add-WorkSheet {
|
||||||
|
param(
|
||||||
|
#TODO Use parametersets to allow a workbook to be passed instead of a package
|
||||||
|
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
|
||||||
|
[OfficeOpenXml.ExcelPackage] $ExcelPackage,
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string] $WorkSheetname,
|
||||||
|
[Switch] $NoClobber
|
||||||
|
)
|
||||||
|
if($ExcelPackage.Workbook.Worksheets[$WorkSheetname]) {
|
||||||
|
if($NoClobber) {
|
||||||
|
$AlreadyExists = $true
|
||||||
|
Write-Error "Worksheet `"$WorkSheetname`" already exists."
|
||||||
|
} else {
|
||||||
|
Write-Debug "Worksheet `"$WorkSheetname`" already exists. Deleting"
|
||||||
|
$ExcelPackage.Workbook.Worksheets.Delete($WorkSheetname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname)
|
||||||
|
}
|
||||||
|
|
||||||
function Export-Excel {
|
function Export-Excel {
|
||||||
<#
|
<#
|
||||||
.Synopsis
|
.Synopsis
|
||||||
@@ -124,24 +146,19 @@ function Export-Excel {
|
|||||||
[Switch]$NoClobber,
|
[Switch]$NoClobber,
|
||||||
[Switch]$FreezeTopRow,
|
[Switch]$FreezeTopRow,
|
||||||
[Switch]$AutoFilter,
|
[Switch]$AutoFilter,
|
||||||
[Switch]$BoldTopRow
|
[Switch]$BoldTopRow,
|
||||||
|
[string]$RangeName
|
||||||
)
|
)
|
||||||
|
|
||||||
Begin {
|
Begin {
|
||||||
try {
|
try {
|
||||||
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
||||||
|
if (Test-Path $path) {
|
||||||
|
Write-Debug "File `"$Path`" already exists"
|
||||||
|
}
|
||||||
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
||||||
|
|
||||||
if($pkg.Workbook.Worksheets[$WorkSheetname]) {
|
$ws = $pkg | Add-WorkSheet -WorkSheetname $WorkSheetname -NoClobber:$NoClobber
|
||||||
if($NoClobber) {
|
|
||||||
$AlreadyExists = $true
|
|
||||||
throw ""
|
|
||||||
} else {
|
|
||||||
$pkg.Workbook.Worksheets.delete($WorkSheetname)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$ws = $pkg.Workbook.Worksheets.Add($WorkSheetname)
|
|
||||||
|
|
||||||
$Row = 1
|
$Row = 1
|
||||||
if($Title) {
|
if($Title) {
|
||||||
@@ -196,19 +213,20 @@ function Export-Excel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
End {
|
End {
|
||||||
|
$startAddress=$ws.Dimension.Start.Address
|
||||||
|
$dataRange="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
|
||||||
|
Write-Debug "Data Range $dataRange"
|
||||||
|
|
||||||
if($IncludePivotTable) {
|
if($IncludePivotTable) {
|
||||||
$pivotTableName = $WorkSheetname + "PivotTable"
|
$pivotTableName = $WorkSheetname + "PivotTable"
|
||||||
$wsPivot = $pkg.Workbook.Worksheets.Add($pivotTableName)
|
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
||||||
|
|
||||||
$wsPivot.View.TabSelected = $true
|
$wsPivot.View.TabSelected = $true
|
||||||
|
|
||||||
$pivotTableDataName=$WorkSheetname + "PivotTableData"
|
$pivotTableDataName=$WorkSheetname + "PivotTableData"
|
||||||
|
|
||||||
$startAddress=$ws.Dimension.Start.Address
|
|
||||||
if($Title) {$startAddress="A2"}
|
if($Title) {$startAddress="A2"}
|
||||||
|
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$dataRange], $pivotTableDataName)
|
||||||
$range="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
|
|
||||||
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$range], $pivotTableDataName)
|
|
||||||
|
|
||||||
if($PivotRows) {
|
if($PivotRows) {
|
||||||
foreach ($Row in $PivotRows) {
|
foreach ($Row in $PivotRows) {
|
||||||
@@ -238,9 +256,7 @@ function Export-Excel {
|
|||||||
if($Password) { $ws.Protection.SetPassword($Password) }
|
if($Password) { $ws.Protection.SetPassword($Password) }
|
||||||
|
|
||||||
if($AutoFilter) {
|
if($AutoFilter) {
|
||||||
$startAddress=$ws.Dimension.Start.Address
|
$ws.Cells[$dataRange].AutoFilter=$true
|
||||||
$range="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address
|
|
||||||
$ws.Cells[$range].AutoFilter=$true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($FreezeTopRow) {
|
if($FreezeTopRow) {
|
||||||
|
|||||||
Reference in New Issue
Block a user