mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 13:23:29 +00:00
Handles piping in the a list of xlsx files
This commit is contained in:
@@ -1,13 +1,36 @@
|
|||||||
Function Remove-WorkSheet {
|
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(
|
param(
|
||||||
[Parameter(Mandatory)]
|
# [Parameter(ValueFromPipelineByPropertyName)]
|
||||||
$Path,
|
[Parameter(ValueFromPipelineByPropertyName)]
|
||||||
[Parameter(Mandatory)]
|
[Alias('Path')]
|
||||||
[String[]]$WorksheetName,
|
$FullName,
|
||||||
|
[String[]]$WorksheetName = "Sheet1",
|
||||||
[Switch]$Show
|
[Switch]$Show
|
||||||
)
|
)
|
||||||
|
|
||||||
$pkg = Open-ExcelPackage -Path $Path
|
Process {
|
||||||
|
if (!$FullName) {
|
||||||
|
throw "Remove-WorkSheet requires the and Excel file"
|
||||||
|
}
|
||||||
|
|
||||||
|
$pkg = Open-ExcelPackage -Path $FullName
|
||||||
|
|
||||||
if ($pkg) {
|
if ($pkg) {
|
||||||
foreach ($wsn in $WorksheetName) {
|
foreach ($wsn in $WorksheetName) {
|
||||||
@@ -16,4 +39,5 @@
|
|||||||
|
|
||||||
Close-ExcelPackage -ExcelPackage $pkg -Show:$Show
|
Close-ExcelPackage -ExcelPackage $pkg -Show:$Show
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,26 +3,76 @@ Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
|||||||
|
|
||||||
Describe "Remove Worksheet" {
|
Describe "Remove Worksheet" {
|
||||||
Context "Remove a worksheet output" {
|
Context "Remove a worksheet output" {
|
||||||
BeforeAll {
|
BeforeEach {
|
||||||
# Create three sheets
|
# Create three sheets
|
||||||
$data = ConvertFrom-Csv @"
|
$data = ConvertFrom-Csv @"
|
||||||
|
Name,Age
|
||||||
|
Jane,10
|
||||||
|
John,20
|
||||||
"@
|
"@
|
||||||
$xlFile = "$env:TEMP\RemoveWorsheet.xlsx"
|
$xlFile1 = "$env:TEMP\RemoveWorsheet1.xlsx"
|
||||||
Remove-Item $xlFile -ErrorAction SilentlyContinue
|
Remove-Item $xlFile1 -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
$data | Export-Excel -Path $xlFile -WorksheetName Target1
|
$data | Export-Excel -Path $xlFile1 -WorksheetName Target1
|
||||||
$data | Export-Excel -Path $xlFile -WorksheetName Target2
|
$data | Export-Excel -Path $xlFile1 -WorksheetName Target2
|
||||||
$data | Export-Excel -Path $xlFile -WorksheetName Target3
|
$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" {
|
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[0].Name | Should Be "Target1"
|
||||||
$actual[1].Name | Should Be "Target3"
|
$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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user