Replaced -Force switch with -NoClobber switch

This commit is contained in:
Doug Finke
2015-04-10 14:32:48 -04:00
parent ea463c9622
commit e32095d88d
2 changed files with 14 additions and 4 deletions

View File

@@ -59,7 +59,7 @@ function Export-Excel {
[Switch]$IncludePivotChart, [Switch]$IncludePivotChart,
[Switch]$AutoSize, [Switch]$AutoSize,
[Switch]$Show, [Switch]$Show,
[Switch]$Force [Switch]$NoClobber
) )
Begin { Begin {
@@ -68,15 +68,24 @@ function Export-Excel {
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path $pkg = New-Object OfficeOpenXml.ExcelPackage $Path
if($pkg.Workbook.Worksheets[$WorkSheetname]) { if($pkg.Workbook.Worksheets[$WorkSheetname]) {
if($NoClobber) {
$AlreadyExists = $true
throw ""
} else {
$pkg.Workbook.Worksheets.delete($WorkSheetname) $pkg.Workbook.Worksheets.delete($WorkSheetname)
} }
}
$ws = $pkg.Workbook.Worksheets.Add($WorkSheetname) $ws = $pkg.Workbook.Worksheets.Add($WorkSheetname)
$Row = 1 $Row = 1
} Catch { } Catch {
if($AlreadyExists) {
throw "$WorkSheetname already exists."
} else {
throw $Error[0].Exception.InnerException throw $Error[0].Exception.InnerException
} }
} }
}
Process { Process {

View File

@@ -16,6 +16,7 @@ What's new
* Renamed `AutoFitColumns` to `AutoSize` * Renamed `AutoFitColumns` to `AutoSize`
* Implemented `Export-MultipleExcelSheets` * Implemented `Export-MultipleExcelSheets`
* Implemented `-Password` for a worksheet * Implemented `-Password` for a worksheet
* Repalce `-Force` switch with `-NoClobber` switch
#### Examples #### Examples
`gsv | Export-Excel .\test.xlsx -WorkSheetname Services` `gsv | Export-Excel .\test.xlsx -WorkSheetname Services`