Here is a range selection example based on Range Selection - Apache OpenOffice Wiki. It can be run in a new spreadsheet.
As is often the case with Python-UNO, the code starts a separate thread so that the spreadsheet interface doesn’t lock up. Separate threads do not seem to be necessary for Basic or Java macros, presumably because those interpreters do not block the main LO graphics thread, whereas the Python interpreter does for some reason.
import threading
import time
import uno
import unohelper
from com.sun.star.beans import PropertyValue
from com.sun.star.sheet import XRangeSelectionListener
def getRange(dummy_context=None):
controller = XSCRIPTCONTEXT.getDocument().getCurrentController()
xRngSel = controller
aListener = ExampleRangeListener()
xRngSel.addRangeSelectionListener(aListener)
aArguments = (
createProp("Title", "Please select a range"),
createProp("CloseOnMouseRelease", False)
)
xRngSel.startRangeSelection(aArguments)
t1 = WaiterThread(xRngSel, aListener)
t1.start()
class ExampleRangeListener(XRangeSelectionListener, unohelper.Base):
def __init__(self):
self.aResult = "not yet"
def done(self, aEvent):
self.aResult = aEvent.RangeDescriptor
def aborted(self, dummy_aEvent):
self.aResult = "nothing"
def disposing(self, dummy_aEvent):
pass
class WaiterThread(threading.Thread):
def __init__(self, xRngSel, aListener):
threading.Thread.__init__(self)
self.xRngSel = xRngSel
self.aListener = aListener
def run(self):
for dummy in range(120): # don't wait more than 60 seconds
if self.aListener.aResult != "not yet":
break
time.sleep(0.5)
self.xRngSel.removeRangeSelectionListener(self.aListener)
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.getSheets().getByIndex(0)
cell = sheet.getCellByPosition(0,0)
cell.setString(self.aListener.aResult)
def createProp(name, value):
"""Creates an UNO property."""
prop = PropertyValue()
prop.Name = name
prop.Value = value
return prop