@@ 677-710 (lines=34) @@ | ||
674 | // echo 'Before: ', $cellDataFormula,'<br />'; |
|
675 | $temp = explode('"', $cellDataFormula); |
|
676 | $key = false; |
|
677 | foreach ($temp as &$value) { |
|
678 | // Only replace in alternate array entries (i.e. non-quoted blocks) |
|
679 | if ($key = !$key) { |
|
680 | preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); |
|
681 | // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way |
|
682 | // through the formula from left to right. Reversing means that we work right to left.through |
|
683 | // the formula |
|
684 | $cellReferences = array_reverse($cellReferences); |
|
685 | // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, |
|
686 | // then modify the formula to use that new reference |
|
687 | foreach ($cellReferences as $cellReference) { |
|
688 | $rowReference = $cellReference[2][0]; |
|
689 | // Empty R reference is the current row |
|
690 | if ($rowReference == '') { |
|
691 | $rowReference = $rowID; |
|
692 | } |
|
693 | // Bracketed R references are relative to the current row |
|
694 | if ($rowReference{0} == '[') { |
|
695 | $rowReference = $rowID + trim($rowReference, '[]'); |
|
696 | } |
|
697 | $columnReference = $cellReference[4][0]; |
|
698 | // Empty C reference is the current column |
|
699 | if ($columnReference == '') { |
|
700 | $columnReference = $columnNumber; |
|
701 | } |
|
702 | // Bracketed C references are relative to the current column |
|
703 | if ($columnReference{0} == '[') { |
|
704 | $columnReference = $columnNumber + trim($columnReference, '[]'); |
|
705 | } |
|
706 | $A1CellReference = \PhpSpreadsheet\Cell::stringFromColumnIndex($columnReference-1).$rowReference; |
|
707 | $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); |
|
708 | } |
|
709 | } |
|
710 | } |
|
711 | } |
|
712 | unset($value); |
|
713 | // Then rebuild the formula string |
@@ 303-337 (lines=35) @@ | ||
300 | // Convert R1C1 style references to A1 style references (but only when not quoted) |
|
301 | $temp = explode('"', $cellDataFormula); |
|
302 | $key = false; |
|
303 | foreach ($temp as &$value) { |
|
304 | // Only count/replace in alternate array entries |
|
305 | if ($key = !$key) { |
|
306 | preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER+PREG_OFFSET_CAPTURE); |
|
307 | // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way |
|
308 | // through the formula from left to right. Reversing means that we work right to left.through |
|
309 | // the formula |
|
310 | $cellReferences = array_reverse($cellReferences); |
|
311 | // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, |
|
312 | // then modify the formula to use that new reference |
|
313 | foreach ($cellReferences as $cellReference) { |
|
314 | $rowReference = $cellReference[2][0]; |
|
315 | // Empty R reference is the current row |
|
316 | if ($rowReference == '') { |
|
317 | $rowReference = $row; |
|
318 | } |
|
319 | // Bracketed R references are relative to the current row |
|
320 | if ($rowReference{0} == '[') { |
|
321 | $rowReference = $row + trim($rowReference, '[]'); |
|
322 | } |
|
323 | $columnReference = $cellReference[4][0]; |
|
324 | // Empty C reference is the current column |
|
325 | if ($columnReference == '') { |
|
326 | $columnReference = $column; |
|
327 | } |
|
328 | // Bracketed C references are relative to the current column |
|
329 | if ($columnReference{0} == '[') { |
|
330 | $columnReference = $column + trim($columnReference, '[]'); |
|
331 | } |
|
332 | $A1CellReference = \PhpSpreadsheet\Cell::stringFromColumnIndex($columnReference-1).$rowReference; |
|
333 | ||
334 | $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); |
|
335 | } |
|
336 | } |
|
337 | } |
|
338 | unset($value); |
|
339 | // Then rebuild the formula string |
|
340 | $cellDataFormula = implode('"', $temp); |