forked from dfinke/ImportExcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportExcelHeaderName.tests.ps1
More file actions
196 lines (143 loc) · 6.64 KB
/
Copy pathImportExcelHeaderName.tests.ps1
File metadata and controls
196 lines (143 loc) · 6.64 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
$xlfile = "TestDrive:\testImportExcel.xlsx"
Describe "Import-Excel on a sheet with no headings" {
BeforeAll {
$xl = "" | export-excel $xlfile -PassThru
Set-Format -WorkSheet $xl.Sheet1 -Range A1 -Value 'A'
Set-Format -WorkSheet $xl.Sheet1 -Range B1 -Value 'B'
Set-Format -WorkSheet $xl.Sheet1 -Range C1 -Value 'C'
Set-Format -WorkSheet $xl.Sheet1 -Range A2 -Value 'D'
Set-Format -WorkSheet $xl.Sheet1 -Range B2 -Value 'E'
Set-Format -WorkSheet $xl.Sheet1 -Range C2 -Value 'F'
Set-Format -WorkSheet $xl.Sheet1 -Range A3 -Value 'G'
Set-Format -WorkSheet $xl.Sheet1 -Range B3 -Value 'H'
Set-Format -WorkSheet $xl.Sheet1 -Range C3 -Value 'I'
Close-ExcelPackage $xl
}
It "Import-Excel should have this shape" {
$actual = @(Import-Excel $xlfile)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'A'
$actualNames[1] | Should BeExactly 'B'
$actualNames[2] | Should BeExactly 'C'
$actual.Count | Should Be 2
$actual[0].A | Should BeExactly 'D'
$actual[0].B | Should BeExactly 'E'
$actual[0].C | Should BeExactly 'F'
}
It "Import-Excel -NoHeader should have this shape" {
$actual = @(Import-Excel $xlfile -NoHeader)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'P1'
$actualNames[1] | Should BeExactly 'P2'
$actualNames[2] | Should BeExactly 'P3'
$actual.Count | Should Be 3
}
It "Import-Excel -HeaderName should have this shape" {
$actual = @(Import-Excel $xlfile -HeaderName 'Q', 'R', 'S')
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'Q'
$actualNames[1] | Should BeExactly 'R'
$actualNames[2] | Should BeExactly 'S'
$actual.Count | Should Be 3
$actual[0].Q | Should BeExactly 'A'
$actual[0].R | Should BeExactly 'B'
$actual[0].S | Should BeExactly 'C'
$actual[1].Q | Should BeExactly 'D'
$actual[1].R | Should BeExactly 'E'
$actual[1].S | Should BeExactly 'F'
}
It "Should work with StartRow" {
$actual = @(Import-Excel $xlfile -HeaderName 'Q', 'R', 'S' -startrow 2)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'Q'
$actualNames[1] | Should BeExactly 'R'
$actualNames[2] | Should BeExactly 'S'
$actual.Count | Should Be 2
$actual[0].Q | Should BeExactly 'D'
$actual[0].R | Should BeExactly 'E'
$actual[0].S | Should BeExactly 'F'
$actual[1].Q | Should BeExactly 'G'
$actual[1].R | Should BeExactly 'H'
$actual[1].S | Should BeExactly 'I'
}
It "Should work with -NoHeader" {
$actual = @(Import-Excel $xlfile -NoHeader)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'P1'
$actualNames[1] | Should BeExactly 'P2'
$actualNames[2] | Should BeExactly 'P3'
$actual.Count | Should Be 3
$actual[0].P1 | Should BeExactly 'A'
$actual[0].P2 | Should BeExactly 'B'
$actual[0].P3 | Should BeExactly 'C'
$actual[1].P1 | Should BeExactly 'D'
$actual[1].P2 | Should BeExactly 'E'
$actual[1].P3 | Should BeExactly 'F'
$actual[2].P1 | Should BeExactly 'G'
$actual[2].P2 | Should BeExactly 'H'
$actual[2].P3 | Should BeExactly 'I'
}
It "Should work with -NoHeader -DataOnly" {
$actual = @(Import-Excel $xlfile -NoHeader -DataOnly)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'P1'
$actualNames[1] | Should BeExactly 'P2'
$actualNames[2] | Should BeExactly 'P3'
$actual.Count | Should Be 3
$actual[0].P1 | Should BeExactly 'A'
$actual[0].P2 | Should BeExactly 'B'
$actual[0].P3 | Should BeExactly 'C'
$actual[1].P1 | Should BeExactly 'D'
$actual[1].P2 | Should BeExactly 'E'
$actual[1].P3 | Should BeExactly 'F'
$actual[2].P1 | Should BeExactly 'G'
$actual[2].P2 | Should BeExactly 'H'
$actual[2].P3 | Should BeExactly 'I'
}
It "Should work with -HeaderName -DataOnly -StartRow" {
$actual = @(Import-Excel $xlfile -HeaderName 'Q', 'R', 'S' -DataOnly -StartRow 2)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'Q'
$actualNames[1] | Should BeExactly 'R'
$actualNames[2] | Should BeExactly 'S'
$actual.Count | Should Be 1
$actual[0].Q | Should BeExactly 'G'
$actual[0].R | Should BeExactly 'H'
$actual[0].S | Should BeExactly 'I'
}
It "Should" {
$xlfile = "TestDrive:\testImportExcelSparse.xlsx"
$xl = "" | export-excel $xlfile -PassThru
Set-Format -WorkSheet $xl.Sheet1 -Range A1 -Value 'Chuck'
Set-Format -WorkSheet $xl.Sheet1 -Range B1 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range C1 -Value 'Norris'
Set-Format -WorkSheet $xl.Sheet1 -Range D1 -Value 'California'
Set-Format -WorkSheet $xl.Sheet1 -Range A2 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range B2 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range C2 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range D2 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range A3 -Value 'Jean-Claude'
Set-Format -WorkSheet $xl.Sheet1 -Range B3 -Value ''
Set-Format -WorkSheet $xl.Sheet1 -Range C3 -Value 'Vandamme'
Set-Format -WorkSheet $xl.Sheet1 -Range D3 -Value 'Brussels'
Close-ExcelPackage $xl
$actual = @(Import-Excel -Path $xlfile -DataOnly -HeaderName 'FirstName', 'SecondName', 'City' -StartRow 2)
$actualNames = $actual[0].psobject.properties.name
$actualNames.Count | Should Be 3
$actualNames[0] | Should BeExactly 'FirstName'
$actualNames[1] | Should BeExactly 'SecondName'
$actualNames[2] | Should BeExactly 'City'
$actual.Count | Should Be 1
# Looks like -DataOnly does not handle empty columns
# $actual[0].FirstName | Should BeExactly 'Jean-Claude'
# $actual[0].SecondName | Should BeExactly 'Vandamme'
# $actual[0].City | Should BeExactly 'Brussels'
}
}