PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / LookupRef / Helpers.php

Helpers.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/Helpers.php

76 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
6 use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell;
7 use TablePress\PhpOffice\PhpSpreadsheet\DefinedName;
8 use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
10 class Helpers
11 {
12 public const CELLADDRESS_USE_A1 = true;
13
14 public const CELLADDRESS_USE_R1C1 = false;
15
16 private static function convertR1C1(string &$cellAddress1, ?string &$cellAddress2, bool $a1, ?int $baseRow = null, ?int $baseCol = null): string
17 {
18 if ($a1 === self::CELLADDRESS_USE_R1C1) {
19 $cellAddress1 = AddressHelper::convertToA1($cellAddress1, $baseRow ?? 1, $baseCol ?? 1);
20 if ($cellAddress2) {
21 $cellAddress2 = AddressHelper::convertToA1($cellAddress2, $baseRow ?? 1, $baseCol ?? 1);
22 }
23 }
24
25 return $cellAddress1 . ($cellAddress2 ? ":$cellAddress2" : '');
26 }
27
28 private static function adjustSheetTitle(string &$sheetTitle, ?string $value): void
29 {
30 if ($sheetTitle) {
31 $sheetTitle .= '!';
32 if (stripos($value ?? '', $sheetTitle) === 0) {
33 $sheetTitle = '';
34 }
35 }
36 }
37
38 /** @return array{string, ?string, string} */
39 public static function extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName = '', ?int $baseRow = null, ?int $baseCol = null): array
40 {
41 $cellAddress1 = $cellAddress;
42 $cellAddress2 = null;
43 $namedRange = DefinedName::resolveName($cellAddress1, $sheet, $sheetName);
44 if ($namedRange !== null) {
45 $workSheet = $namedRange->getWorkSheet();
46 $sheetTitle = ($workSheet === null) ? '' : $workSheet->getTitle();
47 $value = (string) preg_replace('/^=/', '', $namedRange->getValue());
48 self::adjustSheetTitle($sheetTitle, $value);
49 $cellAddress1 = $sheetTitle . $value;
50 $cellAddress = $cellAddress1;
51 $a1 = self::CELLADDRESS_USE_A1;
52 }
53 if (str_contains($cellAddress, ':')) {
54 [$cellAddress1, $cellAddress2] = explode(':', $cellAddress);
55 }
56 $cellAddress = self::convertR1C1($cellAddress1, $cellAddress2, $a1, $baseRow, $baseCol);
57
58 return [$cellAddress1, $cellAddress2, $cellAddress];
59 }
60
61 /** @return array{string, ?Worksheet, string} */
62 public static function extractWorksheet(string $cellAddress, Cell $cell): array
63 {
64 $sheetName = '';
65 if (str_contains($cellAddress, '!')) {
66 [$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true, true);
67 }
68
69 $worksheet = ($sheetName !== '')
70 ? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($sheetName)
71 : $cell->getWorksheet();
72
73 return [$cellAddress, $worksheet, $sheetName];
74 }
75 }
76