How can I dynamically link spin buttons to an adjacent cell?

I am trying to create a sheet in LibreOffice Calc where Column C will be populated by spin buttons linked to Column B. The spin button in C1 links to B1, the spin button in C2 links to B2, and spin button in C3 links to B3, and so on. I want to be able to quickly add new rows with their own spin buttons, so I want to avoid manually clicking through Design Mode > Right Click on Spin Button > Control Properties > Left Click on Data Tab > Enter Linked Cell.

I assumed I could just copy and paste the spin buttons and they would dynamically adjust their values, but when I do this, the spin button retains the Linked Cell of whichever spin button the button was copied from.

Is there any way to do this? If not, are there alternative form controls or macros I could use to get the result I’m looking for?

spin-buttons

Yes, I think you need a macro. If tomorrow anybody not response you, I will help with this macro.

It is a built-in feature of database forms.
SimpleInventory_embedded.odb (55.3 KB)
Open a form, focus the “Quantity”, use the buttons or the mouse wheel to change the integer value. Minimum is 1.

1 Like

Hallo
see also similar topic in german

please double-check the Buttons are anchored to cell (⇒rightclick⇒Anchor⇒[x]To Cell (resize with Cell))

from com.sun.star.beans import NamedValue as NV

def bind_relative_to_anchor(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.CurrentController.ActiveSheet
    draw_page = sheet.DrawPage
    for button in draw_page:
        control = button.Control
        if control.supportsService('com.sun.star.form.component.SpinButton'):
            cell_address = button.Anchor.CellAddress
            cell_address.Column -= 1  # the column left of Anchor
            nv = NV( Name="BoundCell", Value=cell_address )    
            bind = doc.createInstance("com.sun.star.table.CellValueBinding")
            bind.initialize((nv,))
            control.setValueBinding(bind)