From 9db2bc068e7fa7de4b3952d21e762f606131ceac Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 08:29:18 -0500 Subject: [PATCH 1/7] Added to psm1 --- ImportExcel.psm1 | 1 + RemoveWorksheet.ps1 | 34 +--------------------------------- 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index da354159..8a387b8a 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -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 diff --git a/RemoveWorksheet.ps1 b/RemoveWorksheet.ps1 index a62cfd5e..60933521 100644 --- a/RemoveWorksheet.ps1 +++ b/RemoveWorksheet.ps1 @@ -1,34 +1,2 @@ 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 | Foreach-Object { Remove-WorkSheet C:\Temp\testDelete.xlsx $_.Name} - -##Remove-WorkSheet C:\Temp\testDelete.xlsx sheet6 \ No newline at end of file +} \ No newline at end of file From ef9be471ab1c77eab8a7ea44299c7a3d2e994248 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 08:45:51 -0500 Subject: [PATCH 2/7] First test --- __tests__/Remove-WorkSheet.tests.ps1 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 __tests__/Remove-WorkSheet.tests.ps1 diff --git a/__tests__/Remove-WorkSheet.tests.ps1 b/__tests__/Remove-WorkSheet.tests.ps1 new file mode 100644 index 00000000..14529a64 --- /dev/null +++ b/__tests__/Remove-WorkSheet.tests.ps1 @@ -0,0 +1,26 @@ +#Requires -Modules Pester +Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force + +Describe "Remove Worksheet" { + Context "Remove a worksheet output" { + BeforeAll { + # Create three sheets + $data = ConvertFrom-Csv @" +"@ + $xlFile = "$env:TEMP\RemoveWorsheet.xlsx" + Remove-Item $xlFile -ErrorAction SilentlyContinue + + $data | Export-Excel -Path $xlFile -WorksheetName Target1 + $data | Export-Excel -Path $xlFile -WorksheetName Target2 + $data | Export-Excel -Path $xlFile -WorksheetName Target3 + } + + it "Should delete Target2" { + Remove-WorkSheet -Path $xlFile -WorksheetName Target2 + + $actual = (Get-ExcelSheetInfo -Path $xlFile).count + + $actual | Should Be 2 + } + } +} \ No newline at end of file From 56acf56430c200db58a851f2b2e5c9c1330d0912 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 08:52:11 -0500 Subject: [PATCH 3/7] Add code to do removal --- RemoveWorksheet.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/RemoveWorksheet.ps1 b/RemoveWorksheet.ps1 index 60933521..6533d489 100644 --- a/RemoveWorksheet.ps1 +++ b/RemoveWorksheet.ps1 @@ -1,2 +1,19 @@ Function Remove-WorkSheet { + param( + [Parameter(Mandatory)] + $Path, + [Parameter(Mandatory)] + [String[]]$WorksheetName, + [Switch]$Show + ) + + $pkg = Open-ExcelPackage -Path $Path + + if ($pkg) { + foreach ($wsn in $WorksheetName) { + $pkg.Workbook.Worksheets.Delete($wsn) + } + + Close-ExcelPackage -ExcelPackage $pkg -Show:$Show + } } \ No newline at end of file From 9217962306dd6065f69af2f198fb3feb3be9cd60 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 08:52:19 -0500 Subject: [PATCH 4/7] Update and fix tests --- __tests__/Remove-WorkSheet.tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/__tests__/Remove-WorkSheet.tests.ps1 b/__tests__/Remove-WorkSheet.tests.ps1 index 14529a64..b4babc4c 100644 --- a/__tests__/Remove-WorkSheet.tests.ps1 +++ b/__tests__/Remove-WorkSheet.tests.ps1 @@ -18,9 +18,11 @@ Describe "Remove Worksheet" { it "Should delete Target2" { Remove-WorkSheet -Path $xlFile -WorksheetName Target2 - $actual = (Get-ExcelSheetInfo -Path $xlFile).count + $actual = Get-ExcelSheetInfo -Path $xlFile - $actual | Should Be 2 + $actual.Count | Should Be 2 + $actual[0].Name | Should Be "Target1" + $actual[1].Name | Should Be "Target3" } } } \ No newline at end of file From 34c924ae19778e614eaa547666253831b503db4a Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 09:34:44 -0500 Subject: [PATCH 5/7] Handles piping in the a list of xlsx files --- RemoveWorksheet.ps1 | 44 ++++++++++++++---- __tests__/Remove-WorkSheet.tests.ps1 | 68 ++++++++++++++++++++++++---- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/RemoveWorksheet.ps1 b/RemoveWorksheet.ps1 index 6533d489..06c6a9c9 100644 --- a/RemoveWorksheet.ps1 +++ b/RemoveWorksheet.ps1 @@ -1,19 +1,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(Mandatory)] - $Path, - [Parameter(Mandatory)] - [String[]]$WorksheetName, + # [Parameter(ValueFromPipelineByPropertyName)] + [Parameter(ValueFromPipelineByPropertyName)] + [Alias('Path')] + $FullName, + [String[]]$WorksheetName = "Sheet1", [Switch]$Show ) - $pkg = Open-ExcelPackage -Path $Path - - if ($pkg) { - foreach ($wsn in $WorksheetName) { - $pkg.Workbook.Worksheets.Delete($wsn) + Process { + if (!$FullName) { + throw "Remove-WorkSheet requires the and Excel file" } - Close-ExcelPackage -ExcelPackage $pkg -Show:$Show + $pkg = Open-ExcelPackage -Path $FullName + + if ($pkg) { + foreach ($wsn in $WorksheetName) { + $pkg.Workbook.Worksheets.Delete($wsn) + } + + Close-ExcelPackage -ExcelPackage $pkg -Show:$Show + } } } \ No newline at end of file diff --git a/__tests__/Remove-WorkSheet.tests.ps1 b/__tests__/Remove-WorkSheet.tests.ps1 index b4babc4c..bf39a13d 100644 --- a/__tests__/Remove-WorkSheet.tests.ps1 +++ b/__tests__/Remove-WorkSheet.tests.ps1 @@ -3,26 +3,76 @@ Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force Describe "Remove Worksheet" { Context "Remove a worksheet output" { - BeforeAll { + BeforeEach { # Create three sheets $data = ConvertFrom-Csv @" +Name,Age +Jane,10 +John,20 "@ - $xlFile = "$env:TEMP\RemoveWorsheet.xlsx" - Remove-Item $xlFile -ErrorAction SilentlyContinue + $xlFile1 = "$env:TEMP\RemoveWorsheet1.xlsx" + Remove-Item $xlFile1 -ErrorAction SilentlyContinue - $data | Export-Excel -Path $xlFile -WorksheetName Target1 - $data | Export-Excel -Path $xlFile -WorksheetName Target2 - $data | Export-Excel -Path $xlFile -WorksheetName Target3 + $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 $xlFile -WorksheetName Target2 + Remove-WorkSheet -Path $xlFile1 -WorksheetName Target2 - $actual = Get-ExcelSheetInfo -Path $xlFile + $actual = Get-ExcelSheetInfo -Path $xlFile1 - $actual.Count | Should Be 2 + $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" + } } } \ No newline at end of file From e0b2d15c53de658b84bdb62c25dc4b1201710eb0 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 09:38:36 -0500 Subject: [PATCH 6/7] bump version --- ImportExcel.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 0d10c59e..cf754827 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -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' From 0fbe9dbc9bd6e349bcbfbaeb6fe7e21e0d2c569b Mon Sep 17 00:00:00 2001 From: dfinke Date: Sun, 30 Dec 2018 09:38:43 -0500 Subject: [PATCH 7/7] update readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 73e96eaa..a5f96bb3 100644 --- a/README.md +++ b/README.md @@ -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