Macros/Calc/py018

From The Document Foundation Wiki
< Macros‎ | Calc
Jump to navigation Jump to search
This page contains changes which are not marked for translation.
Other languages:
English • ‎español • ‎italiano

Description

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 com.sun.star.beans 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('com.sun.star.frame.DispatchHelper', ctx)
    
    propVal = PropertyValue()                 # Default constructor
   
    propVal.Name="ToPoint"
    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.Name="StringName"
    propVal.Value="=sum(B3:B10)"    # It can be any string, here we want to put the sum of B3:B10 into B11
    properties=(propVal,)

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

    propVal.Name="EndCell"
    propVal.Value="$D$11"    # set EndCell of the command AutoFill to D11
    properties=(propVal,)

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