1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
8
|
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
9
|
|
|
|
10
|
|
|
class TableTest extends AbstractFunctional |
11
|
|
|
{ |
12
|
|
|
protected function populateData(Spreadsheet $spreadsheet): Table |
13
|
|
|
{ |
14
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
15
|
|
|
|
16
|
|
|
$tableName = 'SalesData'; |
17
|
|
|
$columnFormula = "=SUM({$tableName}[[#This Row],[Q1]:[Q4]])"; |
18
|
|
|
|
19
|
|
|
$dataArray = [ |
20
|
|
|
['Year', 'Country', 'Q1', 'Q2', 'Q3', 'Q4', 'Sales'], |
21
|
|
|
[2010, 'Belgium', 380, 390, 420, 460, $columnFormula], |
22
|
|
|
[2010, 'France', 510, 490, 460, 590, $columnFormula], |
23
|
|
|
[2010, 'Germany', 720, 680, 640, 660, $columnFormula], |
24
|
|
|
[2010, 'Italy', 440, 410, 420, 450, $columnFormula], |
25
|
|
|
[2010, 'Spain', 510, 490, 470, 420, $columnFormula], |
26
|
|
|
[2010, 'UK', 690, 610, 620, 600, $columnFormula], |
27
|
|
|
[2010, 'United States', 790, 730, 860, 850, $columnFormula], |
28
|
|
|
[2011, 'Belgium', 400, 350, 450, 500, $columnFormula], |
29
|
|
|
[2011, 'France', 620, 650, 415, 570, $columnFormula], |
30
|
|
|
[2011, 'Germany', 680, 620, 710, 690, $columnFormula], |
31
|
|
|
[2011, 'Italy', 430, 370, 350, 335, $columnFormula], |
32
|
|
|
[2011, 'Spain', 460, 390, 430, 415, $columnFormula], |
33
|
|
|
[2011, 'UK', 720, 650, 580, 510, $columnFormula], |
34
|
|
|
[2011, 'United States', 800, 700, 900, 950, $columnFormula], |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$worksheet->fromArray($dataArray, null, 'A1'); |
38
|
|
|
|
39
|
|
|
$rowColumnRange = "{$worksheet->getHighestDataColumn()}{$worksheet->getHighestDataRow()}"; |
40
|
|
|
|
41
|
|
|
$table = new Table("A1:{$rowColumnRange}", $tableName); |
42
|
|
|
$table->setRange("A1:{$rowColumnRange}"); |
43
|
|
|
|
44
|
|
|
$table->getColumn('G') |
45
|
|
|
->setTotalsRowLabel('Total') |
46
|
|
|
->setColumnFormula($columnFormula); |
47
|
|
|
$worksheet->getCell('A16')->setValue('Total'); |
48
|
|
|
$worksheet->getCell('G16')->setValue("=SUBTOTAL(109,{$tableName}[Sales])"); |
49
|
|
|
|
50
|
|
|
$spreadsheet->getActiveSheet()->addTable($table); |
51
|
|
|
|
52
|
|
|
return $table; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testTableCreation(): void |
56
|
|
|
{ |
57
|
|
|
$spreadsheet = new Spreadsheet(); |
58
|
|
|
|
59
|
|
|
$this->populateData($spreadsheet); |
60
|
|
|
|
61
|
|
|
// TODO: We don't yet support Structured References in formulae, so we need to disable precalculation |
62
|
|
|
// when writing. |
63
|
|
|
$disablePrecalculation = function (Xlsx $writer): void { |
64
|
|
|
$writer->setPreCalculateFormulas(false); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', null, $disablePrecalculation); |
68
|
|
|
$reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet(); |
69
|
|
|
|
70
|
|
|
$reloadedTable = $reloadedWorksheet->getTableCollection()[0]; |
71
|
|
|
self::assertNotNull($reloadedTable); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testTableWithoutFilter(): void |
75
|
|
|
{ |
76
|
|
|
$spreadsheet = new Spreadsheet(); |
77
|
|
|
|
78
|
|
|
$table = $this->populateData($spreadsheet); |
79
|
|
|
$table->setAllowFilter(false); |
80
|
|
|
|
81
|
|
|
// TODO: We don't yet support Structured References in formulae, so we need to disable precalc when writing |
82
|
|
|
$disablePrecalculation = function (Xlsx $writer): void { |
83
|
|
|
$writer->setPreCalculateFormulas(false); |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', null, $disablePrecalculation); |
87
|
|
|
$reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet(); |
88
|
|
|
|
89
|
|
|
$reloadedTable = $reloadedWorksheet->getTableCollection()[0]; |
90
|
|
|
/** @var Table $reloadedTable */ |
91
|
|
|
self::assertNotNull($reloadedTable); |
92
|
|
|
self::assertFalse($reloadedTable->getAllowFilter()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|