1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods; |
6
|
|
|
|
7
|
|
|
use DOMDocument; |
8
|
|
|
use DOMXPath; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Ods; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
class ContentTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $samplesPath = 'tests/data/Writer/Ods'; |
29
|
|
|
|
30
|
|
|
private string $compatibilityMode; |
31
|
|
|
|
32
|
|
|
protected function setUp(): void |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
|
36
|
|
|
$this->compatibilityMode = Functions::getCompatibilityMode(); |
37
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function tearDown(): void |
41
|
|
|
{ |
42
|
|
|
parent::tearDown(); |
43
|
|
|
Functions::setCompatibilityMode($this->compatibilityMode); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testWriteEmptySpreadsheet(): void |
47
|
|
|
{ |
48
|
|
|
$content = new Content(new Ods(new Spreadsheet())); |
49
|
|
|
$xml = $content->write(); |
50
|
|
|
|
51
|
|
|
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-empty.xml', $xml); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testWriteSpreadsheet(): void |
55
|
|
|
{ |
56
|
|
|
$workbook = new Spreadsheet(); |
57
|
|
|
|
58
|
|
|
// Worksheet 1 |
59
|
|
|
$worksheet1 = $workbook->getActiveSheet(); |
60
|
|
|
$worksheet1->setCellValue('A1', 1); // Number |
61
|
|
|
$worksheet1->setCellValue('B1', 12345.6789); // Number |
62
|
|
|
$worksheet1->setCellValue('C1', '1'); // Number without cast |
63
|
|
|
$worksheet1->setCellValueExplicit('D1', '01234', DataType::TYPE_STRING); // Number casted to string |
64
|
|
|
$worksheet1->setCellValue('E1', 'Lorem ipsum'); // String |
65
|
|
|
|
66
|
|
|
$worksheet1->setCellValue('A2', true); // Boolean |
67
|
|
|
$worksheet1->setCellValue('B2', false); // Boolean |
68
|
|
|
|
69
|
|
|
$worksheet1->setCellValueExplicit( |
70
|
|
|
'C2', |
71
|
|
|
'=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))', |
72
|
|
|
DataType::TYPE_FORMULA |
73
|
|
|
); // Formula |
74
|
|
|
|
75
|
|
|
$worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date |
76
|
|
|
$worksheet1->getStyle('D2') |
77
|
|
|
->getNumberFormat() |
78
|
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME); |
79
|
|
|
|
80
|
|
|
$worksheet1->setCellValueExplicit('F1', null, DataType::TYPE_ERROR); |
81
|
|
|
$worksheet1->setCellValueExplicit('G1', 'Lorem ipsum', DataType::TYPE_INLINE); |
82
|
|
|
|
83
|
|
|
// Styles |
84
|
|
|
$worksheet1->getStyle('A1')->getFont()->setBold(true); |
85
|
|
|
$worksheet1->getStyle('B1')->getFont()->setItalic(true); |
86
|
|
|
$worksheet1->getStyle('C1')->getFont()->setName('Courier'); |
87
|
|
|
$worksheet1->getStyle('C1')->getFont()->setSize(14); |
88
|
|
|
$worksheet1->getStyle('C1')->getFont()->setColor(new Color(Color::COLOR_BLUE)); |
89
|
|
|
|
90
|
|
|
$worksheet1->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID); |
91
|
|
|
$worksheet1->getStyle('C1')->getFill()->setStartColor(new Color(Color::COLOR_RED)); |
92
|
|
|
|
93
|
|
|
$worksheet1->getStyle('C1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE); |
94
|
|
|
$worksheet1->getStyle('C2')->getFont()->setUnderline(Font::UNDERLINE_DOUBLE); |
95
|
|
|
$worksheet1->getStyle('D2')->getFont()->setUnderline(Font::UNDERLINE_NONE); |
96
|
|
|
|
97
|
|
|
// Worksheet 2 |
98
|
|
|
$worksheet2 = $workbook->createSheet(); |
99
|
|
|
$worksheet2->setTitle('New Worksheet'); |
100
|
|
|
$worksheet2->setCellValue('A1', 2); |
101
|
|
|
|
102
|
|
|
// Write |
103
|
|
|
$content = new Content(new Ods($workbook)); |
104
|
|
|
$xml = $content->write(); |
105
|
|
|
|
106
|
|
|
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-with-data.xml', $xml); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testWriteWithHiddenWorksheet(): void |
110
|
|
|
{ |
111
|
|
|
$workbook = new Spreadsheet(); |
112
|
|
|
|
113
|
|
|
// Worksheet 1 |
114
|
|
|
$worksheet1 = $workbook->getActiveSheet(); |
115
|
|
|
$worksheet1->setCellValue('A1', 1); |
116
|
|
|
|
117
|
|
|
// Worksheet 2 |
118
|
|
|
$worksheet2 = $workbook->createSheet(); |
119
|
|
|
$worksheet2->setTitle('New Worksheet'); |
120
|
|
|
$worksheet2->setCellValue('A1', 2); |
121
|
|
|
|
122
|
|
|
$worksheet2->setSheetState(Worksheet::SHEETSTATE_HIDDEN); |
123
|
|
|
|
124
|
|
|
// Write |
125
|
|
|
$content = new Content(new Ods($workbook)); |
126
|
|
|
$xml = $content->write(); |
127
|
|
|
|
128
|
|
|
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-hidden-worksheet.xml', $xml); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testWriteBorderStyle(): void |
132
|
|
|
{ |
133
|
|
|
$spreadsheet = new Spreadsheet(); |
134
|
|
|
$spreadsheet->getActiveSheet()->getStyle('A1:B2')->applyFromArray([ |
135
|
|
|
'borders' => [ |
136
|
|
|
'outline' => [ |
137
|
|
|
'borderStyle' => Border::BORDER_THICK, |
138
|
|
|
'color' => ['argb' => 'AA22DD00'], |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$content = new Content(new Ods($spreadsheet)); |
144
|
|
|
$xml = $content->write(); |
145
|
|
|
|
146
|
|
|
$xmlDoc = new DOMDocument(); |
147
|
|
|
$xmlDoc->loadXML($xml); |
148
|
|
|
$xmlPath = new DOMXPath($xmlDoc); |
149
|
|
|
|
150
|
|
|
foreach (['top', 'bottom'] as $keyRow => $row) { |
151
|
|
|
foreach (['left', 'right'] as $keyCell => $cell) { |
152
|
|
|
$styles = ['top' => '', 'bottom' => '', 'left' => '', 'right' => '']; |
153
|
|
|
$styles[$row] = '2.5pt solid #22DD00'; |
154
|
|
|
$styles[$cell] = '2.5pt solid #22DD00'; |
155
|
|
|
|
156
|
|
|
$query = 'string(//office:document-content/office:body/office:spreadsheet/table:table/table:table-row[position()=' . ($keyRow + 1) . ']/table:table-cell[position()=' . ($keyCell + 1) . ']/@table:style-name)'; |
157
|
|
|
$idStyle = $xmlPath->evaluate($query); |
158
|
|
|
|
159
|
|
|
foreach ($styles as $direction => $value) { |
160
|
|
|
$query = 'string(//office:document-content/office:automatic-styles/style:style[@style:name="' . $idStyle . '"]/style:table-cell-properties/@fo:border-' . $direction . ')'; |
161
|
|
|
$style = $xmlPath->evaluate($query); |
162
|
|
|
self::assertEquals($style, $value); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|