Skip to content

Commit a72f3b8

Browse files
authored
Merge pull request #3 from Ang3/issue_2
[Fix] Multiple sheets encoding
2 parents b33dc41 + a181dcc commit a72f3b8

File tree

4 files changed

+70
-37
lines changed

4 files changed

+70
-37
lines changed

src/ExcelEncoder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public function encode($data, $format, array $context = []): string
119119
throw new NotEncodableValueException(sprintf('Expected data of sheet #%d of type "iterable", "%s" given', $sheetName, \gettype($sheetData)));
120120
}
121121

122-
if ($sheetIndex === $sheetName) {
123-
$sheetName = sprintf('Sheet_%d', $sheetIndex);
122+
if ($sheetIndex > 0) {
123+
$spreadsheet->createSheet($sheetIndex);
124124
}
125125

126126
$spreadsheet->setActiveSheetIndex($sheetIndex);
@@ -195,6 +195,8 @@ public function encode($data, $format, array $context = []): string
195195
$columnDimension->setWidth($context[self::COLUMNS_MAXSIZE_KEY]);
196196
}
197197
}
198+
199+
++$sheetIndex;
198200
}
199201

200202
try {

tests/ExcelEncoderTest.php

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ public static function dataDecodingProvider(): array
7373
'array.array.0' => 'again',
7474
],
7575
],
76+
'Feuil1' => [
77+
[
78+
'bool' => 0.0,
79+
'int' => 1,
80+
'float' => 1.618,
81+
'string' => 'Hello',
82+
'object.date' => '2000-01-01 13:37:00.000000',
83+
'object.timezone_type' => 3,
84+
'object.timezone' => 'Europe/Berlin',
85+
'array.bool' => 1,
86+
'array.int' => 3,
87+
'array.float' => 3.14,
88+
'array.string' => 'World',
89+
'array.object.date' => '2000-01-01 13:37:00.000000',
90+
'array.object.timezone_type' => 3,
91+
'array.object.timezone' => 'Europe/Berlin',
92+
'array.array.0' => 'again',
93+
],
94+
],
7695
]],
7796
[__DIR__.'/Resources/encoded.xlsx', ExcelEncoder::XLSX, [
7897
'Sheet_0' => [
@@ -94,6 +113,25 @@ public static function dataDecodingProvider(): array
94113
'array.array.0' => 'again',
95114
],
96115
],
116+
'Feuil1' => [
117+
[
118+
'bool' => 0,
119+
'int' => 1,
120+
'float' => 1.618,
121+
'string' => 'Hello',
122+
'object.date' => '2000-01-01 13:37:00.000000',
123+
'object.timezone_type' => 3,
124+
'object.timezone' => 'Europe/Berlin',
125+
'array.bool' => 1,
126+
'array.int' => 3,
127+
'array.float' => 3.14,
128+
'array.string' => 'World',
129+
'array.object.date' => '2000-01-01 13:37:00.000000',
130+
'array.object.timezone_type' => 3,
131+
'array.object.timezone' => 'Europe/Berlin',
132+
'array.array.0' => 'again',
133+
],
134+
],
97135
]],
98136
];
99137
}
@@ -111,50 +149,43 @@ public function testDecode(string $file, string $format, array $result): void
111149
*/
112150
private function getInitialData(): array
113151
{
114-
return [[
115-
[
116-
'bool' => false,
117-
'int' => 1,
118-
'float' => 1.618,
119-
'string' => 'Hello',
120-
'object' => new \DateTime('2000-01-01 13:37:00'),
121-
'array' => [
122-
'bool' => true,
123-
'int' => 3,
124-
'float' => 3.14,
125-
'string' => 'World',
152+
return [
153+
'Sheet_0' => [
154+
[
155+
'bool' => false,
156+
'int' => 1,
157+
'float' => 1.618,
158+
'string' => 'Hello',
126159
'object' => new \DateTime('2000-01-01 13:37:00'),
127160
'array' => [
128-
'again',
161+
'bool' => true,
162+
'int' => 3,
163+
'float' => 3.14,
164+
'string' => 'World',
165+
'object' => new \DateTime('2000-01-01 13:37:00'),
166+
'array' => [
167+
'again',
168+
],
129169
],
130170
],
131171
],
132-
]];
133-
}
134-
135-
/**
136-
* @internal
137-
*/
138-
private function getDecodedData(string $sheetName): array
139-
{
140-
return [
141-
$sheetName => [
172+
'Feuil1' => [
142173
[
143-
'bool' => 0.0,
174+
'bool' => false,
144175
'int' => 1,
145176
'float' => 1.618,
146177
'string' => 'Hello',
147-
'object.date' => '2000-01-01 13:37:00.000000',
148-
'object.timezone_type' => 3,
149-
'object.timezone' => 'Europe/Berlin',
150-
'array.bool' => 1,
151-
'array.int' => 3,
152-
'array.float' => 3.14,
153-
'array.string' => 'World',
154-
'array.object.date' => '2000-01-01 13:37:00.000000',
155-
'array.object.timezone_type' => 3,
156-
'array.object.timezone' => 'Europe/Berlin',
157-
'array.array.0' => 'again',
178+
'object' => new \DateTime('2000-01-01 13:37:00'),
179+
'array' => [
180+
'bool' => true,
181+
'int' => 3,
182+
'float' => 3.14,
183+
'string' => 'World',
184+
'object' => new \DateTime('2000-01-01 13:37:00'),
185+
'array' => [
186+
'again',
187+
],
188+
],
158189
],
159190
],
160191
];

tests/Resources/encoded.xls

1.5 KB
Binary file not shown.

tests/Resources/encoded.xlsx

915 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)