Repository navigation
Expand file tree
/
Copy pathformula_fill.py
More file actions
345 lines (290 loc) · 10.5 KB
/
Copy pathformula_fill.py
File metadata and controls
345 lines (290 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Fill-down / fill-across and repeated-formula detection.
``write_range`` fills one formula across a range instead of repeating
the same text in every cell. Ordinary formulas shift relative A1 refs per cell
(``$`` stays absolute). ``=PY`` / PYTHON / PYTHONFUNCTION formulas use a
direction-aware span policy: only refs whose span does not cross the fill
axis are adjusted; other refs outside the quoted Python string stay
verbatim so a block like ``A1:H1517`` is not walked down the column.
``repeated_formula_columns`` detects list-write columns that repeat a single
row-aligned formula so callers can warn.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Callable, Sequence
from plugin.calc.address_utils import (
MAX_COL,
MAX_ROW,
column_to_index,
index_to_column,
)
from plugin.calc.array_formula import skip_quoted
from plugin.calc.python.formula_edit import PyFormula, is_ascii_letter
@dataclass(frozen=True)
class _Part:
"""One end of an A1 token. ``col``/``row`` are 0-based; None = omitted."""
col: int | None
row: int | None
col_abs: bool
row_abs: bool
@dataclass(frozen=True)
class _Ref:
"""A sheet ref matched in formula text (optional sheet on each end)."""
sheet1: str
sheet2: str
start: _Part
end: _Part | None
def expand_single_formula(formula: str, num_rows: int, num_cols: int) -> list[str]:
"""Expand one formula into a row-major 1-D or 2-D rectangle of formulas.
Raises:
ValueError: a dimension is less than 1.
"""
if num_rows < 1 or num_cols < 1:
raise ValueError("num_rows and num_cols must be >= 1")
# "both" on every 2-D fill: a Python cell at (c>0, r=0) would infer "cols".
# Non-Python formulas ignore axis; 1-D offsets infer rows vs cols.
axis = "both" if num_rows > 1 and num_cols > 1 else None
return [
adjust_a1_formula(formula, c, r, axis=axis)
for r in range(num_rows)
for c in range(num_cols)
]
def _ref_crosses_axis(ref: _Ref, axis: str | None) -> bool:
"""True when *ref* spans the fill axis and must stay verbatim.
``_try_match_ref`` only builds a lone cell (no end), a cell range, a
whole column (row omitted), or a whole row (column omitted).
``None != None`` would miss a whole column on a vertical fill.
"""
if axis == "rows":
return ref.end is not None and (ref.start.row is None or ref.start.row != ref.end.row)
if axis == "cols":
return ref.end is not None and (ref.start.col is None or ref.start.col != ref.end.col)
if axis == "both":
return ref.end is not None
return False
def _map_refs(formula: str, on_ref: Callable[[str, _Ref], str]) -> str:
"""Walk *formula*, skipping strings, quoted names, and structured refs, and map each ref."""
out: list[str] = []
i = 0
n = len(formula)
while i < n:
if formula[i] == '"':
j = skip_quoted(formula, i)
out.append(formula[i:j])
i = j
continue
matched = _try_match_ref(formula, i)
if matched is not None:
end, ref = matched
out.append(on_ref(formula[i:end], ref))
i = end
continue
if formula[i] == "'":
j = skip_quoted(formula, i)
out.append(formula[i:j])
i = j
continue
if formula[i] == "[":
depth = 0
j = i
while j < n:
if formula[j] == "[":
depth += 1
elif formula[j] == "]":
depth -= 1
if depth == 0:
j += 1
break
j += 1
out.append(formula[i:j])
i = j
continue
out.append(formula[i])
i += 1
return "".join(out)
def adjust_a1_formula(formula: str, dcol: int, drow: int, *, axis: str | None = None) -> str:
"""Shift relative A1 refs by *dcol*/*drow*. ``$`` stays put.
Double-quoted strings (Calc ``""`` escape) are left untouched, so
``A1:H10`` inside ``=PY("…")`` is not a sheet ref. Common
``Sheet.A1`` / ``'Sheet'!A1`` prefixes are preserved.
"""
if not formula or (dcol == 0 and drow == 0):
return formula
is_py = PyFormula.open_end(formula, require_equals=True) is not None
if is_py and axis is None:
axis = "both" if dcol and drow else "cols" if dcol else "rows"
def _shift(text: str, ref: _Ref) -> str:
if is_py and _ref_crosses_axis(ref, axis):
return text
return _format_shifted_ref(ref, dcol, drow)
return _map_refs(formula, _shift)
def _relative_rows(formula: str) -> set[int]:
"""0-based rows of every relative-row ref end in *formula*."""
rows: set[int] = set()
def _collect(text: str, ref: _Ref) -> str:
if ref.start.row is not None and not ref.start.row_abs:
rows.add(ref.start.row)
if ref.end is not None and ref.end.row is not None and not ref.end.row_abs:
rows.add(ref.end.row)
return text
_map_refs(formula, _collect)
return rows
def repeated_formula_columns(
values: Sequence[Any], first_row: int, num_rows: int, num_cols: int
) -> list[tuple[int, str]]:
"""Find columns repeating one row-aligned formula across every row.
A model that pastes the same row-2 formula in every row likely meant
fill-down, but rewriting silently can corrupt intentional pinning, so
callers warn instead. Returns ``[(0-based col offset, formula), ...]``.
"""
if num_rows < 2 or num_cols < 1 or len(values) != num_rows * num_cols:
return []
repeated: list[tuple[int, str]] = []
target_rows = {first_row}
for c in range(num_cols):
col = values[c::num_cols]
head = col[0]
if not isinstance(head, str) or not head.startswith("="):
continue
if any(v != head for v in col[1:]):
continue
if _relative_rows(head) != target_rows:
continue
repeated.append((c, head))
return repeated
def _is_digit(ch: str) -> bool:
return "0" <= ch <= "9"
def _skip_ws(s: str, i: int) -> int:
n = len(s)
while i < n and s[i].isspace():
i += 1
return i
def _read_dollar(s: str, i: int) -> tuple[int, bool]:
if i < len(s) and s[i] == "$":
return i + 1, True
return i, False
def _read_col(s: str, i: int) -> tuple[int, int] | None:
start = i
n = len(s)
while i < n and is_ascii_letter(s[i]):
i += 1
if i == start:
return None
letters = s[start:i]
if len(letters) > 3:
return None
col_idx = column_to_index(letters)
if col_idx > MAX_COL:
return None
return i, col_idx
def _read_row(s: str, i: int) -> tuple[int, int] | None:
start = i
n = len(s)
while i < n and _is_digit(s[i]):
i += 1
if i == start:
return None
row_num = int(s[start:i])
if row_num < 1:
return None
return i, row_num - 1
def _try_sheet_prefix(s: str, i: int) -> tuple[str, int] | None:
"""``Sheet.`` / ``'My Sheet'!`` including the separator, or None."""
n = len(s)
if i >= n:
return None
if s[i] == "'":
j = skip_quoted(s, i)
if s[j - 1] != "'":
return None
else:
j = i
if s[j] == "$":
j += 1
start = j
while j < n and s[j] not in ".!'(),;+-*/&=<>^%:" and not s[j].isspace():
j += 1
if j == start:
return None
j = _skip_ws(s, j)
if j >= n or s[j] not in ".!":
return None
j += 1
j = _skip_ws(s, j)
if j >= n or not (s[j] == "$" or is_ascii_letter(s[j]) or _is_digit(s[j])):
return None
return s[i:j], j
def _try_part(s: str, i: int) -> tuple[_Part, int] | None:
"""Cell, column-only, or row-only token starting at *i*."""
j, col_abs = _read_dollar(s, i)
col = _read_col(s, j)
if col is not None:
j, col_idx = col
k, row_abs = _read_dollar(s, j)
row = _read_row(s, k)
if row is not None:
return _Part(col_idx, row[1], col_abs, row_abs), row[0]
return _Part(col_idx, None, col_abs, False), j
row = _read_row(s, j)
if row is not None:
return _Part(None, row[1], False, col_abs), row[0]
return None
def _next_is_open_paren(s: str, i: int) -> bool:
i = _skip_ws(s, i)
return i < len(s) and s[i] == "("
def _try_match_ref(s: str, i: int) -> tuple[int, _Ref] | None:
"""Match a cell / range / whole-col / whole-row (optional sheet) at *i*."""
if i > 0 and (is_ascii_letter(s[i - 1]) or _is_digit(s[i - 1]) or s[i - 1] == "_"):
return None
sheet1 = ""
j = i
prefixed = _try_sheet_prefix(s, i)
if prefixed is not None:
sheet1, j = prefixed
part1 = _try_part(s, j)
if part1 is None:
return None
part, j = part1
if j < len(s) and s[j] == ":":
k = j + 1
sheet2 = ""
prefixed2 = _try_sheet_prefix(s, k)
if prefixed2 is not None:
sheet2, k = prefixed2
part2 = _try_part(s, k)
if part2 is not None:
other, end = part2
if _next_is_open_paren(s, end):
return None
if (part.col is None, part.row is None) == (other.col is None, other.row is None):
return end, _Ref(sheet1, sheet2, part, other)
if part.col is not None and part.row is not None:
if _next_is_open_paren(s, j):
return None
return j, _Ref(sheet1, "", part, None)
return None
def _shift_part_str(part: _Part, dcol: int, drow: int) -> str:
col, row = part.col, part.row
if col is not None and not part.col_abs:
col += dcol
if row is not None and not part.row_abs:
row += drow
if (col is not None and not 0 <= col <= MAX_COL) or (row is not None and not 0 <= row <= MAX_ROW):
return "#REF!"
out = ""
if col is not None:
out += ("$" if part.col_abs else "") + index_to_column(col)
if row is not None:
out += ("$" if part.row_abs else "") + str(row + 1)
return out
def _format_shifted_ref(ref: _Ref, dcol: int, drow: int) -> str:
left_str = _shift_part_str(ref.start, dcol, drow)
if ref.end is None:
return f"{ref.sheet1}{left_str}"
right_str = _shift_part_str(ref.end, dcol, drow)
return f"{ref.sheet1}{left_str}:{ref.sheet2}{right_str}"