How to Detect Cell Delete event?

Is there an event listener that can detect when a cell is being deleted from a Calc Sheet?

Do you mean the Cell (or row or column) itself, or the Cell content only?
.
.
Theoretical problem: The LO Calc can pass the Object (where the Event is raised) to the macro assigned to the Event. But the object is not exist after deleting the Cell (Row, Column).

1 Like

We can set XModifyListener for each observed cell.
If the cell is deleted, then as @Zizi64 pointed out, the Source property of the event will contain a SheetCell object, which will throw an error when getting the AbsoluteName property.
To find out which cell is deleted, we can add the name of the sheet with the reference to the observed cell and, if the above situation occurs, look for the name with an invalid reference (and delete this name after finding it).

@sokol92 He has (@vib ) lost in his own Documentation

import uno, unohelper
from com.sun.star.util import XModifyListener


class Observe(unohelper.Base, XModifyListener):
    def __init__(self, doc=None, inspector=None):
        self.doc = doc
        self.inspector = inspector
        XModifyListener.__init__(self)

        
    def modified(self, event):
        try:
            self.doc.Sheets[1]["A1"].String = event.Source.AbsoluteName
            print(event.Source.AbsoluteName, flush=True)
            #self.inspector.inspect(event)
        except:
            print("event.Source has been removed")
            
    def disposing(self, event):
        pass

def main():
    doc = XSCRIPTCONTEXT.getDocument()
    cell = doc.Sheets[0]['A10']
    Mod_observer = Observe( doc )
    cell.addModifyListener( Mod_observer )
1 Like

LOL, Yep, that happens.
I am aware of the modify listener. I was not aware however how to use it to detect when cells were removed.


This looks promising. I will give it a try soon.

I would just like to add a note that if a cell is deleted and then restored, Undo, redo, then the event.source.AbsoluteName would reflect that. So Deleted does not necessarly mean deleted in this context.