Validations.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
  4. use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
  5. use PhpOffice\PhpSpreadsheet\Cell\CellRange;
  6. class Validations
  7. {
  8. /**
  9. * Validate a cell address.
  10. *
  11. * @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
  12. * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
  13. */
  14. public static function validateCellAddress($cellAddress): string
  15. {
  16. if (is_string($cellAddress)) {
  17. [$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
  18. // if (!empty($worksheet) && $worksheet !== $this->getTitle()) {
  19. // throw new Exception('Reference is not for this worksheet');
  20. // }
  21. return empty($worksheet) ? strtoupper($address) : $worksheet . '!' . strtoupper($address);
  22. }
  23. if (is_array($cellAddress)) {
  24. $cellAddress = CellAddress::fromColumnRowArray($cellAddress);
  25. }
  26. return (string) $cellAddress;
  27. }
  28. /**
  29. * Validate a cell address or cell range.
  30. *
  31. * @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  32. * or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  33. * or as a CellAddress or AddressRange object.
  34. */
  35. public static function validateCellOrCellRange($cellRange): string
  36. {
  37. if (is_string($cellRange) || is_numeric($cellRange)) {
  38. // Convert a single column reference like 'A' to 'A:A',
  39. // a single row reference like '1' to '1:1'
  40. $cellRange = (string) preg_replace('/^([A-Z]+|\d+)$/', '${1}:${1}', (string) $cellRange);
  41. } elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
  42. $cellRange = new CellRange($cellRange, $cellRange);
  43. }
  44. return self::validateCellRange($cellRange);
  45. }
  46. /**
  47. * Validate a cell range.
  48. *
  49. * @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  50. * or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  51. * or as an AddressRange object.
  52. */
  53. public static function validateCellRange($cellRange): string
  54. {
  55. if (is_string($cellRange)) {
  56. [$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
  57. // Convert Column ranges like 'A:C' to 'A1:C1048576'
  58. // or Row ranges like '1:3' to 'A1:XFD3'
  59. $addressRange = (string) preg_replace(
  60. ['/^([A-Z]+):([A-Z]+)$/i', '/^(\\d+):(\\d+)$/'],
  61. ['${1}1:${2}1048576', 'A${1}:XFD${2}'],
  62. $addressRange
  63. );
  64. return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange);
  65. }
  66. if (is_array($cellRange)) {
  67. [$from, $to] = array_chunk($cellRange, 2);
  68. $cellRange = new CellRange(CellAddress::fromColumnRowArray($from), CellAddress::fromColumnRowArray($to));
  69. }
  70. return (string) $cellRange;
  71. }
  72. public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
  73. {
  74. // Uppercase coordinate
  75. $coordinate = strtoupper($coordinate);
  76. // Eliminate leading equal sign
  77. $testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
  78. $defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);
  79. if ($defined !== null) {
  80. if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
  81. $coordinate = (string) preg_replace('/^=/', '', $defined->getValue());
  82. }
  83. }
  84. return $coordinate;
  85. }
  86. }