Search code examples
javaopenoffice-writeruno

How to check when a bookmark placed inside a cell?


Is there any way to check if a bookmark placed inside a table cell? If so, how could I also get the cell's indices (row and column)?

Thought about using something like XTextViewCursor, but I don't know how to get a cell cursor in order to compare cursors positions.

(Non-java solutions also acceptable, I just want to get the idea)


Solution

  • Apparently bookmark anchors work like the TextRange interface. See 7.1.2 Can I check the current selection for a TextTable or Cell in Andrew Pitonyak's macro document.

    The following python example checks if the anchor is located in a table cell. If it is, it gets the cell name, from which we can determine the row and column.

    def is_bookmark_in_cell():
        doc = XSCRIPTCONTEXT.getDocument()
        marks = doc.getBookmarks()
        anchor = marks.getByName("MyMarkInCell").getAnchor()
        cell = anchor.Cell
        if cell:
            cellName = cell.CellName
        else:
            # Bookmark is not in a cell.
    

    In my test, the cell name was B2. For simple tables, B2 simply means the second row and the second column. It may be more complex with tables that have rows spanning multiple columns, or columns spanning multiple rows.