-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionSpoutTest.php
More file actions
255 lines (227 loc) · 7.4 KB
/
ExtensionSpoutTest.php
File metadata and controls
255 lines (227 loc) · 7.4 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
use Kriss\DataExporter\DataExporter;
use Kriss\DataExporter\Writer\Extension\NullSpoutExtend;
use OpenSpout\Common\Entity\Cell;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\WriterInterface;
/**
* 配置 csv
*/
class ExtensionSpoutConfigCsvExtend extends NullSpoutExtend
{
/**
* @inheritDoc
* @link https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#configuration-for-csv
*/
public function beforeOpen(WriterInterface $writer): void
{
if ($writer instanceof \OpenSpout\Writer\CSV\Writer) {
$options = $writer->getOptions();
$options->FIELD_DELIMITER = '|';
}
}
}
/**
* 默认样式
*/
class ExtensionSpoutDefaultStyleExtend extends NullSpoutExtend
{
/**
* @inheritDoc
*/
public function beforeOpen(WriterInterface $writer): void
{
if ($writer instanceof OpenSpout\Writer\XLSX\Writer || $writer instanceof OpenSpout\Writer\ODS\Writer) {
$style = (new Style())
->setFontName('Arial')
->setFontSize(11)
->setFontColor(Color::GREEN);
$options = $writer->getOptions();
$options->DEFAULT_ROW_STYLE = $style;
}
}
}
/**
* 行和列的样式
*/
class ExtensionSpoutRowCellStyleExtend extends NullSpoutExtend
{
/**
* @inheritDoc
*/
public function buildCellStyle(int|string $colIndex, int $rowIndex, array $context = []): ?Style
{
if ($colIndex === 0 && $rowIndex === 2) {
// 第一列,第二行(A2)
return (new Style())
->setFontColor(Color::RED)
->setFontBold();
}
if ($colIndex === 1 && $rowIndex === 2) {
// 第二列,第二行(B2)
return (new Style())
->setFontColor(Color::WHITE)
->setBackgroundColor(Color::BLACK);
}
if ($colIndex === 0 && $rowIndex === 2) {
// 第一列,第五行(A5)
return (new Style())
->setShouldWrapText();
}
return null;
}
/**
* @inheritDoc
*/
public function buildRowStyle(int $rowIndex, array $context = []): ?Style
{
if ($rowIndex === 3) {
// 第三行
return (new Style())
->setBackgroundColor(Color::BLUE);
}
if ($rowIndex === 1) {
// 第一行
return (new Style())
->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::CENTER);
}
if ($rowIndex === 6) {
// 第五行
return (new Style())
->setShouldShrinkToFit();
}
return null;
}
/**
* @inheritDoc
*/
public function beforeClose(WriterInterface $writer): void
{
if ($writer instanceof \OpenSpout\Writer\XLSX\Writer) {
$options = $writer->getOptions();
// 合并单元格
// https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#cell-merging
$options->mergeCells(0, 5, 2, 5);
// 设置列宽
// https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#column-widths
$options->setColumnWidth(60, 1);
}
}
}
beforeEach(function () {
$this->source = [
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['This is long For fonts and alignments, OpenSpout does not support all the possible formatting options yet. But you can find the most important ones', '', ''],
['This is long For fonts and alignments, OpenSpout does not support all the possible formatting options yet. But you can find the most important ones', '', ''],
];
$this->filename = __DIR__ . '/../tmp/test';
});
it("Extension Spout: config csv", function () {
$filename = DataExporter::csvSpout($this->source, [
'showHeaders' => false,
'extend' => new ExtensionSpoutConfigCsvExtend(),
])->saveAs($this->filename);
$options = new \OpenSpout\Reader\CSV\Options();
$options->FIELD_DELIMITER = '|';
$reader = new \OpenSpout\Reader\CSV\Reader($options);
$reader->open($filename);
$firstRow = [];
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
/** @var Row $row */
$firstRow = $row->toArray();
break;
}
}
expect($this->source[0])->toEqual($firstRow);
});
it("Extension Spout: set default style", function () {
DataExporter::xlsxSpout($this->source, [
'extend' => new ExtensionSpoutDefaultStyleExtend(),
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});
it("Extension Spout: set cell or row style", function () {
DataExporter::xlsxSpout($this->source, [
'showHeaders' => false,
'extend' => new ExtensionSpoutRowCellStyleExtend(),
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});
it("Extension Spout: col index", function () {
DataExporter::xlsxSpout([
['aa', 'bb', 'cc'],
], [
'showHeaders' => false,
'extend' => new class () extends NullSpoutExtend {
public function buildCellStyle($colIndex, $rowIndex): ?Style
{
expect($colIndex)->toBeInt()
->and($rowIndex)->toBeInt();
return null;
}
},
])->saveAs($this->filename);
DataExporter::xlsxSpout([
['key1' => 'aa', 'key2' => 'bb'],
], [
'showHeaders' => false,
'extend' => new class () extends NullSpoutExtend {
public function buildCellStyle($colIndex, $rowIndex): ?Style
{
expect($colIndex)->toBeString()
->and($rowIndex)->toBeInt();
return null;
}
},
])->saveAs($this->filename);
});
it("Extension Spout: change style use context", function () {
DataExporter::xlsxSpout([
[10, 2, 20, 4],
[20, 6, 10, 8],
], [
'showHeaders' => false,
'extend' => new class () extends NullSpoutExtend {
public function buildCellStyleWithContext($colIndex, int $rowIndex, array $context = []): ?Style
{
if ($colIndex === 0) {
$rowData = $context['row_data'] ?? [];
if ($rowData[2] > $rowData[0]) {
return (new Style())
->setFontColor(Color::GREEN);
}
}
return null;
}
},
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});
it("Extension Spout: change cell", function () {
DataExporter::xlsxSpout([
[10, 2, 20, 4],
[20, 6, 10, 8],
], [
'showHeaders' => false,
'extend' => new class () extends NullSpoutExtend {
public function buildCell($colIndex, int $rowIndex, Cell $cell, array $context = []): Cell
{
if ($colIndex == 0 && $rowIndex == 1) {
return Cell::fromValue('new value');
}
return $cell;
}
},
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});