Creating a menu using MenuApp
from ooodev.calc import CalcDoc
from ooodev.utils.kind.menu_lookup_kind import MenuLookupKind
doc = CalcDoc.create_doc(loader=loader, visible=True)
menu = doc.menu[MenuLookupKind.TOOLS] # or .menu[".uno:ToolsMenu"]
itm = menu.items[".uno:AutoComplete"] # or .items[6]
menu_name = ".custom:my.custom_menu"
new_menu = {
"Label": "My Menu",
"CommandURL": menu_name,
"Submenu": [
{
"Label": "Execute macro...",
"CommandURL": "RunMacro",
"ShortCut": "Shift+Ctrl+Alt+E",
},
{
"Label": "My macro",
"CommandURL": {"library": "test", "name": "hello"},
"ShortCut": {"key": "Shift+Ctrl+Alt+F", "save": True},
},
],
}
if not menu_name in menu:
# only add the menu if it does not already exist
menu.insert(new_menu, after=itm.command, save=True)
In this case doc.menu is MenuApp for Calc menus.
Config
Menu Keys
Menu keys are case sensitive
Label
Label this is the label for the menu and is required.
To insert a separator set the Label value to -
{
"Label": "-",
}
CommandURL
CommandURL Optional for menus separators, Otherwise required.
CommandURL is the command to be executed when the menu item is clicked, such as RunMacro.
This can be a string or a dictionary.
String
As a string CommandURL is as follows.
"CommandURL": ".custom:just.a.command",
The menu command would be just.a.command.
If string does not start with .custom: or .uno: then .uno: is automatically prepended when command is being dispatched.
In the following code CommandURL value is set to RunMacro.
{
"Label": "Execute macro...",
"CommandURL": "RunMacro",
"ShortCut": "Shift+Ctrl+Alt+E",
},
When the menu is build the CommandURL value is actually .uno:RunMacro.
If you need the actual CommandURL to be RunMacro then when in the actual menu then:
{
"Label": "Execute macro...",
"CommandURL": ".custom:RunMacro",
"ShortCut": "Shift+Ctrl+Alt+E",
},
Dictionary
A dictionary value indicates that a macro URL should be constructed to run a macro when the menu item is clicked.
{
"Label": "My macro",
"CommandURL": {"library": "test", "name": "hello"},
},
As a Dictionary CommandURL can accept to following values.
library(str, optional): Macro Library. Defaults toStandard.name(str): Macro Name.language(str, optional): LanguageBasicorPython. Defaults toBasic.location(str, optional): Locationuserorapplication. Defaults to “user”.module(str, optional): Module portion. Only Applies iflanguageis notBasicorPython. Defaults to “.”.
These are the exact same parameters that are accepted by ooodev.macro.script.MacroScript.get_url_script() method.
Style
This is the style of the menu.
from ooodev.utils.kind.item_style_kind import ItemStyleKind
from ooodev.utils.kind.menu_lookup_kind import MenuLookupKind
menu = doc.menu[MenuLookupKind.TOOLS]
menu_name = ".custom:my.custom_menu"
new_menu = {
"Label": "My Menu",
"CommandURL": menu_name,
"Submenu": [
{
"Label": "A",
"CommandURL": ".uno:WarningCellStyles",
"Style": ItemStyleKind.RADIO_CHECK,
},
{
"Label": "B",
"CommandURL": ".uno:FootnoteCellStyles",
"Style": ItemStyleKind.RADIO_CHECK,
},
{
"Label": "C",
"CommandURL": ".uno:NoteCellStyles",
"Style": ItemStyleKind.RADIO_CHECK,
},
],
}
See API ItemStyle Constant Group.
There is a ItemStyleKind for accessing the constants in an enum.