# tablepress/3.4/libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/Helpers.php

TablePress – Tables in WordPress made easy, version 3.4. 76 lines.

- Page: https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/Helpers.php
- Raw: https://pluginprobe.com/plugins/tablepress/3.4/raw/libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/Helpers.php
- Modified: 2025-08-26T04:06:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/Helpers.php#L10-L20`.

```php
<?php

namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef;

use TablePress\PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell;
use TablePress\PhpOffice\PhpSpreadsheet\DefinedName;
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class Helpers
{
	public const CELLADDRESS_USE_A1 = true;

	public const CELLADDRESS_USE_R1C1 = false;

	private static function convertR1C1(string &$cellAddress1, ?string &$cellAddress2, bool $a1, ?int $baseRow = null, ?int $baseCol = null): string
	{
		if ($a1 === self::CELLADDRESS_USE_R1C1) {
			$cellAddress1 = AddressHelper::convertToA1($cellAddress1, $baseRow ?? 1, $baseCol ?? 1);
			if ($cellAddress2) {
				$cellAddress2 = AddressHelper::convertToA1($cellAddress2, $baseRow ?? 1, $baseCol ?? 1);
			}
		}

		return $cellAddress1 . ($cellAddress2 ? ":$cellAddress2" : '');
	}

	private static function adjustSheetTitle(string &$sheetTitle, ?string $value): void
	{
		if ($sheetTitle) {
			$sheetTitle .= '!';
			if (stripos($value ?? '', $sheetTitle) === 0) {
				$sheetTitle = '';
			}
		}
	}

	/** @return array{string, ?string, string} */
	public static function extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName = '', ?int $baseRow = null, ?int $baseCol = null): array
	{
		$cellAddress1 = $cellAddress;
		$cellAddress2 = null;
		$namedRange = DefinedName::resolveName($cellAddress1, $sheet, $sheetName);
		if ($namedRange !== null) {
			$workSheet = $namedRange->getWorkSheet();
			$sheetTitle = ($workSheet === null) ? '' : $workSheet->getTitle();
			$value = (string) preg_replace('/^=/', '', $namedRange->getValue());
			self::adjustSheetTitle($sheetTitle, $value);
			$cellAddress1 = $sheetTitle . $value;
			$cellAddress = $cellAddress1;
			$a1 = self::CELLADDRESS_USE_A1;
		}
		if (str_contains($cellAddress, ':')) {
			[$cellAddress1, $cellAddress2] = explode(':', $cellAddress);
		}
		$cellAddress = self::convertR1C1($cellAddress1, $cellAddress2, $a1, $baseRow, $baseCol);

		return [$cellAddress1, $cellAddress2, $cellAddress];
	}

	/** @return array{string, ?Worksheet, string} */
	public static function extractWorksheet(string $cellAddress, Cell $cell): array
	{
		$sheetName = '';
		if (str_contains($cellAddress, '!')) {
			[$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true, true);
		}

		$worksheet = ($sheetName !== '')
			? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($sheetName)
			: $cell->getWorksheet();

		return [$cellAddress, $worksheet, $sheetName];
	}
}

```
