Skip to content

Commit f4919af

Browse files
authored
Merge pull request #4118 from oleibman/brkupxlsr2
Refactor Xls Reader
2 parents 39fc513 + 1326e37 commit f4919af

File tree

9 files changed

+5643
-6156
lines changed

9 files changed

+5643
-6156
lines changed

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 3278 additions & 6154 deletions
Large diffs are not rendered by default.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6+
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
7+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
8+
9+
class Biff5 extends Xls
10+
{
11+
/**
12+
* Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
13+
* always fixed range
14+
* section 2.5.14.
15+
*/
16+
public static function readBIFF5CellRangeAddressFixed(string $subData): string
17+
{
18+
// offset: 0; size: 2; index to first row
19+
$fr = self::getUInt2d($subData, 0) + 1;
20+
21+
// offset: 2; size: 2; index to last row
22+
$lr = self::getUInt2d($subData, 2) + 1;
23+
24+
// offset: 4; size: 1; index to first column
25+
$fc = ord($subData[4]);
26+
27+
// offset: 5; size: 1; index to last column
28+
$lc = ord($subData[5]);
29+
30+
// check values
31+
if ($fr > $lr || $fc > $lc) {
32+
throw new ReaderException('Not a cell range address');
33+
}
34+
35+
// column index to letter
36+
$fc = Coordinate::stringFromColumnIndex($fc + 1);
37+
$lc = Coordinate::stringFromColumnIndex($lc + 1);
38+
39+
if ($fr == $lr && $fc == $lc) {
40+
return "$fc$fr";
41+
}
42+
43+
return "$fc$fr:$lc$lr";
44+
}
45+
46+
/**
47+
* Read BIFF5 cell range address list
48+
* section 2.5.15.
49+
*/
50+
public static function readBIFF5CellRangeAddressList(string $subData): array
51+
{
52+
$cellRangeAddresses = [];
53+
54+
// offset: 0; size: 2; number of the following cell range addresses
55+
$nm = self::getUInt2d($subData, 0);
56+
57+
$offset = 2;
58+
// offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
59+
for ($i = 0; $i < $nm; ++$i) {
60+
$cellRangeAddresses[] = self::readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
61+
$offset += 6;
62+
}
63+
64+
return [
65+
'size' => 2 + 6 * $nm,
66+
'cellRangeAddresses' => $cellRangeAddresses,
67+
];
68+
}
69+
}

0 commit comments

Comments
 (0)