From The Document Foundation Wiki
< Macros‎ | Calc
Jump to navigation Jump to search
This page is a translated version of the page Macros/Calc/py018 and the translation is 7% complete.
Other languages:
English • ‎español • ‎italiano


Run dispatcher to insert a formula (actually any string) into a certain cell

Python code

# coding: utf-8
from __future__ import unicode_literals
import uno
from import PropertyValue

def calcInsertStringToCell(*args ):    # *args here is only used when this module is launched by a form button
    document = XSCRIPTCONTEXT.getDocument()
    frame = document.getCurrentController().getFrame()
    ctx = XSCRIPTCONTEXT.getComponentContext()
    dispatcher = ctx.ServiceManager.createInstanceWithContext('', ctx)
    propVal = PropertyValue()                 # Default constructor
    propVal.Value="$B$11"   # The cell to insert
    properties=(propVal,)    # The parameter passing into the executeDispatch **must** be a tuple

    dispatcher.executeDispatch(frame, ".uno:GoToCell", "", 0, properties)  # Execute the Dispatch
    propVal.Value="=sum(B3:B10)"    # It can be any string, here we want to put the sum of B3:B10 into B11

    dispatcher.executeDispatch(frame, ".uno:EnterString", "", 0, properties)  # Execute the Dispatch
    # Extra: Auto fill the same content from the current cell (B11) to D11

    propVal.Value="$D$11"    # set EndCell of the command AutoFill to D11

    dispatcher.executeDispatch(frame, ".uno:AutoFill", "", 0, properties)