Macros/Calc/py018

From The Document Foundation Wiki
< Macros‎ | Calc
Jump to: navigation, search
This page contains changes which are not marked for translation.


Description

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

Python code

 1 # coding: utf-8
 2 from __future__ import unicode_literals
 3 import uno
 4 from com.sun.star.beans import PropertyValue
 5 
 6 def 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)