Skip to content

Fix for Issue 2029 (Invalid Cell Coordinate A-1) #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private function processDomElementTable(Worksheet $sheet, int &$row, string &$co
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);
Expand Down Expand Up @@ -878,14 +878,14 @@ private function applyInlineStyle(&$sheet, $row, $column, $attributeArray): void

case 'width':
$sheet->getColumnDimension($column)->setWidth(
(float) str_replace('px', '', $styleValue)
(float) str_replace(['px', 'pt'], '', $styleValue)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have half of a helper class that covers a wide range of the UoM that can be applied in html styles, with conversion factors for import to Spreadsheet styles... but this will work well enough for the moment... it's the relative units that are a real annoyance, because we can't adjudge 50% accurately, as we don't know 50% of what? I'll do that as a separate PR at some point though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be really helpful, especially because Excel widths and heights aren't well documented. They aren't pixels, or points, or twips, or ... I know the default column width in Excel units and in pixels, but I haven't had time to establish whether there is a linear relationship between them. The Html writer and reader probably make things too wide or too narrow because of this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't really mean non-linear above - it is linear, but it depends on the font. Which may be too sophisticated to try to emulate (as with your 50% comment). But I think it translates better to expressing the unit as ems rather than points or pixels.

);

break;

case 'height':
$sheet->getRowDimension($row)->setRowHeight(
(float) str_replace('px', '', $styleValue)
(float) str_replace(['px', 'pt'], '', $styleValue)
);

break;
Expand Down Expand Up @@ -1009,7 +1009,15 @@ private function setBorderStyle(Style $cellStyle, $styleValue, $type): void
$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([
Expand Down
115 changes: 115 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Html/Issue2029Test.php
Original file line number Diff line number Diff line change
@@ -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());
}
}