This is the function creating the button:
Function ButtonAdd(strName as String, lngRow as Long, lngCol as Long) as Boolean
dim objBttn as Object: objBttn = ThisComponent.createInstance("com.sun.star.form.component.CommandButton")
dim objShape as Object: objShape = ThisComponent.createInstance("com.sun.star.drawing.ControlShape")
dim strLstnr as String: strLstnr = "com.sun.star.awt.XItemListener"
dim objScript as Object: objScript = new com.sun.star.script.ScriptEventDescriptor
dim objSheet as Object: objSheet = ThisComponent.Sheets.getByName("App")
dim objForm as Object: objForm = objSheet.Drawpage.Forms.getByName("App")
dim objCell as Object: objCell = objSheet.getCellByPosition(lngCol, lngRow)
dim lngCellWidth as Long: objSheet.Columns(lngCol)
dim lngCellHeight as Long: objSheet.Rows(lngRow)
ButtonAdd = False
objBttn.Name = strName
objBttn.Label = "Detail"
objBttn.FocusOnClick = False
objShape.setSize(objCell.Size)
objShape.setPosition(objCell.Position)
objShape.setName(strName)
objShape.SizeProtect = False
objShape.MoveProtect = False
objShape.setControl(objBttn)
objSheet.Drawpage.add(objShape)
objShape.Anchor = objCell 'this should set the anchor to the cell
objScript.ListenerType = "com.sun.star.awt.XActionListener"
objScript.EventMethod = "actionPerformed"
objScript.ScriptType = "StarBasic"
objScript.ScriptCode = "document:Standard.Process.TestExecute"
objForm.registerScriptEvent(objForm.count-1, objScript)
ButtonAdd = True
End Function