ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Helpers.php
Go to the documentation of this file.
1 <?php
2 
4 
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): string
17  {
18  if ($a1 === self::CELLADDRESS_USE_R1C1) {
19  $cellAddress1 = AddressHelper::convertToA1($cellAddress1);
20  if ($cellAddress2) {
21  $cellAddress2 = AddressHelper::convertToA1($cellAddress2);
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  public static function extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName = ''): array
39  {
40  $cellAddress1 = $cellAddress;
41  $cellAddress2 = null;
42  $namedRange = DefinedName::resolveName($cellAddress1, $sheet, $sheetName);
43  if ($namedRange !== null) {
44  $workSheet = $namedRange->getWorkSheet();
45  $sheetTitle = ($workSheet === null) ? '' : $workSheet->getTitle();
46  $value = preg_replace('/^=/', '', $namedRange->getValue());
47  self::adjustSheetTitle($sheetTitle, $value);
48  $cellAddress1 = $sheetTitle . $value;
49  $cellAddress = $cellAddress1;
50  $a1 = self::CELLADDRESS_USE_A1;
51  }
52  if (strpos($cellAddress, ':') !== false) {
53  [$cellAddress1, $cellAddress2] = explode(':', $cellAddress);
54  }
55  $cellAddress = self::convertR1C1($cellAddress1, $cellAddress2, $a1);
56 
57  return [$cellAddress1, $cellAddress2, $cellAddress];
58  }
59 
60  public static function extractWorksheet(string $cellAddress, Cell $pCell): array
61  {
62  $sheetName = '';
63  if (strpos($cellAddress, '!') !== false) {
64  [$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true);
65  $sheetName = trim($sheetName, "'");
66  }
67 
68  $pSheet = ($sheetName !== '')
69  ? $pCell->getWorksheet()->getParent()->getSheetByName($sheetName)
70  : $pCell->getWorksheet();
71 
72  return [$cellAddress, $pSheet, $sheetName];
73  }
74 }
static extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName='')
Definition: Helpers.php:38
static extractSheetTitle($pRange, $returnRange=false)
Extract worksheet title from range.
Definition: Worksheet.php:2719
static convertR1C1(string &$cellAddress1, ?string &$cellAddress2, bool $a1)
Definition: Helpers.php:16
static adjustSheetTitle(string &$sheetTitle, ?string $value)
Definition: Helpers.php:28
static resolveName(string $pDefinedName, Worksheet $pSheet, string $sheetName='')
Resolve a named range to a regular cell range or formula.
getWorksheet()
Get parent worksheet.
Definition: Cell.php:479
static extractWorksheet(string $cellAddress, Cell $pCell)
Definition: Helpers.php:60