Skip to content

Commit cc5c020

Browse files
authored
Fix for Issue 2029 (Invalid Cell Coordinate A-1) (#2032)
* Fix for Issue 2029 (Invalid Cell Coordinate A-1) Fix for #2021. When Html Reader encounters an embedded table, it tries to shift it up a row. It obviously should not attempt to shift it above row 1. @danmodini reported the problem, and suggests the correct solution. This PR implements that and adds a test case. Performing some additional testing, I found that Html Reader cannot handle inline column width or row height set in points rather than pixels (and HTML writer with useInlineCss generates these values in points). It also doesn't handle border style when the border width (which it ignores) is omitted. Fixed and added tests.
1 parent e4973fa commit cc5c020

File tree

2 files changed

+127
-4
lines changed

2 files changed

+127
-4
lines changed

src/PhpSpreadsheet/Reader/Html.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ private function processDomElementTable(Worksheet $sheet, int &$row, string &$co
469469
if ($child->nodeName === 'table') {
470470
$this->flushCell($sheet, $column, $row, $cellContent);
471471
$column = $this->setTableStartColumn($column);
472-
if ($this->tableLevel > 1) {
472+
if ($this->tableLevel > 1 && $row > 1) {
473473
--$row;
474474
}
475475
$this->processDomElement($child, $sheet, $row, $column, $cellContent);
@@ -878,14 +878,14 @@ private function applyInlineStyle(&$sheet, $row, $column, $attributeArray): void
878878

879879
case 'width':
880880
$sheet->getColumnDimension($column)->setWidth(
881-
(float) str_replace('px', '', $styleValue)
881+
(float) str_replace(['px', 'pt'], '', $styleValue)
882882
);
883883

884884
break;
885885

886886
case 'height':
887887
$sheet->getRowDimension($row)->setRowHeight(
888-
(float) str_replace('px', '', $styleValue)
888+
(float) str_replace(['px', 'pt'], '', $styleValue)
889889
);
890890

891891
break;
@@ -1009,7 +1009,15 @@ private function setBorderStyle(Style $cellStyle, $styleValue, $type): void
10091009
$borderStyle = Border::BORDER_NONE;
10101010
$color = null;
10111011
} else {
1012-
[, $borderStyle, $color] = explode(' ', $styleValue);
1012+
$borderArray = explode(' ', $styleValue);
1013+
$borderCount = count($borderArray);
1014+
if ($borderCount >= 3) {
1015+
$borderStyle = $borderArray[1];
1016+
$color = $borderArray[2];
1017+
} else {
1018+
$borderStyle = $borderArray[0];
1019+
$color = $borderArray[1] ?? null;
1020+
}
10131021
}
10141022

10151023
$cellStyle->applyFromArray([
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Html;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class Issue2029Test extends TestCase
9+
{
10+
public function testIssue2029(): void
11+
{
12+
$content = <<<'EOF'
13+
<!DOCTYPE html>
14+
<html>
15+
<head>
16+
<meta charset='utf-8'>
17+
<title>Declaracion en Linea</title>
18+
</head>
19+
<body>
20+
<table>
21+
<tr>
22+
<td>
23+
<table>
24+
<tr>
25+
<td>
26+
<table>
27+
<tbody>
28+
<tr>
29+
<td>CUIT:</td>
30+
<td><label id="lblCUIT" class="text-left">30-53914190-9</label></td>
31+
</tr>
32+
<tr>
33+
<td>Per&iacute;odo</td>
34+
<td><label id="lblPeriodo" class="text-left">02 2021</label></td>
35+
</tr>
36+
<tr>
37+
<td>Secuencia:</td>
38+
<td><label id="lblSecuencia" class="text-left">0 - Original</label></td>
39+
</tr>
40+
<tr>
41+
<td>Contribuyente:</td>
42+
<td><label id="lblContribuyente">SIND DE TRABAJADORES DE IND DE LA ALIMENTACION</label></td>
43+
<td><label id="lblFechaHoy"></label></td>
44+
</tr>
45+
</tbody>
46+
</table>
47+
</td>
48+
</tr>
49+
</table>
50+
</td>
51+
</tr>
52+
</table>
53+
<table border="1px">
54+
<tr>
55+
<th class="text-center">
56+
CUIL
57+
</th>
58+
<th class="text-center">
59+
Apellido y Nombre
60+
</th>
61+
<th class="text-center">
62+
Obra Social
63+
</th>
64+
<th class="text-center">
65+
Corresponde Reducci&oacute;n?
66+
</th>
67+
</tr>
68+
69+
<tr>
70+
<td class="text-center">
71+
12345678901
72+
</td>
73+
<td class="text-center">
74+
EMILIANO ZAPATA SALAZAR
75+
</td>
76+
<td class="text-center">
77+
101208
78+
</td>
79+
<td class="text-center">
80+
Yes
81+
</td>
82+
</tr>
83+
84+
<tr>
85+
<td class="text-center">
86+
23456789012
87+
</td>
88+
<td class="text-center">
89+
FRANCISCO PANCHO VILLA
90+
</td>
91+
<td class="text-center">
92+
101208
93+
</td>
94+
<td class="text-center">
95+
No
96+
</td>
97+
</tr>
98+
</table>
99+
</body>
100+
</html>
101+
102+
EOF;
103+
$reader = new Html();
104+
$spreadsheet = $reader->loadFromString($content);
105+
$sheet = $spreadsheet->getActiveSheet();
106+
self::assertSame('CUIT:', $sheet->getCell('A1')->getValue());
107+
self::assertSame('30-53914190-9', $sheet->getCell('B1')->getValue());
108+
self::assertSame('Contribuyente:', $sheet->getCell('A4')->getValue());
109+
self::assertSame('Apellido y Nombre', $sheet->getCell('B9')->getValue());
110+
self::assertEquals('101208', $sheet->getCell('C10')->getValue());
111+
self::assertEquals('Yes', $sheet->getCell('D10')->getValue());
112+
self::assertEquals('23456789012', $sheet->getCell('A11')->getValue());
113+
self::assertEquals('No', $sheet->getCell('D11')->getValue());
114+
}
115+
}

0 commit comments

Comments
 (0)