RemoveWorksheet.ps1

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
35
36
37
38
39
40
41
42
43
Function Remove-WorkSheet {
    <#
      .SYNOPSIS
        Removes one or more worksheets from one or more workbooks
      .EXAMPLE
        C:\> Remove-WorkSheet -Path Test1.xlsx -WorksheetName Sheet1
        Removes the worksheet named 'Sheet1' from 'Test1.xlsx'
 
        C:\> Remove-WorkSheet -Path Test1.xlsx -WorksheetName Sheet1,Target1
        Removes the worksheet named 'Sheet1' and 'Target1' from 'Test1.xlsx'
 
        C:\> Remove-WorkSheet -Path Test1.xlsx -WorksheetName Sheet1,Target1 -Show
        Removes the worksheets and then launches the xlsx in Excel
 
        C:\> dir c:\reports\*.xlsx | Remove-WorkSheet
        Removes 'Sheet1' from all the xlsx files in the c:\reports directory
 
#>

    param(
        # [Parameter(ValueFromPipelineByPropertyName)]
        [Parameter(ValueFromPipelineByPropertyName)]
        [Alias('Path')]
        $FullName,
        [String[]]$WorksheetName = "Sheet1",
        [Switch]$Show
    )

    Process {
        if (!$FullName) {
            throw "Remove-WorkSheet requires the and Excel file"
        }

        $pkg = Open-ExcelPackage -Path $FullName

        if ($pkg) {
            foreach ($wsn in $WorksheetName) {
                $pkg.Workbook.Worksheets.Delete($wsn)
            }

            Close-ExcelPackage -ExcelPackage $pkg -Show:$Show
        }
    }
}