forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbridge.py
More file actions
116 lines (92 loc) · 4.24 KB
/
Copy pathbridge.py
File metadata and controls
116 lines (92 loc) · 4.24 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
# 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/>.
"""In-process UNO bridge for Calc.
Wraps a Calc document and provides convenience methods for accessing
sheets, cells, and ranges. Ported from core/calc_bridge.py for the
plugin framework.
"""
import logging
from plugin.calc.address_utils import index_to_column, column_to_index, parse_range_string, parse_address
logger = logging.getLogger("writeragent.calc")
class CalcBridge:
"""Bridge between the plugin layer and the UNO Calc document."""
def __init__(self, doc):
self.doc = doc
def get_active_document(self):
"""Return the wrapped document."""
return self.doc
def get_active_sheet(self):
"""Return the currently active sheet.
Falls back to the first sheet when the controller does not expose
*getActiveSheet* (e.g. headless mode).
Raises:
RuntimeError: Document is not a spreadsheet or no sheet found.
"""
if not hasattr(self.doc, "getSheets"):
raise RuntimeError("Active document is not a spreadsheet.")
controller = self.doc.getCurrentController()
if hasattr(controller, "getActiveSheet"):
sheet = controller.getActiveSheet()
else:
sheets = self.doc.getSheets()
sheet = sheets.getByIndex(0)
if sheet is None:
raise RuntimeError("No active sheet found.")
return sheet
def get_cell(self, sheet, col: int, row: int):
"""Return the cell object at *col*, *row* on *sheet*."""
return sheet.getCellByPosition(col, row)
def get_cell_by_address(self, address: str):
"""Return the cell object for *address*."""
col, row = parse_address(address)
sheet = self.get_active_sheet()
return self.get_cell(sheet, col, row)
def get_cell_range(self, sheet, range_str: str):
"""Return a cell range object from a range string like ``A1:D10``."""
start, end = parse_range_string(range_str)
return sheet.getCellRangeByPosition(start[0], start[1], end[0], end[1])
def resolve_range_or_address(self, range_or_address: str):
"""Resolves a string identifier to a cell or cell range object.
Supports:
- Named Ranges (e.g. "MyRange")
- A1:B2 Range Strings
- A1 Cell Address Strings
"""
range_or_address = range_or_address.strip()
if hasattr(self.doc, "NamedRanges") and self.doc.NamedRanges.hasByName(range_or_address):
named_range = self.doc.NamedRanges.getByName(range_or_address)
return named_range.getReferredCells()
if ":" in range_or_address:
return self.get_cell_range(self.get_active_sheet(), range_or_address)
try:
return self.get_cell_by_address(range_or_address)
except Exception:
return self.get_active_sheet().getCellRangeByName(range_or_address)
@staticmethod
def _index_to_column(index: int) -> str:
return index_to_column(index)
@staticmethod
def _column_to_index(col_str: str) -> int:
return column_to_index(col_str)
@staticmethod
def parse_range_string(range_str: str):
return parse_range_string(range_str)
@staticmethod
def _range_to_str(range_addr):
"""Convert a CellRangeAddress to a string."""
return "%s%d:%s%d" % (index_to_column(range_addr.StartColumn), range_addr.StartRow + 1, index_to_column(range_addr.EndColumn), range_addr.EndRow + 1)