Skip to content

Commit 34b86af

Browse files
committed
add AI folder
1 parent 3d29947 commit 34b86af

File tree

5 files changed

+85046
-0
lines changed

5 files changed

+85046
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import re
2+
3+
formula = "=-(scenarios!R39C4+scenarios!R40C4)*debt!R[-5]C*time_macro!R[7]C[-1]+'Annual CF'!RC * 'Time&Macro'!R16C-RC*R[-15]C3"
4+
5+
def get_all_references(formula):
6+
"""
7+
Finds all R1C1-style cell references in a formula string using regex.
8+
Returns a list of tuples, where each tuple is (sheet, row_str, col_str).
9+
"""
10+
pattern = r"(?:(?P<sheet>'[^']+'|\w+)!)?R(?P<row>(?:\[-?\d+\])|\d*)C(?P<col>(?:\[-?\d+\])|\d*)"
11+
references = re.findall(pattern, formula)
12+
return references
13+
14+
def convert_reference_to_absolute(ref, current_row, current_col):
15+
"""
16+
Converts a single R1C1 reference tuple to absolute 0-indexed coordinates.
17+
ref is a tuple: (sheet, row_str, col_str)
18+
"""
19+
sheet, row, col = ref
20+
# Clean up sheet name by removing single quotes if they exist
21+
sheet = sheet.strip("'")
22+
23+
if row.startswith('['):
24+
row_offset = int(row[1:-1])
25+
abs_row = current_row + row_offset
26+
elif row == '':
27+
abs_row = current_row
28+
else:
29+
abs_row = int(row) - 1 # Convert to 0-based index
30+
31+
if col.startswith('['):
32+
col_offset = int(col[1:-1])
33+
abs_col = current_col + col_offset
34+
elif col == '':
35+
abs_col = current_col
36+
else:
37+
abs_col = int(col) - 1 # Convert to 0-based index
38+
39+
return (sheet, abs_row, abs_col)
40+
41+
def get_absolute_references(formula, current_row, current_col):
42+
"""
43+
Parses a formula to find all cell references and convert them to absolute coordinates.
44+
"""
45+
references = get_all_references(formula)
46+
absolute_references = [convert_reference_to_absolute(ref, current_row, current_col) for ref in references]
47+
return absolute_references
48+
49+
def main():
50+
current_row = 10 # Example current row (0-based)
51+
current_col = 5 # Example current column (0-based)
52+
53+
references = get_all_references(formula)
54+
absolute_references = get_absolute_references(formula, current_row, current_col)
55+
56+
for ref, abs_ref in zip(references, absolute_references):
57+
print(f"Original: {ref} -> Absolute: {abs_ref}")
58+
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)