<<

PyXLL User Guide, Release 4.4.2

3.7 Context Functions

• Introduction • Adding a Python Function to the • Creating Sub-Menus • Dynamic Menus • References

3.7.1 Introduction

Context menus are the menus that appear in Excel when your right-click on something, most usually a cell in the current workbook. These context menus have become a standard way for users to interact with their spreadsheets and are an efficient way to get to often used functions. With PyXLL you can add your own Python functions to the context menus. The context menu customizations are defined using the same XML file used when customizing the Excel (see Customizing the Ribbon). The XML file is referenced in the config with the ribbon setting. This can be set to a filename relative to the config file, or as an absolute path. The ribbon XML file uses the standard Microsoft CustomUI schema. This is the same schema you would use if you were customizing the ribbon using COM, VBA or VSTO and there are various online resources from Microsoft that document it1. For adding context menus, you must use the 2009 version of the schema or later. Actions referred to in the ribbon XML file are resolved to Python functions. The full path to the function must be included (e.g. “module.function”) and the module must be on the python path so it can be imported. Often it’s useful to include the modules used by the ribbon in the modules list in the config so that when PyXLL is reloaded those modules are also reloaded, but that is not strictly necessary.

3.7.2 Adding a Python Function to the Context Menu

• Create a new ribbon xml file, or add the contextMenus section from below to your existing ribbon xml file.

1 XML Schema Reference

3.7. Context Menu Functions 68 PyXLL User Guide, Release 4.4.2

Note that you must use the 2009 version of the schema in the customUI element, and the contextMenus element must be placed after the ribbon element.