1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Ods; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class ContentTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
private $samplesPath = __DIR__ . '/../../../data/Writer/Ods'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $compatibilityMode; |
25
|
|
|
|
26
|
|
|
protected function setUp(): void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
$this->compatibilityMode = Functions::getCompatibilityMode(); |
31
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function tearDown(): void |
35
|
|
|
{ |
36
|
|
|
parent::tearDown(); |
37
|
|
|
Functions::setCompatibilityMode($this->compatibilityMode); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testWriteEmptySpreadsheet() |
41
|
|
|
{ |
42
|
|
|
$content = new Content(new Ods(new Spreadsheet())); |
43
|
|
|
$xml = $content->write(); |
44
|
|
|
|
45
|
|
|
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-empty.xml', $xml); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testWriteSpreadsheet() |
49
|
|
|
{ |
50
|
|
|
$workbook = new Spreadsheet(); |
51
|
|
|
|
52
|
|
|
// Worksheet 1 |
53
|
|
|
$worksheet1 = $workbook->getActiveSheet(); |
54
|
|
|
$worksheet1->setCellValue('A1', 1); // Number |
55
|
|
|
$worksheet1->setCellValue('B1', 12345.6789); // Number |
56
|
|
|
$worksheet1->setCellValue('C1', '1'); // Number without cast |
57
|
|
|
$worksheet1->setCellValueExplicit('D1', '01234', DataType::TYPE_STRING); // Number casted to string |
58
|
|
|
$worksheet1->setCellValue('E1', 'Lorem ipsum'); // String |
59
|
|
|
|
60
|
|
|
$worksheet1->setCellValue('A2', true); // Boolean |
61
|
|
|
$worksheet1->setCellValue('B2', false); // Boolean |
62
|
|
|
$worksheet1->setCellValueExplicit( |
63
|
|
|
'C2', |
64
|
|
|
'=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))', |
65
|
|
|
DataType::TYPE_FORMULA |
66
|
|
|
); // Formula |
67
|
|
|
|
68
|
|
|
$worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date |
69
|
|
|
$worksheet1->getStyle('D2') |
70
|
|
|
->getNumberFormat() |
71
|
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME); |
72
|
|
|
|
73
|
|
|
// Styles |
74
|
|
|
$worksheet1->getStyle('A1')->getFont()->setBold(true); |
75
|
|
|
$worksheet1->getStyle('B1')->getFont()->setItalic(true); |
76
|
|
|
$worksheet1->getStyle('C1')->getFont()->setName('Courier'); |
77
|
|
|
$worksheet1->getStyle('C1')->getFont()->setSize(14); |
78
|
|
|
$worksheet1->getStyle('C1')->getFont()->setColor(new Color(Color::COLOR_BLUE)); |
79
|
|
|
|
80
|
|
|
$worksheet1->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID); |
81
|
|
|
$worksheet1->getStyle('C1')->getFill()->setStartColor(new Color(Color::COLOR_RED)); |
82
|
|
|
|
83
|
|
|
$worksheet1->getStyle('C1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE); |
84
|
|
|
$worksheet1->getStyle('C2')->getFont()->setUnderline(Font::UNDERLINE_DOUBLE); |
85
|
|
|
$worksheet1->getStyle('D2')->getFont()->setUnderline(Font::UNDERLINE_NONE); |
86
|
|
|
|
87
|
|
|
// Worksheet 2 |
88
|
|
|
$worksheet2 = $workbook->createSheet(); |
89
|
|
|
$worksheet2->setTitle('New Worksheet'); |
90
|
|
|
$worksheet2->setCellValue('A1', 2); |
91
|
|
|
|
92
|
|
|
// Write |
93
|
|
|
$content = new Content(new Ods($workbook)); |
94
|
|
|
$xml = $content->write(); |
95
|
|
|
|
96
|
|
|
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-with-data.xml', $xml); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|