122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
import uno
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
from com.sun.star.awt import XActionListener
|
|
|
|
# Farbwerte (BGR)
|
|
GREEN = 0xC6EFCE
|
|
RED = 0xFFC7CE
|
|
YELLOW = 0xFFEB9C
|
|
|
|
def get_objektbeschreibung_column(sheet):
|
|
"""Findet die Spalte 'Objektbeschreibung'."""
|
|
for row in range(sheet.Rows.Count):
|
|
for col in range(sheet.Columns.Count):
|
|
cell = sheet.getCellByPosition(col, row)
|
|
if cell.String.strip().lower() == "objektbeschreibung":
|
|
return col
|
|
return None
|
|
|
|
def update_cell_color(cell, status):
|
|
"""Färbt die Zelle."""
|
|
if status == "grün":
|
|
cell.CellBackColor = GREEN
|
|
elif status == "gelb":
|
|
cell.CellBackColor = YELLOW
|
|
else:
|
|
cell.CellBackColor = RED
|
|
|
|
def call_mapper(term):
|
|
"""Ruft den lokalen Wrapper auf."""
|
|
wrapper = Path("/home/jarnold/projects/GND-Skript Test/NormVokabular_Mapper_Wrapper.py")
|
|
if not wrapper.exists():
|
|
return {"term": term, "norm_name": "KEIN TREFFER", "norm_id": "", "suggestions": []}
|
|
|
|
result = subprocess.run(
|
|
["python3", str(wrapper), term],
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
try:
|
|
output = json.loads(result.stdout)
|
|
except:
|
|
output = {"term": term, "norm_name": "KEIN TREFFER", "norm_id": "", "suggestions": []}
|
|
return output
|
|
|
|
class SuggestionListener(XActionListener):
|
|
"""Listener für Klick auf Vorschlag-Button."""
|
|
def __init__(self, cell, suggestion, dialog):
|
|
self.cell = cell
|
|
self.suggestion = suggestion
|
|
self.dialog = dialog
|
|
|
|
def actionPerformed(self, event):
|
|
self.cell.String = self.suggestion
|
|
update_cell_color(self.cell, "grün")
|
|
self.dialog.endExecute() # schließt das Dialogfenster
|
|
|
|
def disposing(self, event):
|
|
pass
|
|
|
|
def show_suggestion_dialog(cell, term, suggestions):
|
|
"""Zeigt ein Dialog-Fenster mit klickbaren Vorschlägen."""
|
|
ctx = XSCRIPTCONTEXT.getComponentContext()
|
|
smgr = ctx.getServiceManager()
|
|
toolkit = smgr.createInstance("com.sun.star.awt.Toolkit")
|
|
dialog_model = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
|
|
dialog_model.PositionX = 100
|
|
dialog_model.PositionY = 100
|
|
dialog_model.Width = 200
|
|
dialog_model.Height = 30 + 25*len(suggestions)
|
|
dialog_model.Title = f"Vorschläge für '{term}'"
|
|
|
|
for i, sugg in enumerate(suggestions[:3]):
|
|
btn_model = dialog_model.createInstance("com.sun.star.awt.UnoControlButtonModel")
|
|
btn_model.Name = f"btn_{i}"
|
|
btn_model.Label = sugg
|
|
btn_model.PositionX = 10
|
|
btn_model.PositionY = 10 + i*25
|
|
btn_model.Width = 180
|
|
btn_model.Height = 20
|
|
dialog_model.insertByName(btn_model.Name, btn_model)
|
|
|
|
dialog = smgr.createInstance("com.sun.star.awt.UnoControlDialog")
|
|
dialog.setModel(dialog_model)
|
|
dialog.setVisible(True)
|
|
|
|
for i, sugg in enumerate(suggestions[:3]):
|
|
btn = dialog.getControl(f"btn_{i}")
|
|
listener = SuggestionListener(cell, sugg, dialog)
|
|
btn.addActionListener(listener)
|
|
|
|
toolkit.createDialog(dialog).execute()
|
|
|
|
def mapper_process_column():
|
|
"""Verarbeitet alle Zellen unter 'Objektbeschreibung' in der aktiven Tabelle."""
|
|
doc = XSCRIPTCONTEXT.getDocument()
|
|
sheet = doc.CurrentController.ActiveSheet
|
|
col_index = get_objektbeschreibung_column(sheet)
|
|
if col_index is None:
|
|
return
|
|
|
|
for row in range(sheet.Rows.Count):
|
|
cell = sheet.getCellByPosition(col_index, row)
|
|
if not cell.String.strip():
|
|
continue # leere Zelle ignorieren
|
|
term = cell.String.strip()
|
|
result = call_mapper(term)
|
|
|
|
if result["norm_name"] != "KEIN TREFFER":
|
|
cell.String = result["norm_name"]
|
|
update_cell_color(cell, "grün")
|
|
elif result["suggestions"]:
|
|
update_cell_color(cell, "gelb")
|
|
show_suggestion_dialog(cell, term, result["suggestions"])
|
|
else:
|
|
update_cell_color(cell, "rot")
|
|
show_suggestion_dialog(cell, term, [])
|
|
|
|
# Export
|
|
g_exportedScripts = mapper_process_column,
|