keyboard-shortcutslibreoffice-calclibreoffice-basic

LibreOffice Calc shortcut different between edit cell and edit sheet modes?


I made a simple macro to increase the value of the current cell by 1 (I need that operation quite often):

Sub activecellplus1

ThisComponent.CurrentSelection.value = ThisComponent.CurrentSelection.value +1

End Sub

and I assigned it to Ctrl+A because a) I don't need selecting the whole sheet and b) I am used to it from Vim.

This works as expected, with one problem: Ctrl+A now no longer works when editing cells (it may or may not increase the currently edited cell by 1, but it won't select its content).

Is there a way to define Ctrl+A as "Select All" for cells, but as "run this macro" for the sheet (i.e., when not editing a cell)? Alternatively, is there a way to modify the above macro to perform "Select all" when editing a cell?

I am not familiar with LibreOffice's macro language; I only did this for the described functionality.


Solution

  • I couldn't find a way to detect if I am currently editing a cell or I'm just positioned on one (i.e., in normal mode). Andrew Pitonyak - OpenOffice.org Macros Explained suggests that this cannot be done.

    Here is an ugly solution, in hopes that someone finds a better one:

    Sub HandleCtrlA
    
        cel = ThisComponent.currentSelection
        wasSingleCell = cel.supportsService("com.sun.star.sheet.SheetCell")
        
        ' Select All
        document = ThisComponent.currentController.frame
        dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
        dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())
                    
        if wasSingleCell Then
            isSingleCell = ThisComponent.currentSelection.supportsService("com.sun.star.sheet.SheetCell")
            If not isSingleCell Then
                ' We are no longer working with just one cell.
                ' This means that we were not in cell edit mode and our Select All has selected the whole sheet.
                ' Therefore, we deselect it and increase the current cell's value by one.
                dispatcher.executeDispatch(document, ".uno:Deselect", "", 0, Array())
                If cel.Type = 1 Then
                    ' The active cell contains a number, but we don't know if were in edit mode or normal mode.
                    cel.value = cel.value + 1
                Else
                    ' Our cell is not a number. Refuse to increment and explain why.
                    MsgBox("The cell does not contain a numerical value.", MB_OK + MB_ICONEXCLAMATION, "Error")
                End If
            End If
        End If
    
    End Sub
    

    This is good enough for what I need, but be careful if you decide to use it: it still messes up "Select All" in the macro editor and possibly elsewhere.