forked from dfinke/ImportExcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-ExcelWorksheet.Tests.ps1
More file actions
149 lines (133 loc) · 8.03 KB
/
Copy pathCopy-ExcelWorksheet.Tests.ps1
File metadata and controls
149 lines (133 loc) · 8.03 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
param()
Describe "Copy-Worksheet" {
BeforeAll {
$path1 = "TestDrive:\Test1.xlsx"
$path2 = "TestDrive:\Test2.xlsx"
Remove-Item -Path $path1, $path2 -ErrorAction SilentlyContinue
$ProcRange = Get-Process | Export-Excel $path1 -DisplayPropertySet -WorkSheetname Processes -ReturnRange
if ((Get-Culture).NumberFormat.CurrencySymbol -eq "£") { $OtherCurrencySymbol = "$" }
else { $OtherCurrencySymbol = "£" }
[PSCustOmobject][Ordered]@{
Date = Get-Date
Formula1 = '=SUM(F2:G2)'
String1 = 'My String'
Float = [math]::pi
IPAddress = '10.10.25.5'
StrLeadZero = '07670'
StrComma = '0,26'
StrEngThousand = '1,234.56'
StrEuroThousand = '1.555,83'
StrDot = '1.2'
StrNegInt = '-31'
StrTrailingNeg = '31-'
StrParens = '(123)'
strLocalCurrency = ('{0}123.45' -f (Get-Culture).NumberFormat.CurrencySymbol )
strOtherCurrency = ('{0}123.45' -f $OtherCurrencySymbol )
StrE164Phone = '+32 (444) 444 4444'
StrAltPhone1 = '+32 4 4444 444'
StrAltPhone2 = '+3244444444'
StrLeadSpace = ' 123'
StrTrailSpace = '123 '
Link1 = [uri]"https://github.com/dfinke/ImportExcel"
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
}
Context "Simplest copy" {
BeforeAll {
$path1 = "TestDrive:\Test1.xlsx"
$path2 = "TestDrive:\Test2.xlsx"
Copy-ExcelWorksheet -SourceWorkbook $path1 -DestinationWorkbook $path2
$excel = Open-ExcelPackage -Path $path2
$ws = $excel.Workbook.Worksheets["Processes"]
}
it "Inserted a worksheet " {
$Excel.Workbook.Worksheets.count | Should -Be 2
$ws | Should -Not -BenullorEmpty
$ws.Dimension.Address | Should -Be $ProcRange
}
}
Context "Mixed types using a package object" {
BeforeAll {
$excel = Open-ExcelPackage -Path $path2
Copy-ExcelWorksheet -SourceWorkbook $excel -DestinationWorkbook $excel -DestinationWorkSheet "CopyOfMixedTypes"
Close-ExcelPackage -ExcelPackage $excel
$excel = Open-ExcelPackage -Path $path2
$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
$ws | Should -Not -BenullorEmpty
$ws.Name | Should -Be "CopyOfMixedTypes"
$ws.Dimension.Columns | Should -Be 22
$ws.Dimension.Rows | Should -Be 2
}
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, 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
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'
}
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, 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, 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 = "TestDrive:\reports.xlsx"
$xlfileArchive = "TestDrive:\reportsArchive.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
Remove-Item $xlfileArchive -ErrorAction SilentlyContinue
$sheets = "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 = "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
}
}
Context "Copy worksheet should support piped input" {
BeforeAll {
$xlfile = "TestDrive:\reports.xlsx"
$xlfileArchive = "TestDrive:\reportsArchive.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
Remove-Item $xlfileArchive -ErrorAction SilentlyContinue
$sheets = "1.1.2019", "1.2.2019", "1.3.2019", "1.4.2019", "1.5.2019"
$sheets | ForEach-Object {
"Hello World" | Export-Excel $xlfile -WorksheetName $_
}
$e = Open-ExcelPackage $xlfile
$e.Workbook.Worksheets | Copy-ExcelWorksheet -DestinationWorkbook $xlfileArchive
Close-ExcelPackage -NoSave $e
}
it "Should copy sheets piped into the command " {
$excel = Open-ExcelPackage $xlfileArchive
$excel.Workbook.Worksheets.Count | should -be 5
$excel.Workbook.Worksheets['1.3.2019'].Cells['A1'].Value | should -be 'Hello World'
}
}
}