mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Merge pull request #520 from dfinke/FixCopyExcelWorksheetStreamClose-dcf
Fix Copy-ExcelWorkSheet so Remove-WorkSheet works. Resolves #516
This commit is contained in:
@@ -29,12 +29,12 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
#An ExcelWorkbook or ExcelPackage object or the path to an XLSx file where the data is found.
|
||||
[Parameter(Mandatory=$true)]
|
||||
[Parameter(Mandatory = $true)]
|
||||
$SourceWorkbook,
|
||||
#Name or number (starting from 1) of the worksheet in the source workbook (defaults to 1).
|
||||
$SourceWorkSheet = 1 ,
|
||||
#An ExcelWorkbook or ExcelPackage object or the path to an XLSx file where the data should be copied.
|
||||
[Parameter(Mandatory=$true)]
|
||||
[Parameter(Mandatory = $true)]
|
||||
$DestinationWorkbook,
|
||||
#Name of the worksheet in the destination workbook; by default the same as the source worksheet's name. If the sheet exists it will be deleted and re-copied.
|
||||
$DestinationWorkSheet,
|
||||
@@ -65,46 +65,47 @@
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($SourceWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {$sourcews=$SourceWorkbook.Worksheets[$SourceWorkSheet]}
|
||||
elseif ($SourceWorkbook -is [OfficeOpenXml.ExcelPackage] ) {$sourcews=$SourceWorkbook.Workbook.Worksheets[$SourceWorkSheet]}
|
||||
if ($SourceWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {$sourcews = $SourceWorkbook.Worksheets[$SourceWorkSheet]}
|
||||
elseif ($SourceWorkbook -is [OfficeOpenXml.ExcelPackage] ) {$sourcews = $SourceWorkbook.Workbook.Worksheets[$SourceWorkSheet]}
|
||||
else {
|
||||
$SourceWorkbook = (Resolve-Path $SourceWorkbook).ProviderPath
|
||||
try {
|
||||
Write-Verbose "Opening worksheet '$Worksheetname' in Excel workbook '$SourceWorkbook'."
|
||||
$Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $SourceWorkbook, 'Open', 'Read' ,'ReadWrite'
|
||||
$Package1 = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream
|
||||
$sourceWs = $Package1.Workbook.Worksheets[$SourceWorkSheet]
|
||||
}
|
||||
catch {Write-Warning -Message "Could not open $SourceWorkbook" ; return}
|
||||
$SourceWorkbook = (Resolve-Path $SourceWorkbook).ProviderPath
|
||||
try {
|
||||
Write-Verbose "Opening worksheet '$Worksheetname' in Excel workbook '$SourceWorkbook'."
|
||||
$Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $SourceWorkbook, 'Open', 'Read' , 'ReadWrite'
|
||||
$Package1 = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream
|
||||
$sourceWs = $Package1.Workbook.Worksheets[$SourceWorkSheet]
|
||||
}
|
||||
catch {Write-Warning -Message "Could not open $SourceWorkbook" ; return}
|
||||
}
|
||||
if (-not $sourceWs) {Write-Warning -Message "Could not find worksheet '$Sourceworksheet' in the source workbook." ; return}
|
||||
if (-not $sourceWs) {Write-Warning -Message "Could not find worksheet '$Sourceworksheet' in the source workbook." ; return}
|
||||
else {
|
||||
try {
|
||||
if ($DestinationWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {
|
||||
$wb = $DestinationWorkbook
|
||||
}
|
||||
elseif ($DestinationWorkbook -is [OfficeOpenXml.ExcelPackage] ) {
|
||||
$wb = $DestinationWorkbook.workbook
|
||||
if ($show) {$package2 =$DestinationWorkbook}
|
||||
}
|
||||
else {
|
||||
$package2 = Open-ExcelPackage -Create -Path $DestinationWorkbook
|
||||
$wb = $package2.Workbook
|
||||
}
|
||||
if (-not $DestinationWorkSheet) {$DestinationWorkSheet = $SourceWs.Name}
|
||||
if ($wb.Worksheets[$DestinationWorkSheet]) {
|
||||
Write-Verbose "Destination workbook already has a sheet named '$DestinationWorkSheet', deleting it."
|
||||
$wb.Worksheets.Delete($DestinationWorkSheet)
|
||||
}
|
||||
Write-Verbose "Copying $($SourceWorkSheet) from $($SourceWorkbook) to $($DestinationWorkSheet) in $($DestinationWorkbook)"
|
||||
$null = Add-WorkSheet -ExcelWorkbook $wb -WorkSheetname $DestinationWorkSheet -CopySource $sourceWs
|
||||
if ($package1) {Close-ExcelPackage -ExcelPackage $Package1 -NoSave }
|
||||
if ($package2) {Close-ExcelPackage -ExcelPackage $Package2 -Show:$show }
|
||||
if ($show -and $DestinationWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {
|
||||
Write-Warning -Message "-Show only works if the Destination workbook is given as a file path or an ExcelPackage object."
|
||||
}
|
||||
try {
|
||||
if ($DestinationWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {
|
||||
$wb = $DestinationWorkbook
|
||||
}
|
||||
catch {Write-Warning -Message "Could not write to sheet '$DestinationWorkSheet' in the destination workbook" ; return}
|
||||
elseif ($DestinationWorkbook -is [OfficeOpenXml.ExcelPackage] ) {
|
||||
$wb = $DestinationWorkbook.workbook
|
||||
if ($show) {$package2 = $DestinationWorkbook}
|
||||
}
|
||||
else {
|
||||
$package2 = Open-ExcelPackage -Create -Path $DestinationWorkbook
|
||||
$wb = $package2.Workbook
|
||||
}
|
||||
if (-not $DestinationWorkSheet) {$DestinationWorkSheet = $SourceWs.Name}
|
||||
if ($wb.Worksheets[$DestinationWorkSheet]) {
|
||||
Write-Verbose "Destination workbook already has a sheet named '$DestinationWorkSheet', deleting it."
|
||||
$wb.Worksheets.Delete($DestinationWorkSheet)
|
||||
}
|
||||
Write-Verbose "Copying $($SourceWorkSheet) from $($SourceWorkbook) to $($DestinationWorkSheet) in $($DestinationWorkbook)"
|
||||
$null = Add-WorkSheet -ExcelWorkbook $wb -WorkSheetname $DestinationWorkSheet -CopySource $sourceWs
|
||||
if ($Stream) {$Stream.Close() }
|
||||
if ($package1) {Close-ExcelPackage -ExcelPackage $Package1 -NoSave }
|
||||
if ($package2) {Close-ExcelPackage -ExcelPackage $Package2 -Show:$show }
|
||||
if ($show -and $DestinationWorkbook -is [OfficeOpenXml.ExcelWorkbook]) {
|
||||
Write-Warning -Message "-Show only works if the Destination workbook is given as a file path or an ExcelPackage object."
|
||||
}
|
||||
}
|
||||
catch {Write-Warning -Message "Could not write to sheet '$DestinationWorkSheet' in the destination workbook" ; return}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,12 +56,12 @@ Install-Module ImportExcel
|
||||
# What's new 5.4.4
|
||||
|
||||
- Fix issue when only a single property is piped into Export-Excel
|
||||
- Fix issue in `Copy-ExcelWorksheet`, close the `$Stream`
|
||||
|
||||
# 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
|
||||
|
||||
@@ -2,10 +2,10 @@ $path1 = "$env:TEMP\Test1.xlsx"
|
||||
$path2 = "$env:TEMP\Test2.xlsx"
|
||||
Remove-item -Path $path1, $path2 -ErrorAction SilentlyContinue
|
||||
|
||||
$ProcRange = Get-Process | Export-Excel $path1 -DisplayPropertySet -WorkSheetname Processes -ReturnRange
|
||||
$ProcRange = Get-Process | Export-Excel $path1 -DisplayPropertySet -WorkSheetname Processes -ReturnRange
|
||||
|
||||
if ((Get-Culture).NumberFormat.CurrencySymbol -eq "<EFBFBD>") {$OtherCurrencySymbol = "$"}
|
||||
else {$OtherCurrencySymbol = "<EFBFBD>"}
|
||||
if ((Get-Culture).NumberFormat.CurrencySymbol -eq "<EFBFBD>") {$OtherCurrencySymbol = "$"}
|
||||
else {$OtherCurrencySymbol = "<EFBFBD>"}
|
||||
[PSCustOmobject][Ordered]@{
|
||||
Date = Get-Date
|
||||
Formula1 = '=SUM(F2:G2)'
|
||||
@@ -31,7 +31,7 @@ else {$OtherCurrencySymbol = "
|
||||
Link2 = "https://github.com/dfinke/ImportExcel" # Links are not copied correctly, hopefully this will be fixed at some future date
|
||||
} | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -WorkSheetname MixedTypes -Path $path2
|
||||
Describe "Copy-Worksheet" {
|
||||
Context "Simplest copy"{
|
||||
Context "Simplest copy" {
|
||||
BeforeAll {
|
||||
Copy-ExcelWorkSheet -SourceWorkbook $path1 -DestinationWorkbook $path2
|
||||
$excel = Open-ExcelPackage -Path $path2
|
||||
@@ -43,12 +43,12 @@ Describe "Copy-Worksheet" {
|
||||
$ws.Dimension.Address | should be $ProcRange
|
||||
}
|
||||
}
|
||||
Context "Mixed types using a package object"{
|
||||
Context "Mixed types using a package object" {
|
||||
BeforeAll {
|
||||
Copy-ExcelWorkSheet -SourceWorkbook $excel -DestinationWorkbook $excel -DestinationWorkSheet "CopyOfMixedTypes"
|
||||
Close-ExcelPackage -ExcelPackage $excel
|
||||
$excel = Open-ExcelPackage -Path $path2
|
||||
$ws = $Excel.Workbook.Worksheets[3]
|
||||
$ws = $Excel.Workbook.Worksheets[3]
|
||||
}
|
||||
it "Copied a worksheet, giving the expected name, number of rows and number of columns " {
|
||||
$Excel.Workbook.Worksheets.count | Should be 3
|
||||
@@ -60,29 +60,57 @@ Describe "Copy-Worksheet" {
|
||||
it "Copied the expected data into the worksheet " {
|
||||
$ws.Cells[2, 1].Value.Gettype().name | Should be 'DateTime'
|
||||
$ws.Cells[2, 2].Formula | Should be 'SUM(F2:G2)'
|
||||
$ws.Cells[2, 5].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 6].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 5].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 6].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 18].Value.GetType().name | Should be 'String'
|
||||
($ws.Cells[2, 11].Value -is [valuetype] ) | Should be $true
|
||||
($ws.Cells[2, 12].Value -is [valuetype] ) | Should be $true
|
||||
($ws.Cells[2, 13].Value -is [valuetype] ) | Should be $true
|
||||
$ws.Cells[2, 11].Value | Should beLessThan 0
|
||||
$ws.Cells[2, 12].Value | Should beLessThan 0
|
||||
$ws.Cells[2, 13].Value | Should beLessThan 0
|
||||
$ws.Cells[2, 11].Value | Should beLessThan 0
|
||||
$ws.Cells[2, 12].Value | Should beLessThan 0
|
||||
$ws.Cells[2, 13].Value | Should beLessThan 0
|
||||
if ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ",") {
|
||||
($ws.Cells[2, 8].Value -is [valuetype] ) | Should be $true
|
||||
$ws.Cells[2, 9].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 9].Value.GetType().name | Should be 'String'
|
||||
}
|
||||
elseif ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ".") {
|
||||
($ws.Cells[2, 9].Value -is [valuetype] ) | Should be $true
|
||||
$ws.Cells[2, 8].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 8].Value.GetType().name | Should be 'String'
|
||||
}
|
||||
($ws.Cells[2, 14].Value -is [valuetype] ) | Should be $true
|
||||
$ws.Cells[2, 15].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 16].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 17].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 15].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 16].Value.GetType().name | Should be 'String'
|
||||
$ws.Cells[2, 17].Value.GetType().name | Should be 'String'
|
||||
($ws.Cells[2, 19].Value -is [valuetype] ) | Should be $true
|
||||
($ws.Cells[2, 20].Value -is [valuetype] ) | Should be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context "Copy worksheet should close all files" {
|
||||
BeforeAll {
|
||||
$xlfile = "$env:TEMP\reports.xlsx"
|
||||
$xlfileArchive = "$env:TEMP\reportsArchive.xlsx"
|
||||
|
||||
rm $xlfile -ErrorAction SilentlyContinue
|
||||
rm $xlfileArchive -ErrorAction SilentlyContinue
|
||||
|
||||
$sheets = echo 1.1.2019 1.2.2019 1.3.2019 1.4.2019 1.5.2019
|
||||
|
||||
$sheets | ForEach-Object {
|
||||
"Hello World" | Export-Excel $xlfile -WorksheetName $_
|
||||
}
|
||||
}
|
||||
|
||||
it "Should copy and remove sheets" {
|
||||
$targetSheets = echo 1.1.2019 1.4.2019
|
||||
|
||||
$targetSheets | ForEach-Object {
|
||||
Copy-ExcelWorkSheet -SourceWorkbook $xlfile -DestinationWorkbook $xlfileArchive -SourceWorkSheet $_ -DestinationWorkSheet $_
|
||||
}
|
||||
|
||||
$targetSheets | ForEach-Object { Remove-WorkSheet -FullName $xlfile -WorksheetName $_ }
|
||||
|
||||
(Get-ExcelSheetInfo -Path $xlfile ).Count | Should Be 3
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user