Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello FAL, To change the value in a control in a Base Form using Python, you could write:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    value1 = get_FormControl_Value( oForm, "Your_Control_1" )
    # Do your computations here ...
    set_FormControl_TextValue( oForm, "Your_Control_2" , value1 )

def get_FormControl_Value( oForm, strControlname ):
    # Gets the current Value from the specified control's model.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            return oCtrlModel.getCurrentValue()

def set_FormControl_TextValue( oForm, strControlname, aTextValue ):
    # Sets the Text of the specified control.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            oCtrl = getParentDocument( oForm ).CurrentController.getControl( oCtrlModel )
            if oCtrl.isEnabled and oCtrl.isEditable:
                oCtrl.Text = aTextValue

def  getParentDocument( oForm ):
    # Returns the OfficeDocument object of the specified Form, SubForm or Control.
    if not oForm.supportsService("com.sun.star.form.FormComponent"): return oForm
    x = oForm
    while not x.supportsService("com.sun.star.document.OfficeDocument"):
        x = x.getParent()
    return x

Hello FAL, To change the value in a control in a Base Form using Python, you could write:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    value1 = get_FormControl_Value( oForm, "Your_Control_1" )
    # Do your computations here ...
    set_FormControl_TextValue( oForm, "Your_Control_2" , value1 )

def get_FormControl_Value( oForm, strControlname ):
    # Gets the current Value from the specified control's model.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            return oCtrlModel.getCurrentValue()

def set_FormControl_TextValue( oForm, strControlname, aTextValue ):
    # Sets the Text of the specified control.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            oCtrl = getParentDocument( oForm ).CurrentController.getControl( oCtrlModel )
            if oCtrl.isEnabled and oCtrl.isEditable:
                oCtrl.Text = aTextValue

def  getParentDocument( oForm ):
    # Returns the OfficeDocument object of the specified Form, SubForm or Control.
    if not oForm.supportsService("com.sun.star.form.FormComponent"): return oForm
    x = oForm
    while not x.supportsService("com.sun.star.document.OfficeDocument"):
        x = x.getParent()
    return x

EDIT: to change the table values directly:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    table_field1 = oForm.findColumn( "Field1" )
    table_field2 = oForm.findColumn( "Field2" )
    float_value1 = oForm.getFloat( table_field1 )
    # Do your computations here ...
    oForm.updateFloat( table_Field2,  float_value1 )
    oForm.updateRow()

Hello FAL, To change the value in a control in a Base Form using Python, you could write:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    value1 = get_FormControl_Value( oForm, "Your_Control_1" )
    # Do your computations here ...
    set_FormControl_TextValue( oForm, "Your_Control_2" , value1 )

def get_FormControl_Value( oForm, strControlname ):
    # Gets the current Value from the specified control's model.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            return oCtrlModel.getCurrentValue()

def set_FormControl_TextValue( oForm, strControlname, aTextValue ):
    # Sets the Text of the specified control.
    if oForm:
        oCtrlModel = oForm.getByName( strControlname  )
        if oCtrlModel:
            oCtrl = getParentDocument( oForm ).CurrentController.getControl( oCtrlModel )
            if oCtrl.isEnabled and oCtrl.isEditable:
                oCtrl.Text = aTextValue

def  getParentDocument( oForm ):
    # Returns the OfficeDocument object of the specified Form, SubForm or Control.
    if not oForm.supportsService("com.sun.star.form.FormComponent"): return oForm
    x = oForm
    while not x.supportsService("com.sun.star.document.OfficeDocument"):
        x = x.getParent()
    return x

EDIT: to change the table values directly:

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    table_field1 = oForm.findColumn( "Field1" )
    table_field2 = oForm.findColumn( "Field2" )
    float_value1 = oForm.getFloat( table_field1 )
    # Do your computations here ...
    oForm.updateFloat( table_Field2,  float_value1 )
    oForm.updateRow()

EDIT2: with format

desktop = XSCRIPTCONTEXT.getDesktop()
thisComponent = desktop.getCurrentComponent()
oForm = thisComponent.getDrawPage().getForms().getByIndex( 0 )  # MainForm
if oForm:
    table_field1 = oForm.findColumn( "Field1" )     # Get the table field index
    float_value1 = oForm.getFloat( table_field1 )   # Get the table field value as Float
    float_value1 *= 2                       # Do your computations here ...
    oCtrlModel = oForm.getByName( "txtField3"  )    # The name of your unbound control here
    if oCtrlModel:
        oCtrlModel.setString( "{0:.4f}".format( float_value1 ) )    # show precision 4 decimals