Skip to content

Commit 30384ac

Browse files
committed
Xls Reader Some Ranges Not Handled Properly
This supersedes PR #607 by @christian-forgacs, who deserves all the credit for reporting the problem and devising the solution. The PR went stale in 2018, and it is just easier to resubmit a clean version rather than clean up the old one. Among the suggestions in the PR was that you should try to create a spreadsheet from scratch to demonstrate the problem rather than supply one. However, my attempts to match the failing spreadsheet do not have a problem when they are read. So, a supplied spreadsheet it is. Fix #1570. No sample spreadsheet was supplied with that issue, but I am almost certain that this is another example of the same problem. I am removing the stale label from that issue; it will be closed properly when this PR is merged.
1 parent f7c183b commit 30384ac

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6859,9 +6859,9 @@ private function readBIFF8CellRangeAddressB(string $subData, string $baseCell =
68596859
$lc = '$' . $lc;
68606860
} else {
68616861
// column offset
6862-
// offset: 4; size: 2; first column with relative/absolute flags
6862+
// offset: 6; size: 2; last column with relative/absolute flags
68636863
// bit: 7-0; mask 0x00FF; column index
6864-
$relativeLcIndex = 0x00FF & self::getInt2d($subData, 4);
6864+
$relativeLcIndex = 0x00FF & self::getInt2d($subData, 6);
68656865
$lcIndex = $baseCol + $relativeLcIndex;
68666866
$lcIndex = ($lcIndex < 256) ? $lcIndex : $lcIndex - 256;
68676867
$lcIndex = ($lcIndex >= 0) ? $lcIndex : $lcIndex + 256;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
6+
7+
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8+
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class Pr607Test extends TestCase
12+
{
13+
/**
14+
* Test file with cell range expressed in unexpected manner.
15+
*/
16+
public static function testSumData(): void
17+
{
18+
$filename = 'tests/data/Reader/XLS/pr607.sum_data.xls';
19+
$reader = new XlsReader();
20+
$spreadsheet = $reader->load($filename);
21+
22+
$tests = [
23+
'Test' => [
24+
'A1' => 1,
25+
'A2' => 2,
26+
'A3' => 3,
27+
'A4' => 4,
28+
'A5' => 5,
29+
'A6' => 6,
30+
'A7' => 7,
31+
'A8' => 8,
32+
'A9' => 9,
33+
'A10' => 10,
34+
35+
'B1' => 3,
36+
'B2' => 4,
37+
'B3' => 5,
38+
'B4' => 6,
39+
'B5' => 7,
40+
'B6' => 8,
41+
'B7' => 9,
42+
'B8' => 10,
43+
'B9' => 11,
44+
'B10' => 12,
45+
46+
'C1' => 4,
47+
'C2' => 6,
48+
'C3' => 8,
49+
'C4' => 10,
50+
'C5' => 12,
51+
'C6' => 14,
52+
'C7' => 16,
53+
'C8' => 18,
54+
'C9' => 20,
55+
'C10' => 22,
56+
],
57+
];
58+
59+
foreach ($tests as $sheetName => $testsForSheet) {
60+
$sheet = $spreadsheet->getSheetByName($sheetName);
61+
self::assertNotNull($sheet);
62+
63+
foreach ($testsForSheet as $cellCoordinate => $result) {
64+
$calculatedValue = $sheet->getCell($cellCoordinate)->getCalculatedValue();
65+
self::assertSame($result, $calculatedValue, "cell $cellCoordinate");
66+
}
67+
}
68+
$spreadsheet->disconnectWorksheets();
69+
}
70+
}
29 KB
Binary file not shown.

0 commit comments

Comments
 (0)