From cc5c0205d5c87e17716ab6d3047005d0532a75c4 Mon Sep 17 00:00:00 2001 From: oleibman Date: Thu, 29 Apr 2021 13:59:01 -0700 Subject: [PATCH] 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. --- src/PhpSpreadsheet/Reader/Html.php | 16 ++- .../Reader/Html/Issue2029Test.php | 115 ++++++++++++++++++ 2 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Reader/Html/Issue2029Test.php diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 1a0ef5d3..b7faac87 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -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([ diff --git a/tests/PhpSpreadsheetTests/Reader/Html/Issue2029Test.php b/tests/PhpSpreadsheetTests/Reader/Html/Issue2029Test.php new file mode 100644 index 00000000..08cdbc25 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Html/Issue2029Test.php @@ -0,0 +1,115 @@ + + + + + Declaracion en Linea + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + +
CUIT:
Período
Secuencia:
Contribuyente:
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ CUIL + + Apellido y Nombre + + Obra Social + + Corresponde Reducción? +
+ 12345678901 + + EMILIANO ZAPATA SALAZAR + + 101208 + + Yes +
+ 23456789012 + + FRANCISCO PANCHO VILLA + + 101208 + + No +
+ + + +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()); + } +}