Repository navigation
Expand file tree
/
Copy pathcalc_utils.py
More file actions
253 lines (206 loc) · 10.1 KB
/
Copy pathcalc_utils.py
File metadata and controls
253 lines (206 loc) · 10.1 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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2024 John Balis
# Copyright (c) 2026 KeithCu (modifications and relicensing)
# Copyright (c) 2026 LibreCalc AI Assistant (Calc integration features, originally MIT)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""General utilities for Calc tools."""
from __future__ import annotations
import logging
from typing import Any
import uno
from com.sun.star.awt import Point
from com.sun.star.table.CellContentType import EMPTY, FORMULA, TEXT, VALUE
from plugin.calc.address_utils import parse_address, split_sheet_prefix
from plugin.calc.bridge import CalcBridge
from plugin.calc.datetime_wire import format_category_from_type
from plugin.framework.errors import ToolExecutionError, is_disposal, uno_read
log = logging.getLogger(__name__)
CELL_FLAG_DATETIME = 2 # com.sun.star.sheet.CellFlags.DATETIME
CELL_FLAG_FORMULA = 16 # com.sun.star.sheet.CellFlags.FORMULA
CELL_CONTENT_FLAGS = 23 # CellFlags: VALUE=1, DATETIME=2, STRING=4, FORMULA=16
# com.sun.star.sheet.FormulaResult.VALUE (offapi/com/sun/star/sheet/FormulaResult.idl).
FORMULA_RESULT_VALUE = 1
def range_args(raw: Any, *, exactly_one: bool = False) -> list[Any]:
"""Normalize tool ``range`` kwargs to a list of range strings."""
if exactly_one:
if not isinstance(raw, list) or len(raw) != 1 or not isinstance(raw[0], str) or not raw[0].strip():
raise ToolExecutionError("Pass exactly one range.", code="INVALID_PARAM")
return [raw[0].strip()]
if not raw:
return []
if isinstance(raw, str):
return [raw]
return list(raw)
def query_interface(obj: Any, typename: str) -> Any:
"""PyUNO requires ``uno.getTypeByName`` for ``queryInterface``; imported IDL classes fail."""
if obj is None or not hasattr(obj, "queryInterface"):
return None
return obj.queryInterface(uno.getTypeByName(typename))
def resolve_sheet_and_cell(doc: Any, address: str) -> tuple[Any, int, int] | None:
"""Resolve *address* (e.g. 'A1' or 'Sheet1.B2') to ``(sheet, col, row)`` for an open Calc document.
This is the "or None" variant of ``CalcBridge.resolve_cell`` for program-built
A1 addresses (cell links, dependency chain) and does not take a named range.
"""
text = address.strip()
if not text or doc is None:
return None
sheet_name, cell_part = split_sheet_prefix(text)
try:
col, row = parse_address(cell_part)
except ValueError:
return None
sheet = uno_read(lambda: CalcBridge(doc).sheet_or_active(sheet_name), None)
if sheet is None:
return None
return sheet, col, row
def get_cell_geometry(sheet: Any, cell: Any) -> tuple[Any, Any]:
"""Return (Position, Size) for *cell*, collapsing merged areas to get correct coordinates.
Standard ``cell.Position`` / ``cell.Size`` return the top-left sub-cell geometry
when cells are merged, which is wrong for overlay placement. This helper detects
the merge and asks for the full merged area's geometry instead.
"""
if getattr(cell, "IsMerged", False):
cell = sheet.createCursorByRange(cell)
cell.collapseToMergedArea()
return cell.Position, cell.Size
def sheet_is_rtl(sheet: Any) -> bool:
"""True when sheet writing direction is right-to-left (TableLayout == 1)."""
return sheet.TableLayout == 1
def selection_top_left_cell(controller: Any, sheet: Any) -> Any | None:
"""Return the top-left cell of *controller*'s selection on *sheet*, or None.
Shape selections (``SvxShapeCollection``) and multi-range selections
(``ScCellRangesObj``) have no ``getRangeAddress`` and return None. A closed
document raises.
"""
def _read() -> Any:
addr = controller.getSelection().getRangeAddress()
return sheet.getCellByPosition(addr.StartColumn, addr.StartRow)
return uno_read(_read, None)
def anchor_shape_at_cell(sheet: Any, shape: Any, cell: Any) -> None:
"""Anchor *shape* to *cell* with Excel's "Move but don't size with cells" semantics.
Sets ``Anchor`` to *cell* and ``ResizeWithCell`` to False, which LibreOffice
exports as OOXML ``twoCellAnchor editAs="oneCell"`` (``xcl97rec.cxx``
``GetEditAs``). Uses ``get_cell_geometry`` so merged blocks resolve to the
merged area's coordinates, and mirrors horizontally on RTL sheets
(``ScDocument::GetMMRect`` / ``shapeuno.cxx`` "Anchor") so the shape's right
edge aligns with the cell's right edge.
"""
pos, size = get_cell_geometry(sheet, cell)
shape.setPropertyValue("Anchor", cell)
shape.setPropertyValue("ResizeWithCell", False)
x = pos.X
if sheet_is_rtl(sheet):
x += size.Width - shape.getSize().Width
shape.setPosition(Point(x, pos.Y))
def formula_cell_result(cell: Any) -> Any:
"""Return a formula cell's result, keeping numbers as floats.
Non-zero ``getValue()`` is always numeric. A zero is a number only when
``FormulaResultType2`` is ``FormulaResult.VALUE``; text formulas keep
``getString()``.
"""
numeric = cell.getValue()
if isinstance(numeric, bool) or not isinstance(numeric, (int, float)):
return cell.getString()
as_float = float(numeric)
if as_float != 0.0 or cell.FormulaResultType2 == FORMULA_RESULT_VALUE:
return as_float
return cell.getString()
def cell_value_and_type(cell: Any, *, empty_as_none: bool = True, formula_error_as_string: bool = True) -> tuple[Any, str]:
kind = cell.getType()
if kind == EMPTY:
val = None if empty_as_none else cell.getString()
return val, "empty"
if kind == VALUE:
return cell.getValue(), "value"
if kind == TEXT:
return cell.getString(), "text"
if kind == FORMULA:
if formula_error_as_string and cell.getError() != 0:
val = cell.getString()
else:
val = formula_cell_result(cell)
return val, "formula"
return cell.getString(), "unknown"
def classify_array_cell(raw_value: Any, raw_formula: str, is_formula: bool) -> tuple[Any, str, str | None]:
"""(value, type, formula) of one cell from its getDataArray / getFormulaArray entries.
is_formula must come from the cell type (getType, or a CellFlags.FORMULA query):
the formula text cannot tell a formula from a text cell typed as '=abc', and
array-formula cells read '{=...}'. A formula whose result is an error reads None,
because getDataArray gives void for it (rangeseq.cxx ScRangeToSequence::FillMixedArray);
detect_errors reports its token.
"""
if is_formula:
return raw_value, "formula", raw_formula
if isinstance(raw_value, float):
return raw_value, "value", None
if isinstance(raw_value, str) and raw_value:
return raw_value, "text", None
return None, "empty", None
def resolve_document_locale(doc: Any) -> Any:
"""Return document CharLocale, or en-US when Language is empty or unreadable."""
try:
locale = doc.getPropertyValue("CharLocale")
if locale.Language:
return locale
except Exception as e:
if is_disposal(e):
raise
log.debug("CharLocale unavailable; using en-US fallback", exc_info=True)
return uno.createUnoStruct("com.sun.star.lang.Locale", Language="en", Country="US", Variant="")
def elapsed_format_key(formats: Any, locale: Any) -> int:
"""Built-in ``[HH]:MM:SS`` key (formatindex 43), with queryKey/addNew fallback."""
try:
return int(formats.getFormatIndex(43, locale))
except Exception as e:
if is_disposal(e):
raise
log.debug("getFormatIndex(43) failed; falling back to queryKey", exc_info=True)
key = formats.queryKey("[HH]:MM:SS", locale, False)
if key == -1:
key = formats.addNew("[HH]:MM:SS", locale)
return int(key)
def standard_temporal_format_keys(formats: Any, locale: Any) -> dict[str, int]:
"""Standard date, time, datetime, and duration number-format keys for ``locale``.
These are the keys a ``=PY()`` spill neighbour gets when its destination has no
date/time format; the chat write path does not use them because it detects a key
per value and inherits column templates.
"""
return {
"date": int(formats.getStandardFormat(2, locale)),
"time": int(formats.getStandardFormat(4, locale)),
"datetime": int(formats.getStandardFormat(6, locale)),
"duration": elapsed_format_key(formats, locale),
}
class NumberFormatCategories:
"""The date/time category and FormatString of each number-format key, read once per key.
One read or write meets the same few NumberFormat keys on many cells; this
keeps one getByKey per key for the whole operation. A read that raises is
not remembered, so each caller's own guard decides what a failure means
(the write path treats it as "no category"; the spill writer and the
single-cell read let it propagate).
"""
def __init__(self, formats: Any) -> None:
self._formats: Any = formats # the document's XNumberFormats (doc.getNumberFormats())
self._seen: dict[int, tuple[str | None, str | None]] = {}
def lookup(self, key: int) -> tuple[str | None, str | None]:
"""(format_category_from_type(Type), FormatString as str or None) for *key*."""
if key not in self._seen:
props = self._formats.getByKey(key)
code = props.getPropertyValue("FormatString")
self._seen[key] = (format_category_from_type(props.getPropertyValue("Type")), code)
return self._seen[key]
def category(self, key: int) -> str | None:
"""Just the date / time / datetime category of *key*, or None."""
return self.lookup(key)[0]