Macros/Calc/py018

From The Document Foundation Wiki
< Macros‎ | Calc
Jump to navigation Jump to search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:

Description

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

Python code

 1# coding: utf-8
 2from __future__ import unicode_literals
 3import uno
 4from com.sun.star.beans import PropertyValue
 5
 6def calcInsertStringToCell(*args ):    # *args here is only used when this module is launched by a form button
 7    document = XSCRIPTCONTEXT.getDocument()
 8    frame = document.getCurrentController().getFrame()
 9    ctx = XSCRIPTCONTEXT.getComponentContext()
10    dispatcher = ctx.ServiceManager.createInstanceWithContext('com.sun.star.frame.DispatchHelper', ctx)
11    
12    propVal = PropertyValue()                 # Default constructor
13   
14    propVal.Name="ToPoint"
15    propVal.Value="$B$11"   # The cell to insert
16    properties=(propVal,)    # The parameter passing into the executeDispatch **must** be a tuple
17
18    dispatcher.executeDispatch(frame, ".uno:GoToCell", "", 0, properties)  # Execute the Dispatch
19    
20    propVal.Name="StringName"
21    propVal.Value="=sum(B3:B10)"    # It can be any string, here we want to put the sum of B3:B10 into B11
22    properties=(propVal,)
23
24    dispatcher.executeDispatch(frame, ".uno:EnterString", "", 0, properties)  # Execute the Dispatch
25    
26    # Extra: Auto fill the same content from the current cell (B11) to D11
27
28    propVal.Name="EndCell"
29    propVal.Value="$D$11"    # set EndCell of the command AutoFill to D11
30    properties=(propVal,)
31
32    dispatcher.executeDispatch(frame, ".uno:AutoFill", "", 0, properties)