forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathformulas.py
More file actions
249 lines (215 loc) · 10.2 KB
/
Copy pathformulas.py
File metadata and controls
249 lines (215 loc) · 10.2 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
# 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/>.
"""Calc formula integration tools.
Provides built-in Calc function discovery and arbitrary formula pre-evaluation tools.
> [!NOTE]
> **EvaluateFormula Sheet-Copy Pattern**:
> Unlike standard empty-worksheet insertion which fails to resolve relative or unqualified cell
> references (e.g. `=A1+B1` evaluates to 0 on an empty sheet), `EvaluateFormula` uses a robust,
> side-effect-free Sheet-Copy Pattern. It duplicates the active sheet to a temporary hidden sheet,
> writes the formula at the specified `cell` coordinate context (resolving relative cell dependencies
> against the duplicated live sheet values perfectly), and cleanly deletes the copied sheet in a
> `finally` block before returning.
"""
from plugin.framework.constants import now_aware
import logging
from typing import Any, cast
from plugin.framework.errors import ToolExecutionError
from plugin.framework.tool import ToolBase
from plugin.calc.base import ToolCalcErrorBase
try:
from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA
UNO_AVAILABLE = True
except ImportError:
from typing import cast
EMPTY, VALUE, TEXT, FORMULA = cast("Any", 0), cast("Any", 1), cast("Any", 2), cast("Any", 3)
UNO_AVAILABLE = False
logger = logging.getLogger("writeragent.calc")
class ListCalcFunctions(ToolBase):
"""Retrieve available spreadsheet functions inside LibreOffice Calc."""
name = "list_calc_functions"
description = (
"Lists available Calc spreadsheet functions. "
"Use the 'filter' parameter to perform a case-insensitive search for a partial substring "
"anywhere in function names or descriptions to avoid context window bloat."
)
parameters = {
"type": "object",
"properties": {
"filter": {
"type": "string",
"description": "Optional substring to filter functions by (case-insensitive search anywhere in the name or description)."
}
},
"required": []
}
uno_services = ["com.sun.star.sheet.SpreadsheetDocument"]
tier = "core"
is_mutation = False
def execute(self, ctx, **kwargs):
filter_str = kwargs.get("filter", "").strip().upper()
uno_ctx = ctx.ctx
if not uno_ctx:
from plugin.framework.uno_context import get_ctx
uno_ctx = get_ctx()
if not uno_ctx:
raise ToolExecutionError("UNO context not available.")
try:
smgr = cast("Any", uno_ctx).getServiceManager()
func_descr_service = smgr.createInstanceWithContext(
"com.sun.star.sheet.FunctionDescriptions", uno_ctx
)
if not func_descr_service:
raise ToolExecutionError("FunctionDescriptions service not available.")
matched_functions = []
for i in range(func_descr_service.getCount()):
props = func_descr_service.getByIndex(i)
func_data = {prop.Name: prop.Value for prop in props}
name = func_data.get("Name", "")
desc = func_data.get("Description", "")
if filter_str and (filter_str not in name.upper() and filter_str not in desc.upper()):
continue
arguments = func_data.get("Arguments") or ()
arg_list = []
for a in arguments:
arg_list.append({
"name": getattr(a, "Name", ""),
"description": getattr(a, "Description", ""),
"optional": getattr(a, "IsOptional", False)
})
matched_functions.append({
"name": name,
"description": func_data.get("Description", ""),
"category_id": func_data.get("Category", 0),
"arguments": arg_list,
})
logger.info("Found %d matching Calc functions for filter '%s'", len(matched_functions), filter_str)
return {"status": "ok", "functions": matched_functions}
except Exception as e:
logger.error("Error listing Calc functions: %s", str(e))
raise ToolExecutionError(f"Error listing Calc functions: {str(e)}") from e
class EvaluateFormula(ToolCalcErrorBase):
"""Pre-evaluate a spreadsheet formula on a temporary worksheet copy without side effects."""
name = "evaluate_formula"
description = "Evaluates a Calc formula on a temporary duplicate sheet and returns the result or error, without modifying the active sheets."
parameters = {
"type": "object",
"properties": {
"formula": {
"type": "string",
"description": "The formula to evaluate, e.g. '=SUM(A1:B2)' or '=A1*1.1'."
},
"cell": {
"type": "string",
"description": "Optional cell coordinate/address context to evaluate relative references from, e.g. 'C5' (defaults to 'A1')."
}
},
"required": ["formula"]
}
uno_services = ["com.sun.star.sheet.SpreadsheetDocument"]
tier = "specialized"
is_mutation = False
def execute(self, ctx, **kwargs):
formula_string = kwargs.get("formula", "").strip()
cell_address = kwargs.get("cell", "A1").strip()
if not formula_string:
return self._tool_error("formula is required")
if not formula_string.startswith("="):
formula_string = "=" + formula_string
doc = ctx.doc
if not doc:
raise ToolExecutionError("Document model not available.")
try:
sheets = doc.getSheets()
except Exception as e:
raise ToolExecutionError(f"Failed to get sheets: {str(e)}") from e
# Resolve the active sheet to copy from
try:
active_sheet = doc.getCurrentController().getActiveSheet()
active_name = active_sheet.getName()
except Exception as e:
raise ToolExecutionError(f"Failed to get active sheet: {str(e)}") from e
temp_sheet_name = f"__wa_eval_copy_{int(now_aware().timestamp())}_0__"
# Ensure name uniqueness
counter = 0
while sheets.hasByName(temp_sheet_name):
counter += 1
temp_sheet_name = f"__wa_eval_copy_{int(now_aware().timestamp())}_{counter}__"
try:
# Copy active sheet with all its data
sheets.copyByName(active_name, temp_sheet_name, sheets.getCount())
sheet = sheets.getByName(temp_sheet_name)
# Retrieve the cell by coordinates context
try:
cell_range = sheet.getCellRangeByName(cell_address)
cell = cell_range.getCellByPosition(0, 0)
except Exception as e:
return self._tool_error(f"Invalid cell context address '{cell_address}': {str(e)}")
cell.setFormula(formula_string)
error_code = cell.Error
if error_code != 0:
error_msg = f"Formula evaluation error: code {error_code}"
if error_code in (503, 532):
error_msg = "Formula evaluation error: #DIV/0! (Division by zero, code 532)"
elif error_code == 508:
error_msg = "Formula evaluation error: Pair missing bracket (code 508)"
elif error_code == 509:
error_msg = "Formula evaluation error: Operator missing (code 509)"
elif error_code == 510:
error_msg = "Formula evaluation error: Variable missing (code 510)"
elif error_code == 511:
error_msg = "Formula evaluation error: Parameter missing (code 511)"
elif error_code == 524:
error_msg = "Formula evaluation error: #REF! (Invalid reference, code 524)"
elif error_code == 525:
error_msg = "Formula evaluation error: #NAME? (Invalid name, code 525)"
return {"status": "error", "error_code": error_code, "message": error_msg}
result_type = cell.getType()
if result_type == VALUE:
result = cell.getValue()
elif result_type == TEXT:
result = cell.getString()
elif result_type == FORMULA:
result = cell.getValue() if cell.getValue() != 0 else cell.getString()
else:
result = cell.getString()
if result_type == EMPTY:
result_type_str = "empty"
elif result_type == VALUE:
result_type_str = "value"
elif result_type == TEXT:
result_type_str = "text"
elif result_type == FORMULA:
result_type_str = "formula"
else:
result_type_str = "unknown"
return {
"status": "ok",
"formula": formula_string,
"result": result,
"result_type": result_type_str
}
except Exception as e:
logger.error("Formula evaluation failed: %s", str(e))
raise ToolExecutionError(f"Formula evaluation failed: {str(e)}") from e
finally:
try:
if sheets.hasByName(temp_sheet_name):
sheets.removeByName(temp_sheet_name)
except Exception as cleanup_err:
logger.error("Failed to cleanup evaluation sheet: %s", cleanup_err)