This problem is the same as #1238, which was resolved by #1239. For that issue, the fix was to check in one place whether $this->mapCellXfIndex[$xfIndex] was set before using it. The sample spreadsheet supplied as a description for this problem had exactly the same problem in 2 other places in the code. In addition, there were 7 other places in the code where that particular item was used unchecked. This fix corrects all 9 locations. The spreadsheet supplied with the problem is used as the basis for some new tests, which particularly test column dimensions and styles, the problems involved in this case.
This commit is contained in:
parent
3844186397
commit
38fab4e632
|
@ -3622,7 +3622,9 @@ class Xls extends BaseReader
|
|||
$this->phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
|
||||
$this->phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
|
||||
$this->phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
|
||||
$this->phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
if (isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
$this->phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3731,7 +3733,7 @@ class Xls extends BaseReader
|
|||
$numValue = self::getIEEE754($rknum);
|
||||
|
||||
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add style information
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -3866,7 +3868,7 @@ class Xls extends BaseReader
|
|||
// offset: var; size: 4; RK value
|
||||
$numValue = self::getIEEE754(self::getInt4d($recordData, $offset + 2));
|
||||
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add style
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -3910,7 +3912,7 @@ class Xls extends BaseReader
|
|||
$numValue = self::extractNumber(substr($recordData, 6, 8));
|
||||
|
||||
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add cell style
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -4018,7 +4020,7 @@ class Xls extends BaseReader
|
|||
}
|
||||
|
||||
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add cell style
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -4156,7 +4158,7 @@ class Xls extends BaseReader
|
|||
break;
|
||||
}
|
||||
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add cell style
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -4194,7 +4196,9 @@ class Xls extends BaseReader
|
|||
// Read cell?
|
||||
if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
|
||||
$xfIndex = self::getUInt2d($recordData, 4 + 2 * $i);
|
||||
$this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
if (isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
$this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4245,7 +4249,7 @@ class Xls extends BaseReader
|
|||
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
|
||||
$cell->setValueExplicit($value, DataType::TYPE_STRING);
|
||||
|
||||
if (!$this->readDataOnly) {
|
||||
if (!$this->readDataOnly && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
// add cell style
|
||||
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
|
@ -4277,7 +4281,7 @@ class Xls extends BaseReader
|
|||
$xfIndex = self::getUInt2d($recordData, 4);
|
||||
|
||||
// add style information
|
||||
if (!$this->readDataOnly && $this->readEmptyCells) {
|
||||
if (!$this->readDataOnly && $this->readEmptyCells && isset($this->mapCellXfIndex[$xfIndex])) {
|
||||
$this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
||||
|
||||
class XlsTest extends TestCase
|
||||
class XlsTest extends AbstractFunctional
|
||||
{
|
||||
/**
|
||||
* Test load Xls file.
|
||||
|
@ -17,4 +17,30 @@ class XlsTest extends TestCase
|
|||
$spreadsheet = $reader->load($filename);
|
||||
self::assertEquals('Title', $spreadsheet->getSheet(0)->getCell('A1')->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test load Xls file with invalid xfIndex.
|
||||
*/
|
||||
public function testLoadXlsBug1505(): void
|
||||
{
|
||||
$filename = 'tests/data/Reader/XLS/bug1505.xls';
|
||||
$reader = new Xls();
|
||||
$spreadsheet = $reader->load($filename);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$col = $sheet->getHighestColumn();
|
||||
$row = $sheet->getHighestRow();
|
||||
|
||||
$newspreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
|
||||
$newsheet = $newspreadsheet->getActiveSheet();
|
||||
$newcol = $newsheet->getHighestColumn();
|
||||
$newrow = $newsheet->getHighestRow();
|
||||
|
||||
self::assertEquals($spreadsheet->getSheetCount(), $newspreadsheet->getSheetCount());
|
||||
self::assertEquals($sheet->getTitle(), $newsheet->getTitle());
|
||||
self::assertEquals($sheet->getColumnDimensions(), $newsheet->getColumnDimensions());
|
||||
self::assertEquals($col, $newcol);
|
||||
self::assertEquals($row, $newrow);
|
||||
self::assertEquals($sheet->getCell('A1')->getFormattedValue(), $newsheet->getCell('A1')->getFormattedValue());
|
||||
self::assertEquals($sheet->getCell("$col$row")->getFormattedValue(), $newsheet->getCell("$col$row")->getFormattedValue());
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue