forked from dfinke/ImportExcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveWorksheet.ps1
More file actions
34 lines (24 loc) · 839 Bytes
/
RemoveWorksheet.ps1
File metadata and controls
34 lines (24 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Function Remove-WorkSheet {
Param (
$Path,
$WorksheetName
)
$Path = (Resolve-Path $Path).ProviderPath
$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage $Path
$workSheet = $Excel.Workbook.Worksheets[$WorkSheetName]
if($workSheet) {
if($Excel.Workbook.Worksheets.Count -gt 1) {
$Excel.Workbook.Worksheets.Delete($workSheet)
} else {
throw "Cannot delete $WorksheetName. A workbook must contain at least one visible worksheet"
}
} else {
throw "$WorksheetName not found"
}
$Excel.Save()
$Excel.Dispose()
}
Import-Module .\ImportExcel.psd1 -Force
$names = Get-ExcelSheetInfo C:\Temp\testDelete.xlsx
$names | % { Remove-WorkSheet C:\Temp\testDelete.xlsx $_.Name}
##Remove-WorkSheet C:\Temp\testDelete.xlsx sheet6