1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class WorksheetTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testSetTitle() |
12
|
|
|
{ |
13
|
|
|
$testTitle = str_repeat('a', 31); |
14
|
|
|
|
15
|
|
|
$worksheet = new Worksheet(); |
16
|
|
|
$worksheet->setTitle($testTitle); |
17
|
|
|
self::assertSame($testTitle, $worksheet->getTitle()); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function setTitleInvalidProvider() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
[str_repeat('a', 32), 'Maximum 31 characters allowed in sheet title.'], |
24
|
|
|
['invalid*title', 'Invalid character found in sheet title'], |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $title |
30
|
|
|
* @param string $expectMessage |
31
|
|
|
* @dataProvider setTitleInvalidProvider |
32
|
|
|
*/ |
33
|
|
|
public function testSetTitleInvalid($title, $expectMessage) |
34
|
|
|
{ |
35
|
|
|
// First, test setting title with validation disabled -- should be successful |
36
|
|
|
$worksheet = new Worksheet(); |
37
|
|
|
$worksheet->setTitle($title, true, false); |
38
|
|
|
|
39
|
|
|
// Next, test again with validation enabled -- this time we should fail |
40
|
|
|
$worksheet = new Worksheet(); |
41
|
|
|
$this->expectException(\Exception::class); |
42
|
|
|
$this->expectExceptionMessage($expectMessage); |
43
|
|
|
$worksheet->setTitle($title); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testSetTitleDuplicate() |
47
|
|
|
{ |
48
|
|
|
// Create a Spreadsheet with three Worksheets (the first is created automatically) |
49
|
|
|
$spreadsheet = new Spreadsheet(); |
50
|
|
|
$spreadsheet->createSheet(); |
51
|
|
|
$spreadsheet->createSheet(); |
52
|
|
|
|
53
|
|
|
// Set unique title -- should be unchanged |
54
|
|
|
$sheet = $spreadsheet->getSheet(0); |
55
|
|
|
$sheet->setTitle('Test Title'); |
56
|
|
|
self::assertSame('Test Title', $sheet->getTitle()); |
57
|
|
|
|
58
|
|
|
// Set duplicate title -- should have numeric suffix appended |
59
|
|
|
$sheet = $spreadsheet->getSheet(1); |
60
|
|
|
$sheet->setTitle('Test Title'); |
61
|
|
|
self::assertSame('Test Title 1', $sheet->getTitle()); |
62
|
|
|
|
63
|
|
|
// Set duplicate title with validation disabled -- should be unchanged |
64
|
|
|
$sheet = $spreadsheet->getSheet(2); |
65
|
|
|
$sheet->setTitle('Test Title', true, false); |
66
|
|
|
self::assertSame('Test Title', $sheet->getTitle()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSetCodeName() |
70
|
|
|
{ |
71
|
|
|
$testCodeName = str_repeat('a', 31); |
72
|
|
|
|
73
|
|
|
$worksheet = new Worksheet(); |
74
|
|
|
$worksheet->setCodeName($testCodeName); |
75
|
|
|
self::assertSame($testCodeName, $worksheet->getCodeName()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setCodeNameInvalidProvider() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
[str_repeat('a', 32), 'Maximum 31 characters allowed in sheet code name.'], |
82
|
|
|
['invalid*code*name', 'Invalid character found in sheet code name'], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $codeName |
88
|
|
|
* @param string $expectMessage |
89
|
|
|
* @dataProvider setCodeNameInvalidProvider |
90
|
|
|
*/ |
91
|
|
|
public function testSetCodeNameInvalid($codeName, $expectMessage) |
92
|
|
|
{ |
93
|
|
|
// First, test setting code name with validation disabled -- should be successful |
94
|
|
|
$worksheet = new Worksheet(); |
95
|
|
|
$worksheet->setCodeName($codeName, false); |
96
|
|
|
|
97
|
|
|
// Next, test again with validation enabled -- this time we should fail |
98
|
|
|
$worksheet = new Worksheet(); |
99
|
|
|
$this->expectException(\Exception::class); |
100
|
|
|
$this->expectExceptionMessage($expectMessage); |
101
|
|
|
$worksheet->setCodeName($codeName); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSetCodeNameDuplicate() |
105
|
|
|
{ |
106
|
|
|
// Create a Spreadsheet with three Worksheets (the first is created automatically) |
107
|
|
|
$spreadsheet = new Spreadsheet(); |
108
|
|
|
$spreadsheet->createSheet(); |
109
|
|
|
$spreadsheet->createSheet(); |
110
|
|
|
|
111
|
|
|
// Set unique code name -- should be massaged to Snake_Case |
112
|
|
|
$sheet = $spreadsheet->getSheet(0); |
113
|
|
|
$sheet->setCodeName('Test Code Name'); |
114
|
|
|
self::assertSame('Test_Code_Name', $sheet->getCodeName()); |
115
|
|
|
|
116
|
|
|
// Set duplicate code name -- should be massaged and have numeric suffix appended |
117
|
|
|
$sheet = $spreadsheet->getSheet(1); |
118
|
|
|
$sheet->setCodeName('Test Code Name'); |
119
|
|
|
self::assertSame('Test_Code_Name_1', $sheet->getCodeName()); |
120
|
|
|
|
121
|
|
|
// Set duplicate code name with validation disabled -- should be unchanged, and unmassaged |
122
|
|
|
$sheet = $spreadsheet->getSheet(2); |
123
|
|
|
$sheet->setCodeName('Test Code Name', false); |
124
|
|
|
self::assertSame('Test Code Name', $sheet->getCodeName()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testFreezePaneSelectedCell() |
128
|
|
|
{ |
129
|
|
|
$worksheet = new Worksheet(); |
130
|
|
|
$worksheet->freezePane('B2'); |
131
|
|
|
self::assertSame('B2', $worksheet->getTopLeftCell()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function extractSheetTitleProvider() |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
['B2', '', '', 'B2'], |
138
|
|
|
['testTitle!B2', 'testTitle', 'B2', 'B2'], |
139
|
|
|
['test!Title!B2', 'test!Title', 'B2', 'B2'], |
140
|
|
|
['test Title!B2', 'test Title', 'B2', 'B2'], |
141
|
|
|
['test!Title!B2', 'test!Title', 'B2', 'B2'], |
142
|
|
|
["'testSheet 1'!A3", "'testSheet 1'", 'A3', 'A3'], |
143
|
|
|
["'testSheet1'!A2", "'testSheet1'", 'A2', 'A2'], |
144
|
|
|
["'testSheet 2'!A1", "'testSheet 2'", 'A1', 'A1'], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $range |
150
|
|
|
* @param string $expectTitle |
151
|
|
|
* @param string $expectCell |
152
|
|
|
* @param string $expectCell2 |
153
|
|
|
* @dataProvider extractSheetTitleProvider |
154
|
|
|
*/ |
155
|
|
|
public function testExtractSheetTitle($range, $expectTitle, $expectCell, $expectCell2) |
156
|
|
|
{ |
157
|
|
|
// only cell reference |
158
|
|
|
self::assertSame($expectCell, Worksheet::extractSheetTitle($range)); |
159
|
|
|
// with title in array |
160
|
|
|
$arRange = Worksheet::extractSheetTitle($range, true); |
161
|
|
|
self::assertSame($expectTitle, $arRange[0]); |
162
|
|
|
self::assertSame($expectCell2, $arRange[1]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Fix https://github.com/PHPOffice/PhpSpreadsheet/issues/868 when cells are not removed correctly |
167
|
|
|
* on row deletion. |
168
|
|
|
*/ |
169
|
|
|
public function testRemoveCellsCorrectlyWhenRemovingRow() |
170
|
|
|
{ |
171
|
|
|
$workbook = new Spreadsheet(); |
172
|
|
|
$worksheet = $workbook->getActiveSheet(); |
173
|
|
|
$worksheet->getCell('A2')->setValue('A2'); |
174
|
|
|
$worksheet->getCell('C1')->setValue('C1'); |
175
|
|
|
$worksheet->removeRow(1); |
176
|
|
|
$this->assertEquals( |
177
|
|
|
'A2', |
178
|
|
|
$worksheet->getCell('A1')->getValue() |
179
|
|
|
); |
180
|
|
|
$this->assertNull( |
181
|
|
|
$worksheet->getCell('C1')->getValue() |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function removeColumnProvider(): array |
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
|
|
'Remove first column' => [ |
189
|
|
|
[ |
190
|
|
|
['A1', 'B1', 'C1'], |
191
|
|
|
['A2', 'B2', 'C2'], |
192
|
|
|
], |
193
|
|
|
'A', |
194
|
|
|
1, |
195
|
|
|
[ |
196
|
|
|
['B1', 'C1'], |
197
|
|
|
['B2', 'C2'], |
198
|
|
|
], |
199
|
|
|
'B', |
200
|
|
|
], |
201
|
|
|
'Remove middle column' => [ |
202
|
|
|
[ |
203
|
|
|
['A1', 'B1', 'C1'], |
204
|
|
|
['A2', 'B2', 'C2'], |
205
|
|
|
], |
206
|
|
|
'B', |
207
|
|
|
1, |
208
|
|
|
[ |
209
|
|
|
['A1', 'C1'], |
210
|
|
|
['A2', 'C2'], |
211
|
|
|
], |
212
|
|
|
'B', |
213
|
|
|
], |
214
|
|
|
'Remove last column' => [ |
215
|
|
|
[ |
216
|
|
|
['A1', 'B1', 'C1'], |
217
|
|
|
['A2', 'B2', 'C2'], |
218
|
|
|
], |
219
|
|
|
'C', |
220
|
|
|
1, |
221
|
|
|
[ |
222
|
|
|
['A1', 'B1'], |
223
|
|
|
['A2', 'B2'], |
224
|
|
|
], |
225
|
|
|
'B', |
226
|
|
|
], |
227
|
|
|
'Remove a column out of range' => [ |
228
|
|
|
[ |
229
|
|
|
['A1', 'B1', 'C1'], |
230
|
|
|
['A2', 'B2', 'C2'], |
231
|
|
|
], |
232
|
|
|
'D', |
233
|
|
|
1, |
234
|
|
|
[ |
235
|
|
|
['A1', 'B1', 'C1'], |
236
|
|
|
['A2', 'B2', 'C2'], |
237
|
|
|
], |
238
|
|
|
'C', |
239
|
|
|
], |
240
|
|
|
'Remove multiple columns' => [ |
241
|
|
|
[ |
242
|
|
|
['A1', 'B1', 'C1'], |
243
|
|
|
['A2', 'B2', 'C2'], |
244
|
|
|
], |
245
|
|
|
'B', |
246
|
|
|
5, |
247
|
|
|
[ |
248
|
|
|
['A1'], |
249
|
|
|
['A2'], |
250
|
|
|
], |
251
|
|
|
'A', |
252
|
|
|
], |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @dataProvider removeColumnProvider |
258
|
|
|
*/ |
259
|
|
|
public function testRemoveColumn( |
260
|
|
|
array $initialData, |
261
|
|
|
string $columnToBeRemoved, |
262
|
|
|
int $columnsToBeRemoved, |
263
|
|
|
array $expectedData, |
264
|
|
|
string $expectedHighestColumn |
265
|
|
|
) { |
266
|
|
|
$spreadsheet = new Spreadsheet(); |
267
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
268
|
|
|
$worksheet->fromArray($initialData); |
269
|
|
|
|
270
|
|
|
$worksheet->removeColumn($columnToBeRemoved, $columnsToBeRemoved); |
271
|
|
|
|
272
|
|
|
self::assertSame($expectedHighestColumn, $worksheet->getHighestColumn()); |
273
|
|
|
self::assertSame($expectedData, $worksheet->toArray()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function removeRowsProvider() |
277
|
|
|
{ |
278
|
|
|
return [ |
279
|
|
|
'Remove all rows except first one' => [ |
280
|
|
|
[ |
281
|
|
|
['A1', 'B1', 'C1'], |
282
|
|
|
['A2', 'B2', 'C2'], |
283
|
|
|
['A3', 'B3', 'C3'], |
284
|
|
|
['A4', 'B4', 'C4'], |
285
|
|
|
], |
286
|
|
|
2, |
287
|
|
|
3, |
288
|
|
|
[ |
289
|
|
|
['A1', 'B1', 'C1'], |
290
|
|
|
], |
291
|
|
|
1, |
292
|
|
|
], |
293
|
|
|
'Remove all rows except last one' => [ |
294
|
|
|
[ |
295
|
|
|
['A1', 'B1', 'C1'], |
296
|
|
|
['A2', 'B2', 'C2'], |
297
|
|
|
['A3', 'B3', 'C3'], |
298
|
|
|
['A4', 'B4', 'C4'], |
299
|
|
|
], |
300
|
|
|
1, |
301
|
|
|
3, |
302
|
|
|
[ |
303
|
|
|
['A4', 'B4', 'C4'], |
304
|
|
|
], |
305
|
|
|
1, |
306
|
|
|
], |
307
|
|
|
'Remove last row' => [ |
308
|
|
|
[ |
309
|
|
|
['A1', 'B1', 'C1'], |
310
|
|
|
['A2', 'B2', 'C2'], |
311
|
|
|
['A3', 'B3', 'C3'], |
312
|
|
|
['A4', 'B4', 'C4'], |
313
|
|
|
], |
314
|
|
|
4, |
315
|
|
|
1, |
316
|
|
|
[ |
317
|
|
|
['A1', 'B1', 'C1'], |
318
|
|
|
['A2', 'B2', 'C2'], |
319
|
|
|
['A3', 'B3', 'C3'], |
320
|
|
|
], |
321
|
|
|
3, |
322
|
|
|
], |
323
|
|
|
'Remove first row' => [ |
324
|
|
|
[ |
325
|
|
|
['A1', 'B1', 'C1'], |
326
|
|
|
['A2', 'B2', 'C2'], |
327
|
|
|
['A3', 'B3', 'C3'], |
328
|
|
|
['A4', 'B4', 'C4'], |
329
|
|
|
], |
330
|
|
|
1, |
331
|
|
|
1, |
332
|
|
|
[ |
333
|
|
|
['A2', 'B2', 'C2'], |
334
|
|
|
['A3', 'B3', 'C3'], |
335
|
|
|
['A4', 'B4', 'C4'], |
336
|
|
|
], |
337
|
|
|
3, |
338
|
|
|
], |
339
|
|
|
'Remove all rows except first and last' => [ |
340
|
|
|
[ |
341
|
|
|
['A1', 'B1', 'C1'], |
342
|
|
|
['A2', 'B2', 'C2'], |
343
|
|
|
['A3', 'B3', 'C3'], |
344
|
|
|
['A4', 'B4', 'C4'], |
345
|
|
|
], |
346
|
|
|
2, |
347
|
|
|
2, |
348
|
|
|
[ |
349
|
|
|
['A1', 'B1', 'C1'], |
350
|
|
|
['A4', 'B4', 'C4'], |
351
|
|
|
], |
352
|
|
|
2, |
353
|
|
|
], |
354
|
|
|
'Remove non existing rows' => [ |
355
|
|
|
[ |
356
|
|
|
['A1', 'B1', 'C1'], |
357
|
|
|
['A2', 'B2', 'C2'], |
358
|
|
|
['A3', 'B3', 'C3'], |
359
|
|
|
['A4', 'B4', 'C4'], |
360
|
|
|
], |
361
|
|
|
2, |
362
|
|
|
10, |
363
|
|
|
[ |
364
|
|
|
['A1', 'B1', 'C1'], |
365
|
|
|
], |
366
|
|
|
1, |
367
|
|
|
], |
368
|
|
|
'Remove only non existing rows' => [ |
369
|
|
|
[ |
370
|
|
|
['A1', 'B1', 'C1'], |
371
|
|
|
['A2', 'B2', 'C2'], |
372
|
|
|
['A3', 'B3', 'C3'], |
373
|
|
|
['A4', 'B4', 'C4'], |
374
|
|
|
], |
375
|
|
|
5, |
376
|
|
|
10, |
377
|
|
|
[ |
378
|
|
|
['A1', 'B1', 'C1'], |
379
|
|
|
['A2', 'B2', 'C2'], |
380
|
|
|
['A3', 'B3', 'C3'], |
381
|
|
|
['A4', 'B4', 'C4'], |
382
|
|
|
], |
383
|
|
|
4, |
384
|
|
|
], |
385
|
|
|
]; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @dataProvider removeRowsProvider |
390
|
|
|
*/ |
391
|
|
|
public function testRemoveRows( |
392
|
|
|
array $initialData, |
393
|
|
|
int $rowToRemove, |
394
|
|
|
int $rowsQtyToRemove, |
395
|
|
|
array $expectedData, |
396
|
|
|
int $expectedHighestRow |
397
|
|
|
) { |
398
|
|
|
$workbook = new Spreadsheet(); |
399
|
|
|
$worksheet = $workbook->getActiveSheet(); |
400
|
|
|
$worksheet->fromArray($initialData); |
401
|
|
|
|
402
|
|
|
$worksheet->removeRow($rowToRemove, $rowsQtyToRemove); |
403
|
|
|
|
404
|
|
|
self::assertSame($expectedData, $worksheet->toArray()); |
405
|
|
|
self::assertSame($expectedHighestRow, $worksheet->getHighestRow()); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|