Skip to content

Commit

Permalink
Merge pull request #514 from dfinke/AddRemoveWorksheet-dcf
Browse files Browse the repository at this point in the history
Add Remove-Workseet
  • Loading branch information
dfinke authored Dec 30, 2018
2 parents 9560ea8 + 0fbe9db commit d1f4101
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ImportExcel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
RootModule = 'ImportExcel.psm1'

# Version number of this module.
ModuleVersion = '5.4.2'
ModuleVersion = '5.4.3'

# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
Expand Down
1 change: 1 addition & 0 deletions ImportExcel.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\Open-ExcelPackage.ps1
. $PSScriptRoot\Pivot.ps1
. $PSScriptRoot\PivotTable.ps1
. $PSScriptRoot\RemoveWorksheet.ps1
. $PSScriptRoot\Send-SQLDataToExcel.ps1
. $PSScriptRoot\Set-CellStyle.ps1
. $PSScriptRoot\Set-Column.ps1
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ Install-Module ImportExcel -scope CurrentUser
Install-Module ImportExcel
```

# What's new 5.4.3

- Added Remove-Worksheet: Removes one or more worksheets from one or more workbooks


# What's new 5.4.2

- Added parameters -GroupDateRow and -GroupDatePart & -GroupNumericRow, -GroupNumericMin, -GroupNumericMax and -GroupNumericInterval
Expand Down
61 changes: 35 additions & 26 deletions RemoveWorksheet.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
Function Remove-WorkSheet {
Param (
$Path,
$WorksheetName
<#
.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
)

$Path = (Resolve-Path $Path).ProviderPath
Process {
if (!$FullName) {
throw "Remove-WorkSheet requires the and Excel file"
}

$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage $Path
$pkg = Open-ExcelPackage -Path $FullName

$workSheet = $Excel.Workbook.Worksheets[$WorkSheetName]
if ($pkg) {
foreach ($wsn in $WorksheetName) {
$pkg.Workbook.Worksheets.Delete($wsn)
}

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"
Close-ExcelPackage -ExcelPackage $pkg -Show:$Show
}

} else {
throw "$WorksheetName not found"
}

$Excel.Save()
$Excel.Dispose()
}


Import-Module .\ImportExcel.psd1 -Force

$names = Get-ExcelSheetInfo C:\Temp\testDelete.xlsx
$names | Foreach-Object { Remove-WorkSheet C:\Temp\testDelete.xlsx $_.Name}

##Remove-WorkSheet C:\Temp\testDelete.xlsx sheet6
}
78 changes: 78 additions & 0 deletions __tests__/Remove-WorkSheet.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#Requires -Modules Pester
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force

Describe "Remove Worksheet" {
Context "Remove a worksheet output" {
BeforeEach {
# Create three sheets
$data = ConvertFrom-Csv @"
Name,Age
Jane,10
John,20
"@
$xlFile1 = "$env:TEMP\RemoveWorsheet1.xlsx"
Remove-Item $xlFile1 -ErrorAction SilentlyContinue

$data | Export-Excel -Path $xlFile1 -WorksheetName Target1
$data | Export-Excel -Path $xlFile1 -WorksheetName Target2
$data | Export-Excel -Path $xlFile1 -WorksheetName Target3
$data | Export-Excel -Path $xlFile1 -WorksheetName Sheet1

$xlFile2 = "$env:TEMP\RemoveWorsheet2.xlsx"
Remove-Item $xlFile2 -ErrorAction SilentlyContinue

$data | Export-Excel -Path $xlFile2 -WorksheetName Target1
$data | Export-Excel -Path $xlFile2 -WorksheetName Target2
$data | Export-Excel -Path $xlFile2 -WorksheetName Target3
$data | Export-Excel -Path $xlFile2 -WorksheetName Sheet1
}

it "Should throw about the Path" {
{Remove-WorkSheet} | Should throw 'Remove-WorkSheet requires the and Excel file'
}

it "Should delete Target2" {
Remove-WorkSheet -Path $xlFile1 -WorksheetName Target2

$actual = Get-ExcelSheetInfo -Path $xlFile1

$actual.Count | Should Be 3
$actual[0].Name | Should Be "Target1"
$actual[1].Name | Should Be "Target3"
$actual[2].Name | Should Be "Sheet1"
}

it "Should delete Sheet1" {
Remove-WorkSheet -Path $xlFile1

$actual = Get-ExcelSheetInfo -Path $xlFile1

$actual.Count | Should Be 3
$actual[0].Name | Should Be "Target1"
$actual[1].Name | Should Be "Target2"
$actual[2].Name | Should Be "Target3"
}

it "Should delete multiple sheets" {
Remove-WorkSheet -Path $xlFile1 -WorksheetName Target1, Sheet1

$actual = Get-ExcelSheetInfo -Path $xlFile1

$actual.Count | Should Be 2
$actual[0].Name | Should Be "Target2"
$actual[1].Name | Should Be "Target3"
}

it "Should delete sheet from multiple workbooks" {

Get-ChildItem "$env:TEMP\RemoveWorsheet*.xlsx" | Remove-WorkSheet

$actual = Get-ExcelSheetInfo -Path $xlFile1

$actual.Count | Should Be 3
$actual[0].Name | Should Be "Target1"
$actual[1].Name | Should Be "Target2"
$actual[2].Name | Should Be "Target3"
}
}
}

0 comments on commit d1f4101

Please sign in to comment.