def AddMyListener():
document = XSCRIPTCONTEXT.getDocument()
sheets = document.getSheets()
sheet = sheets.getByName( "Panview" )
panlistener = CreateUnoListener( "MyApp_", "com.sun.star.util.XModifyListener" )
cell = sheet.getCellByPosition("A1")
cell.addModifyListener( panlistener )
def MyApp_Modified( dummy )
# the stuff U want to do!
print "cell has changed!"
import uno
from com.sun.star.util import XModifyListener
class myChange(XModifyListener):
# XModifyListener
def modified(oEvent):
calling = oEvent.Source
# parent-interface XEventListener
def disposing(oEvent):
pass #normally not needed, but should be callable anyway
m = myChange()
cell.addModifyListener(m)
import uno
from com.sun.star.util import XModifyListener
class myChange(XModifyListener):
# XModifyListener
def modified(oEvent):
calling = oEvent.Source
print "something changed"
# parent-interface XEventListener
def disposing(oEvent):
pass #normally not needed, but should be callable anyway
#m = myChange()
#cell.addModifyListener(m)
def main():
document = XSCRIPTCONTEXT.getDocument()
sheets = document.getSheets()
sheet = sheets.getByName( "Panview" )
cell = sheet.getCellRangeByName("F1")
m = myChange()
cell.addModifyListener(m)
import uno, unohelper
from com.sun.star.util import XModifyListener
class myChange(XModifyListener,unohelper.Base):
# XModifyListener
def modified(oEvent):
calling = oEvent.Source
print "something changed"
# parent-interface XEventListener
def disposing(oEvent):
pass #normally not needed, but should be callable anyway
#m = myChange()
#cell.addModifyListener(m)
def main():
document = XSCRIPTCONTEXT.getDocument()
sheets = document.getSheets()
sheet = sheets.getByName( "Panview" )
cell = sheet.getCellRangeByName("F1")
m = myChange()
cell.addModifyListener(m)
# the helper implements all the vanilla UNO-interfaces that belong to every object.
# You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(myFoo, 'com.sun.star.module.XFooListener',)
Edit: Working example: |
import uno, unohelper
from com.sun.star.util import XModifyListener
class myChange(XModifyListener,unohelper.Base):
def __init__(self,):
self.doc = None
def setDocument(self, doc):
self.doc = doc
# XModifyListener
def modified(self,oEvent):
calling = oEvent.Source
cbc = calling.CellBackColor
calling.CellBackColor = (cbc== -1) and 0xFF0000 or -1
self.doc.setModified(False)
# parent-interface XEventListener
def disposing(self,oEvent):
pass #normally not needed, but should be callable anyway
def start():
document = XSCRIPTCONTEXT.getDocument()
sheets = document.getSheets()
# top square of 4 in first sheet
sheet = sheets.getByIndex(0)
cell = sheet.getCellRangeByName("A1:B2")
m = myChange()
m.setDocument(document)
cell.addModifyListener(m)
# the helper implements all the vanilla UNO-interfaces that belong to every object.
# You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
myChange,
'com.sun.star.util.XModifyListener',()
)
g_exportedScripts = start,
Edit: [2] The above listener implementation causes a race condition if you put a volatile function such as NOW() or RAND() into the range. You should handle that situation or try to make the following actually working. |
import uno, unohelper
from com.sun.star.beans import XPropertyChangeListener
class myPropertyChangeListener(XPropertyChangeListener,unohelper.Base):
def __init__(self,):
self.doc = None
def setDocument(self, doc):
self.doc = doc
# XPropertyChangeListener
def propertyChange(self,oEvent):
'''gets a struct c.s.s.beans.PropertyChangeEvent'''
calling = oEvent.Source
# with the following additional elements:
#sName = oEvent.PropertyName
#bFurther = oEvent.Further
#iHandler = oEvent.PropertyHandle
#anyOldVal = oEvent.OldValue
#anyNewVal = oEvent.NewValue
calling.CellBackColor = (cbc== -1) and 0xFF0000 or -1
self.doc.setModified(False)
# parent-interface XEventListener
def disposing(self,oEvent):
pass #normally not needed, but should be callable anyway
def start():
document = XSCRIPTCONTEXT.getDocument()
sheets = document.getSheets()
# first *single cell* in first sheet
sheet = sheets.getByIndex(0)
cell = sheet.getCellRangeByName("A1")
m = myPropertyChangeListener()
m.setDocument(document)
# FormulaLocal is a true property (not a pseudo-property) of a single cell
cell.addPropertyChangeListener('FormulaLocal', m)
# the helper implements all the vanilla UNO-interfaces that belong to every object.
# You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(myPropertyChangeListener, 'com.sun.star.beans.XPropertyChangeListener',())
g_exportedScripts = start,
import uno, unohelper
from com.sun.star.util import XModifyListener
ctx = uno.getComponentContext()
odoc = XSCRIPTCONTEXT.getDocument()
osheets = odoc.getSheets()
class Listener(XModifyListener, unohelper.Base):
def modified(self, oEv):
val = oEv.Source.Value
if val: #if the value of the calling cell is not zero
osheets.copyByName('Sheet1', 'NewSheet', 2)
else:
osheet = osheets.getByName('Sheet1')
ocell = osheet.getCellRangeByName('E1')
ocell.Value = ocell.Value + 1
def disposing(self, oEv):
pass
def Start():
x = Listener()
mri(osheets)
osheet = osheets.getByName('Sheet1')
ocell = osheet.getCellRangeByName('A1')
ocell.addModifyListener(x)
def mri( target):
mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
mri.inspect(target)
FJCC wrote:Having an object inspection tool is extremely helpful for writing macros. I use MRI and I used XRay in the past. These allow you to see the properties and methods of your objects without wading through the documentation. For example, I saw you wanted to copy a sheet, so I started MRI, got the Sheets container of a Calc document and found copyByName among its methods. I had to know enough to look at the Sheets container, so it requires some familiarity with the API, but you can start with the top level of the document and work your way down.
import uno, unohelper
from com.sun.star.util import XModifyListener
ctx = uno.getComponentContext()
odoc = XSCRIPTCONTEXT.getDocument()
osheets = odoc.getSheets()
class myListener(XModifyListener, unohelper.Base):
ioCell = None
oldVal = 0
theSheet = None
vDelta = 0
def __init__(self,val):
#self.doc = None
self.oldVal = val
self.theSheet = osheets.getByName('Sheet1')
self.ioCell = self.theSheet.getCellRangeByName('E1')
#def setDocument(self, doc):
#self.doc = doc
def modified(self, oEv):
newVal = oEv.Source.Value
self.vDelta = newVal - self.oldVal
sheetCnt = osheets.getCount()
if (sheetCnt + self.vDelta) < 2 : #OPTION-A
#the change is illegal, do nothing
self.ioCell.FormulaLocal = "ILLEGAL OPERATION"
elif (self.vDelta < 0): #OPTION-B
for count in range(abs(self.vDelta),0,-1):
osheetName = osheets.getByIndex(sheetCnt-1).Name
osheets.removeByName(osheetName)
sheetCnt = sheetCnt
self.oldVal = newVal
elif (self.vDelta > 0): #OPTION-C
self.ioCell.FormulaLocal = "add-" + str(abs(self.vDelta))
self.oldVal = newVal
else: #OPTION-D
pass
def disposing(self, oEv):
pass
def Start():
# mri(osheets)
osheet = osheets.getByName('Sheet1')
ocell = osheet.getCellRangeByName('A1')
iVal = ocell.Value
monitor = myListener(iVal)
ocell.addModifyListener(monitor)
def mri( target):
mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
mri.inspect(target)
# the helper implements all the vanilla UNO-interfaces that belong to every object.
# You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
myListener,
'com.sun.star.util.XModifyListener',()
)
g_exportedScripts = Start,
import uno, unohelper
from com.sun.star.util import XModifyListener
ctx = uno.getComponentContext()
odoc = XSCRIPTCONTEXT.getDocument()
osheets = odoc.getSheets()
class myListener(XModifyListener, unohelper.Base):
ioCell = None
oldVal = 0
theSheet = None
vDelta = 0
def __init__(self,val):
#self.doc = None
self.oldVal = val
self.theSheet = osheets.getByName('Sheet1')
self.ioCell = self.theSheet.getCellRangeByName('E1')
def modified(self, oEv):
newVal = oEv.Source.Value
self.vDelta = newVal - self.oldVal
sheetCnt = osheets.getCount()
if (sheetCnt + self.vDelta) < 2 : #OPTION-A
#the change is illegal, do nothing
self.ioCell.FormulaLocal = "ILLEGAL OPERATION"
elif (self.vDelta < 0): #OPTION-B
for count in range(abs(self.vDelta),0,-1):
osheetName = osheets.getByIndex(sheetCnt-1).Name
osheets.removeByName(osheetName)
sheetCnt = sheetCnt - 1
self.oldVal = newVal
elif (self.vDelta > 0): #OPTION-C
self.ioCell.String = "add-" + str(self.vDelta)
self.oldVal = newVal
else: #OPTION-D
pass
def disposing(self, oEv):
pass
def Start():
# mri(osheets)
osheet = osheets.getByName('Sheet1')
ocell = osheet.getCellRangeByName('A1')
iVal = ocell.Value
monitor = myListener(iVal)
ocell.addModifyListener(monitor)
def mri( target):
mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
mri.inspect(target)
# the helper implements all the vanilla UNO-interfaces that belong to every object.
# You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
myListener,
'com.sun.star.util.XModifyListener',()
)
g_exportedScripts = Start,
import uno, unohelper
from com.sun.star.util import XModifyListener
ctx = uno.getComponentContext()
odoc = XSCRIPTCONTEXT.getDocument()
osheets = odoc.getSheets()
class myListener(XModifyListener, unohelper.Base):
diff = None
hotSheet = None #remove later
formerVal = None
numOfSheets = None
outputCell = None #remove later
minSheets = 2
def __init__(self,currentVal):
#self.doc = None
self.formerVal = currentVal #TODO: code to check that value is correct
self.numOfSheets = osheets.getCount()
self.hotSheet = osheets.getByName('Sheet1') #remove later
self.outputCell = self.hotSheet.getCellRangeByName('E1') #for feedback-remove later
def modified(self, oEv):
currentVal = oEv.Source.Value
self.diff = currentVal - self.formerVal
if (self.formerVal < self.minSheets) and (currentVal < self.minSheets):
self.outputCell.FormulaLocal = "STILL AT MINIMUM" #pass
elif self.diff > 0:
self.outputCell.FormulaLocal = "ADDED " + str(self.diff)
#for x in range(0, abs(self.diff)):
#osheets.copyByName('Sheet2','sheet' + str(self.numOfSheets+1),\
#self.numOfSheets)
#self.numOfSheets+=1
elif self.diff < 0:
self.outputCell.FormulaLocal = "REMOVED " + str(abs(self.diff))
#for x in range(0, abs(self.diff)):
#lastSheetName = osheets.getByIndex(self.numOfSheets-1).Name
#osheets.removeByName(lastSheetName)
#self.numOfSheets-=1
else: #zero difference
self.outputCell.FormulaLocal = "NOTHING TO DO" #pass
self.formerVal = currentVal
def disposing(self, oEv):
pass
def Start():
mri(osheets)
osheet = osheets.getByName('Sheet1')
ocell = osheet.getCellRangeByName('A1')
startValue = ocell.Value
monitor = myListener(startValue)
ocell.addModifyListener(monitor)
def mri(target):
mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
mri.inspect(target)
# the helper implements all the vanilla UNO-interfaces that belong to every
# object. You may override them. See source code of unohelper module.
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
myListener,
'com.sun.star.util.XModifyListener',()
)
g_exportedScripts = Start,
>>> import uno, unohelper
>>> from com.sun.star.util import XModifyListener # Import succeeds.
>>> from com.sun.star.util import XEventListener
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/…/uno.py", line 425, in _uno_import
raise uno_import_exc
File "/…/uno.py", line 347, in _uno_import
return _builtin_import(name, *optargs, **kwargs)
ImportError: No module named 'com' (or 'com.sun.star.util.XEventListener' is unknown)
def SheetContentChanged(obj):
mri = uno.getComponentContext().ServiceManager.createInstance('mytools.Mri')
mri.inspect(obj)
Return to OpenOffice Basic, Python, BeanShell, JavaScript
Users browsing this forum: No registered users and 4 guests