1 | <?php |
||
11 | class CellHelper |
||
12 | { |
||
13 | // Using ord() is super slow... Using a pre-computed hash table instead. |
||
14 | private static $columnLetterToIndexMapping = [ |
||
15 | 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, |
||
16 | 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, |
||
17 | 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, |
||
18 | 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * Returns the base 10 column index associated to the cell index (base 26). |
||
23 | * Excel uses A to Z letters for column indexing, where A is the 1st column, |
||
24 | * Z is the 26th and AA is the 27th. |
||
25 | * The mapping is zero based, so that A1 maps to 0, B2 maps to 1, Z13 to 25 and AA4 to 26. |
||
26 | * |
||
27 | * @param string $cellIndex The Excel cell index ('A1', 'BC13', ...) |
||
28 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
29 | * @return int |
||
30 | */ |
||
31 | 43 | public static function getColumnIndexFromCellIndex($cellIndex) |
|
67 | |||
68 | /** |
||
69 | * Returns whether a cell index is valid, in an Excel world. |
||
70 | * To be valid, the cell index should start with capital letters and be followed by numbers. |
||
71 | * There can only be 3 letters, as there can only be 16,384 rows, which is equivalent to 'XFE'. |
||
72 | * |
||
73 | * @param string $cellIndex The Excel cell index ('A1', 'BC13', ...) |
||
74 | * @return bool |
||
75 | */ |
||
76 | 43 | protected static function isValidCellIndex($cellIndex) |
|
80 | } |
||
81 |