From 2abe56b9469de98fc49918285ccf01c73f4a945f Mon Sep 17 00:00:00 2001 From: GreatHumorist Date: Fri, 22 Sep 2017 13:49:38 +0800 Subject: [PATCH] Support missing attribute `r` in `c` node when reading xlsx When describing a cell, the cell reference (r="A1") is optional. When not present, we should just increment the index of the last processed row. Fixes #201 Closes #225 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Xlsx.php | 7 +++++++ tests/PhpSpreadsheetTests/Reader/XLSXTest.php | 19 ++++++++++++++++++ .../Reader/XLSX/without_cell_reference.xlsx | Bin 0 -> 6384 bytes 4 files changed, 27 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Reader/XLSXTest.php create mode 100644 tests/data/Reader/XLSX/without_cell_reference.xlsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ee38170..e399d3e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Control characters in cell values are automatically escaped - [#212](https://github.com/PHPOffice/PhpSpreadsheet/issues/212) - Prevent color changing when copy/pasting xls files written by PhpSpreadsheet to another file - @al-lala [#218](https://github.com/PHPOffice/PhpSpreadsheet/issues/218) +- Add cell reference automatic when there is no cell reference('r' attribute) in Xlsx file. - @GreatHumorist [#225](https://github.com/PHPOffice/PhpSpreadsheet/pull/225) Refer to [issue#201](https://github.com/PHPOffice/PhpSpreadsheet/issues/201) ### BREAKING CHANGE diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 5b4b3e92..8958e465 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -848,6 +848,7 @@ class Xlsx extends BaseReader implements IReader } if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) { + $cIndex = 1; // Cell Start from 1 foreach ($xmlSheet->sheetData->row as $row) { if ($row['ht'] && !$this->readDataOnly) { $docSheet->getRowDimension((int) ($row['r']))->setRowHeight((float) ($row['ht'])); @@ -865,8 +866,12 @@ class Xlsx extends BaseReader implements IReader $docSheet->getRowDimension((int) ($row['r']))->setXfIndex((int) ($row['s'])); } + $rowIndex = 0; // Start form zero because Cell::stringFromColumnIndex start from A default, actually is 1 foreach ($row->c as $c) { $r = (string) $c['r']; + if ($r == '') { + $r = Cell::stringFromColumnIndex($rowIndex) . $cIndex; + } $cellDataType = (string) $c['t']; $value = null; $calculatedValue = null; @@ -963,7 +968,9 @@ class Xlsx extends BaseReader implements IReader $cell->setXfIndex(isset($styles[(int) ($c['s'])]) ? (int) ($c['s']) : 0); } + $rowIndex += 1; } + $cIndex += 1; } } diff --git a/tests/PhpSpreadsheetTests/Reader/XLSXTest.php b/tests/PhpSpreadsheetTests/Reader/XLSXTest.php new file mode 100644 index 00000000..aab9c6ea --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/XLSXTest.php @@ -0,0 +1,19 @@ +load($filename); + } +} diff --git a/tests/data/Reader/XLSX/without_cell_reference.xlsx b/tests/data/Reader/XLSX/without_cell_reference.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..99e5f1d4295a03e4b188c2e813e87f9170ed4350 GIT binary patch literal 6384 zcmaJ_1z42b5?;C+1VJh3URb1I5fEf)>26qRL6DX%r9+Sq7bFB+Ksp8K2I+1=>5#4q z`ultTxc5BI_nm$AoA=C|dFH%lW>g?3sMvs;@m+)z^5e%JFO=)q-Hu!34*|>{0)sA? z#V(it!1`kV0Pm-Ovx|qFg)^7CyaGOuqI&1$DUxd=Q`Caz_?R~);q1}w+1@cwwDYNVdGx(azMcw`6a^PY z=o+O$fXi*B_2-voo9CukwtCCV5D9q_!OsMa1xAVumo{}Wp_o2(frL_Lw+Scf zcq8l`G0c3uk{yiWd`sj@$7N3(D(+XL;+}Tf6Oqb>SfmsZotb1V;TA2zA#vmVTW+c0 zw2=bh*yJXpnNrE?uPh1fT!9|nLD#8K)cVRB4H_I_!=3-;C)&(+4jx;aADM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