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.
This commit is contained in:
oleibman 2021-04-29 13:59:01 -07:00 committed by GitHub
parent e4973fa041
commit cc5c0205d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 4 deletions

View File

@ -469,7 +469,7 @@ class Html extends BaseReader
if ($child->nodeName === 'table') {
$this->flushCell($sheet, $column, $row, $cellContent);
$column = $this->setTableStartColumn($column);
if ($this->tableLevel > 1) {
if ($this->tableLevel > 1 && $row > 1) {
--$row;
}
$this->processDomElement($child, $sheet, $row, $column, $cellContent);
@ -878,14 +878,14 @@ class Html extends BaseReader
case 'width':
$sheet->getColumnDimension($column)->setWidth(
(float) str_replace('px', '', $styleValue)
(float) str_replace(['px', 'pt'], '', $styleValue)
);
break;
case 'height':
$sheet->getRowDimension($row)->setRowHeight(
(float) str_replace('px', '', $styleValue)
(float) str_replace(['px', 'pt'], '', $styleValue)
);
break;
@ -1009,7 +1009,15 @@ class Html extends BaseReader
$borderStyle = Border::BORDER_NONE;
$color = null;
} else {
[, $borderStyle, $color] = explode(' ', $styleValue);
$borderArray = explode(' ', $styleValue);
$borderCount = count($borderArray);
if ($borderCount >= 3) {
$borderStyle = $borderArray[1];
$color = $borderArray[2];
} else {
$borderStyle = $borderArray[0];
$color = $borderArray[1] ?? null;
}
}
$cellStyle->applyFromArray([

View File

@ -0,0 +1,115 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PHPUnit\Framework\TestCase;
class Issue2029Test extends TestCase
{
public function testIssue2029(): void
{
$content = <<<'EOF'
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Declaracion en Linea</title>
</head>
<body>
<table>
<tr>
<td>
<table>
<tr>
<td>
<table>
<tbody>
<tr>
<td>CUIT:</td>
<td><label id="lblCUIT" class="text-left">30-53914190-9</label></td>
</tr>
<tr>
<td>Per&iacute;odo</td>
<td><label id="lblPeriodo" class="text-left">02 2021</label></td>
</tr>
<tr>
<td>Secuencia:</td>
<td><label id="lblSecuencia" class="text-left">0 - Original</label></td>
</tr>
<tr>
<td>Contribuyente:</td>
<td><label id="lblContribuyente">SIND DE TRABAJADORES DE IND DE LA ALIMENTACION</label></td>
<td><label id="lblFechaHoy"></label></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border="1px">
<tr>
<th class="text-center">
CUIL
</th>
<th class="text-center">
Apellido y Nombre
</th>
<th class="text-center">
Obra Social
</th>
<th class="text-center">
Corresponde Reducci&oacute;n?
</th>
</tr>
<tr>
<td class="text-center">
12345678901
</td>
<td class="text-center">
EMILIANO ZAPATA SALAZAR
</td>
<td class="text-center">
101208
</td>
<td class="text-center">
Yes
</td>
</tr>
<tr>
<td class="text-center">
23456789012
</td>
<td class="text-center">
FRANCISCO PANCHO VILLA
</td>
<td class="text-center">
101208
</td>
<td class="text-center">
No
</td>
</tr>
</table>
</body>
</html>
EOF;
$reader = new Html();
$spreadsheet = $reader->loadFromString($content);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('CUIT:', $sheet->getCell('A1')->getValue());
self::assertSame('30-53914190-9', $sheet->getCell('B1')->getValue());
self::assertSame('Contribuyente:', $sheet->getCell('A4')->getValue());
self::assertSame('Apellido y Nombre', $sheet->getCell('B9')->getValue());
self::assertEquals('101208', $sheet->getCell('C10')->getValue());
self::assertEquals('Yes', $sheet->getCell('D10')->getValue());
self::assertEquals('23456789012', $sheet->getCell('A11')->getValue());
self::assertEquals('No', $sheet->getCell('D11')->getValue());
}
}