Jump to content

Macros/ScriptForge/ApplyMenuCommand

From The Document Foundation Wiki


How to apply a (menu) command to a document

Authored by Jean-Pierre Ledure.

The loading of the ScriptForge Basic library can be done elsewhere. In Basic, all used variables are declared explicitly. The concerned code is presented inside a Basic Sub or a Python def.

Run that piece of code and consider the result.

The very simple example below enlarges a given column in a (new) sheet. There is no method or property in ScriptForge to make that happen. Because the RunCommand() method exists.

Note the difference of syntaxes between the Basic and Python versions: in BASIC, only positional arguments are allowed, whereas in Python, the use of keyword arguments makes the code more elegant.

ALL THE AVAILABLE COMMANDS WITH THEIR ARGUMENTS ARE ON: Dispatch commands

How to run it in BASIC

Create a new document.

  1. Open the Basic IDE
  2. Select the first available blank module
  3. Copy and paste the Basic code
  4. Run Main()

How to run it in Python

Create a new document.

  1. Run APSO
  2. Create a new module, call it 'Module1'
  3. Copy and paste the Python code below
  4. Save and run the Main() method

Code

REM		How to run a command on a document ?
REM		Minimal required version: LibreOffice 7.6
REM 	Used service(s)	Document, Calc

Sub ResizeColumn()
'	ScriptForge has no function to modify a column's width.
'	Can I solve this ?

Dim ui As Object
Dim calc As Object

	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

Const size = 8000	'	in 1/100 mm
Const column = 3	'	column C

	Set ui = CreateScriptService("UI")
	Set calc = ui.CreateDocument("Calc")

	calc.RunCommand(".uno:ColumnWidth", "ColumnWidth", size, _
										"Column", column)
	'	The command without arguments will open the usual dialog
	'	calc.RunCommand(".uno:ColumnWidth")
	
	'	Housekeeping
	calc.Dispose()

End Sub
# coding: utf-8
from __future__ import unicode_literals

from scriptforge import CreateScriptService

###		How to run a command on a document ?
###		Minimal required version: LibreOffice 7.6
### 	Used service(s)	Document, Calc

def resizecolumn():
	#	ScriptForge has no function to modify a column's width.
	#	Can I solve this ?

	size = 8000	#	in 1/100 mm
	column = 3	#	column C
	
	ui = CreateScriptService('UI')
	calc = ui.CreateDocument('Calc')
	
	calc.RunCommand('.uno:ColumnWidth', ColumnWidth = size, Column = column)
	
	#	The command without arguments will open the usual dialog
	#	calc.RunCommand('.uno:ColumnWidth')
	
	# Housekeeping
	calc = calc.Dispose()


g_exportedScripts = (resizecolumn,)

See also