Track down bug in AddressHelper::convertFormulaToA1

This commit is contained in:
Nathan Dench 2021-05-11 09:53:03 +10:00 committed by Mark Baker
parent 03ba547f5a
commit 1a78ecfb10
5 changed files with 41 additions and 10 deletions

View File

@ -72,7 +72,7 @@ class AddressHelper
foreach ($temp as &$value) { foreach ($temp as &$value) {
// Only replace in alternate array entries (i.e. non-quoted blocks) // Only replace in alternate array entries (i.e. non-quoted blocks)
if ($key = !$key) { if ($key = !$key) {
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); preg_match_all('/(?:R(?:(?:\[-?\d*\])|(?:\d*))?)(?:C(?:(?:\[-?\d*\])|(?:\d*))?)/i', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
// through the formula from left to right. Reversing means that we work right to left.through // through the formula from left to right. Reversing means that we work right to left.through
// the formula // the formula

View File

@ -143,7 +143,7 @@ class AddressHelperTest extends TestCase
/** /**
* @dataProvider providerConvertFormulaToA1FromR1C1Relative * @dataProvider providerConvertFormulaToA1FromR1C1Relative
*/ */
public function testConvertFormulaToA1R1C1Relative(string $expectedValue, string $formula, int $row, int $column): void public function testConvertFormulaToA1FromR1C1Relative(string $expectedValue, string $formula, int $row, int $column): void
{ {
$actualValue = AddressHelper::convertFormulaToA1($formula, $row, $column); $actualValue = AddressHelper::convertFormulaToA1($formula, $row, $column);

View File

@ -1,8 +1,16 @@
<?php <?php
return [ return [
['=B4+C4', '=R4C2+R4C3'], // Basic arithmetic
['=B3+C3', '=R[2]C[1]+R[2]C[2]'], ['=D3+F7+G4+C6+5', '=R3C4+R7C6+R4C7+R6C3+5'],
['=SUM(B1:B4)', '=SUM(R1C2:R4C2)'], ['=D3-F7-G4-C6-5', '=R3C4-R7C6-R4C7-R6C3-5'],
['=CONCAT("Result of formula expression =R1C1+R1C2 is: ", A1+B1)', '=CONCAT("Result of formula expression =R1C1+R1C2 is: ", R1C1+R1C2)'], ['=D3*F7*G4*C6*5', '=R3C4*R7C6*R4C7*R6C3*5'],
['=D3/F7/G4/C6/5', '=R3C4/R7C6/R4C7/R6C3/5'],
// Formulas
['=SUM(E1:E5)', '=SUM(R1C5:R5C5)'],
['=SUM(E1:E5, D5)', '=SUM(R1C5:R5C5, R5C4)'],
['=SUM(E1:E5, D5)-C5', '=SUM(R1C5:R5C5, R5C4)-R5C3'],
['=IF(E1>E2, E3, E4)', '=IF(R1C5>R2C5, R3C5, R4C5)'],
// String literals
['=CONCAT("Result of formula expression =R3C3+R4C3 is: ", C3+C4)', '=CONCAT("Result of formula expression =R3C3+R4C3 is: ", R3C3+R4C3)'],
]; ];

View File

@ -1,7 +1,21 @@
<?php <?php
return [ return [
['=N18+O18', '=R[2]C[1]+R[2]C[2]', 16, 13], // Basic arithmetic without the current row/column
['=D3+F7+G4+C6+5', '=R[-2]C[-1]+R[2]C[1]+R[-1]C[2]+R[1]C[-2]+5', 5, 5],
['=D3-F7-G4-C6-5', '=R[-2]C[-1]-R[2]C[1]-R[-1]C[2]-R[1]C[-2]-5', 5, 5],
['=D3*F7*G4*C6*5', '=R[-2]C[-1]*R[2]C[1]*R[-1]C[2]*R[1]C[-2]*5', 5, 5],
['=D3/F7/G4/C6/5', '=R[-2]C[-1]/R[2]C[1]/R[-1]C[2]/R[1]C[-2]/5', 5, 5],
// Basic Arithmetic with the current row/column
['=E3+E7+G5+C5+E5+5', '=R[-2]C+R[2]C+RC[2]+RC[-2]+RC+5', 5, 5],
['=E3-E7-G5-C5-E5-5', '=R[-2]C-R[2]C-RC[2]-RC[-2]-RC-5', 5, 5],
['=E3*E7*G5*C5*E5*5', '=R[-2]C*R[2]C*RC[2]*RC[-2]*RC*5', 5, 5],
['=E3/E7/G5/C5/E5/5', '=R[-2]C/R[2]C/RC[2]/RC[-2]/RC/5', 5, 5],
// Formulas
['=SUM(E1:E5)', '=SUM(R[-4]C:RC)', 5, 5], ['=SUM(E1:E5)', '=SUM(R[-4]C:RC)', 5, 5],
['=SUM(E1:E5, D5)', '=SUM(R[-4]C:RC, RC[-1])', 5, 5],
['=SUM(E1:E5, D5)-C5', '=SUM(R[-4]C:RC, RC[-1])-RC[-2]', 5, 5],
['=IF(E1>E2, E3, E4)', '=IF(R[-4]C>R[-3]C, R[-2]C, R[-1]C)', 5, 5],
// String literals
['=CONCAT("Result of formula expression =R[-2]C[-2]+R[-1]C[-2] is: ", C3+C4)', '=CONCAT("Result of formula expression =R[-2]C[-2]+R[-1]C[-2] is: ", R[-2]C[-2]+R[-1]C[-2])', 5, 5], ['=CONCAT("Result of formula expression =R[-2]C[-2]+R[-1]C[-2] is: ", C3+C4)', '=CONCAT("Result of formula expression =R[-2]C[-2]+R[-1]C[-2] is: ", R[-2]C[-2]+R[-1]C[-2])', 5, 5],
]; ];

View File

@ -1,7 +1,16 @@
<?php <?php
return [ return [
['=B4+C4', 'of:=[.B4]+[.C4]'], // Basic arithmetic
['=SUM(B1:B4)', 'of:=SUM([.B1:.B4])'], ['=D3+F7+G4+C6+5', 'of:=[.D3]+[.F7]+[.G4]+[.C6]+5'],
['=CONCAT("Result of formula expression =[.B4]+[.C4] is: ", B4+C4)', 'of:=CONCAT("Result of formula expression =[.B4]+[.C4] is: ", [.B4]+[.C4])'], ['=D3-F7-G4-C6-5', 'of:=[.D3]-[.F7]-[.G4]-[.C6]-5'],
['=D3*F7*G4*C6*5', 'of:=[.D3]*[.F7]*[.G4]*[.C6]*5'],
['=D3/F7/G4/C6/5', 'of:=[.D3]/[.F7]/[.G4]/[.C6]/5'],
// Formulas
['=SUM(E1:E5)', 'of:=SUM([.E1:.E5])'],
['=SUM(E1:E5, D5)', 'of:=SUM([.E1:.E5], [.D5])'],
['=SUM(E1:E5, D5)-C5', 'of:=SUM([.E1:.E5], [.D5])-[.C5]'],
['=IF(E1>E2, E3, E4)', 'of:=IF([.E1]>[.E2], [.E3], [.E4])'],
// String literals
['=CONCAT("Result of formula expression =[.C3]+[.C4] is: ", C3+C4)', 'of:=CONCAT("Result of formula expression =[.C3]+[.C4] is: ", [.C3]+[.C4])'],
]; ];