16 int $currentRowNumber = 1,
17 int $currentColumnNumber = 1
19 $validityCheck = preg_match(
'/^(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))$/i', $address, $cellReference);
21 if ($validityCheck === 0) {
22 throw new Exception(
'Invalid R1C1-format Cell Reference');
25 $rowReference = $cellReference[2];
27 if ($rowReference ===
'') {
28 $rowReference = (string) $currentRowNumber;
31 if ($rowReference[0] ===
'[') {
32 $rowReference = $currentRowNumber + (int) trim($rowReference,
'[]');
34 $columnReference = $cellReference[4];
36 if ($columnReference ===
'') {
37 $columnReference = (string) $currentColumnNumber;
40 if (is_string($columnReference) && $columnReference[0] ===
'[') {
41 $columnReference = $currentColumnNumber + (int) trim($columnReference,
'[]');
44 if ($columnReference <= 0 || $rowReference <= 0) {
45 throw new Exception(
'Invalid R1C1-format Cell Reference, Value out of range');
49 return $A1CellReference;
54 $formula = substr($formula, 3);
55 $temp = explode(
'"', $formula);
57 foreach ($temp as &$value) {
60 $value = str_replace([
'[.',
':.',
']'], [
'',
':',
''], $value);
65 return implode(
'"', $temp);
73 int $currentRowNumber = 1,
74 int $currentColumnNumber = 1
76 if (substr($formula, 0, 3) ==
'of:') {
78 return self::convertSpreadsheetMLFormula($formula);
82 $temp = explode(
'"', $formula);
84 foreach ($temp as &$value) {
87 preg_match_all(self::R1C1_COORDINATE_REGEX, $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
91 $cellReferences = array_reverse($cellReferences);
94 foreach ($cellReferences as $cellReference) {
95 $A1CellReference = self::convertToA1($cellReference[0][0], $currentRowNumber, $currentColumnNumber);
96 $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
103 return implode(
'"', $temp);
112 ?
int $currentRowNumber = null,
113 ?
int $currentColumnNumber = null
117 if ($validityCheck === 0) {
118 throw new Exception(
'Invalid A1-format Cell Reference');
122 if ($cellReference[
'absolute_col'] ===
'$') {
124 $currentColumnNumber = null;
127 $rowId = (int) $cellReference[
'row_ref'];
128 if ($cellReference[
'absolute_row'] ===
'$') {
130 $currentRowNumber = null;
133 if ($currentRowNumber !== null) {
134 if ($rowId === $currentRowNumber) {
137 $rowId =
'[' . ($rowId - $currentRowNumber) .
']';
141 if ($currentColumnNumber !== null) {
142 if ($columnId === $currentColumnNumber) {
145 $columnId =
'[' . ($columnId - $currentColumnNumber) .
']';
149 $R1C1Address =
"R{$rowId}C{$columnId}";
const R1C1_COORDINATE_REGEX
const A1_COORDINATE_REGEX
static convertFormulaToA1(string $formula, int $currentRowNumber=1, int $currentColumnNumber=1)
Converts a formula that uses R1C1/SpreadsheetXML format cell address to an A1 format cell address...
static convertToR1C1(string $address, ?int $currentRowNumber=null, ?int $currentColumnNumber=null)
Converts an A1 format cell address to an R1C1 format cell address.
static convertToA1(string $address, int $currentRowNumber=1, int $currentColumnNumber=1)
Converts an R1C1 format cell address to an A1 format cell address.
static convertSpreadsheetMLFormula(string $formula)
static columnIndexFromString($pString)
Column index from string.
static stringFromColumnIndex($columnIndex)
String from column index.