Skip to content

Commit

Permalink
Support missing attribute r in c node when reading xlsx
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yasar-luo authored and PowerKiKi committed Sep 22, 2017
1 parent 7aa6233 commit 2abe56b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ public function load($pFilename)
}

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']));
Expand All @@ -865,8 +866,12 @@ public function load($pFilename)
$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;
Expand Down Expand Up @@ -963,7 +968,9 @@ public function load($pFilename)
$cell->setXfIndex(isset($styles[(int) ($c['s'])]) ?
(int) ($c['s']) : 0);
}
$rowIndex += 1;
}
$cIndex += 1;
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/XLSXTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit_Framework_TestCase;

class XLSXTest extends PHPUnit_Framework_TestCase
{
/**
* Test load Xlsx file without cell reference.
*/
public function testLoadXlsxWithoutCellReference()
{
$filename = './data/Reader/XLSX/without_cell_reference.xlsx';
$reader = new Xlsx();
$reader->load($filename);
}
}
Binary file not shown.

0 comments on commit 2abe56b

Please sign in to comment.