PyXLL The Python Excel Add-In
  • Product
    • Features
    • Get Started
    • Request Demo
    • Download
  • Pricing
  • Resources
    • Documentation
    • Blog
    • Videos
    • FAQ
    • Learn Python
    • Customer Portal
    • About Us
  • Support
    • Documentation
    • Videos
    • FAQ
    • Contact Us
  • Contact Us
Table of Contents
  • PyXLL Documentation
  • Introduction to PyXLL
  • User Guide
  • Video Guides and Tutorials
  • API Reference
    • Worksheet Functions
    • Real Time Data
    • Macro Functions
    • Type Conversion
    • Ribbon Functions
    • Menu Functions
    • Plotting
    • Custom Task Panes
    • ActiveX Controls
    • Cell Formatting
    • Tables
    • Errors and Exceptions
    • Utility Functions
      • schedule_call
      • reload
      • rebind
      • xl_version
      • get_config
      • get_dialog_type
      • cached_object_count
      • get_event_loop
    • Event Handlers
    • Excel C API Functions
  • What’s new in PyXLL 5
  • Changelog
Close

Utility Functions¶

  • schedule_call

  • reload

  • rebind

  • xl_version

  • get_config

  • get_dialog_type

  • cached_object_count

  • get_event_loop

schedule_call¶

schedule_call(func, *args, delay=0, nowait=False, retries=0, retry_delay=0, retry_backoff=1.0, retry_filter=None, disable_calculation=False, disable_screen_updating=False)

Schedule a function to be called after the current Excel calculation cycle has completed.

The function is called in an Excel macro context so it is safe to use xl_app and other COM and macro functions.

This can be used by worksheet functions that need to modify the worksheet where calling back into Excel would fail or cause a deadlock.

From Python 3.7 onwards when called from the PyXLL asyncio event loop and ‘nowait’ is not set this function returns an asyncio.Future. This future can be awaited on to get the result of the call (see warning about awaiting in an async UDF below).

NOTE: In the stubs version (not embedded in PyXLL) the function is always called immediately and will not retry.

Parameters:
  • func – Function or callable object to call in an Excel macro context at some time in the near future.

  • args – Arguments to be passed to the the function.

  • delay – Delay in seconds to wait before calling the function.

  • nowait – Do not return a Future even if called from the asyncio event loop.

  • retries – Integer number of times to retry.

  • retry_delay – Time in seconds to wait between retries.

  • retry_backoff – Multiplier to apply to ‘retry_delay’ after each retry. This can be used to increase the time between each retry by setting ‘retry_backoff’ to > 1.0.

  • retry_filter – Callable that receives the exception value in the case of an error. It should return True if a retry should be attempted or False otherwise.

  • disable_calculation –

    Disable automatic calculations while the callback is being called. This switches the Excel calculation mode to manual and restores it to its previous mode after the call is complete.

    New in PyXLL 5.2

  • disable_screen_updating –

    Disable Excel’s screen updating while the callback is being called. Screen updating is restored to its previous mode after the call is complete.

    New in PyXLL 5.2

Example usage:

from pyxll import xl_func, xl_app, xlfCaller, schedule_call

@xl_func(macro=True)
def set_values(rows, cols, value):
    """Copies 'value' to a range of rows x cols below the calling cell."""

    # Get the address of the calling cell
    caller = xlfCaller()
    address = caller.address

    # The update is done asynchronously so as not to block Excel
    # by updating the worksheet from a worksheet function.
    def update_func():
        xl = xl_app()
        xl_range = xl.Range(address)

        # get the cell below and expand it to rows x cols
        xl_range = xl.Range(range.Resize(2, 1), range.Resize(rows+1, cols))

        # and set the range's value
        xl_range.Value = value

    # Schedule calling the update function
    pyxll.schedule_call(update_func)

    return address

Note

This function doesn’t allow passing keyword arguments to the schedule function. To do that, use functools.partial().

# Will schedule "print("Hello", flush=True)"
schedule_call(functools.partial(print, "Hello", flush=True))

Warning

If called from an async UDF it is important not to await on the result of this call! Doing so is likely to cause a deadlock resulting in the Excel process hanging.

This is because the scheduled call won’t run until the current calculation has completed, so your function will not complete if awaiting for the result.

reload¶

reload()

Causes the PyXLL addin and any modules listed in the config file to be reloaded once the calling function has returned control back to Excel.

If the ‘deep_reload’ configuration option is turned on then any dependencies of the modules listed in the config file will also be reloaded.

The Python interpreter is not restarted.

rebind¶

rebind()

Causes the PyXLL addin to rebuild the bindings between the exposed Python functions and Excel once the calling function has returned control back to Excel.

This can be useful when importing modules or declaring new Python functions dynamically and you want newly imported or created Python functions to be exposed to Excel without reloading.

Example usage:

from pyxll import xl_macro, rebind

@xl_macro
def load_python_modules():
    import another_module_with_pyxll_functions
    rebind()

xl_version¶

xl_version()
Returns:

the version of Excel the addin is running in, as a float.

  • 8.0 => Excel 97

  • 9.0 => Excel 2000

  • 10.0 => Excel 2002

  • 11.0 => Excel 2003

  • 12.0 => Excel 2007

  • 14.0 => Excel 2010

  • 15.0 => Excel 2013

  • 16.0 => Excel 2016

get_config¶

get_config()
Returns:

the PyXLL config as a ConfigParser.SafeConfigParser instance

See also Configuring PyXLL.

get_dialog_type¶

get_dialog_type()
Returns:

the type of the current dialog that initiated the call into the current Python function

xlDialogTypeNone

or xlDialogTypeFunctionWizard

or xlDialogTypeSearchAndReplace

xlDialogTypeNone = 0
xlDialogTypeFunctionWizard = 1
xlDialogTypeSearchAndReplace = 2

cached_object_count¶

cached_object_count()

Returns the current number of cached objects.

When objects are returns from worksheet functions using the object or var type they are stored in an internal object cache and a handle is returned to Excel. Once the object is no longer referenced in Excel the object is removed from the cache automatically.

See Cached Objects.

get_event_loop¶

get_event_loop()

New in PyXLL 4.2

Get the async event loop used by PyXLL for scheduling async tasks.

If called in Excel and the event loop is not already running it is started.

If called outside of Excel then the event loop is returned without starting it.

Returns:

asyncio.AbstractEventLoop

See Asynchronous Functions.

« Errors and Exceptions
Event Handlers »
  • Home
  • Product
  • Features
  • Documentation
  • Download
  • Pricing
  • Support
  • Documentation
  • Videos
  • FAQ
  • Learn Python
  • Contact Us
  • About
  • About Us
  • Legal
  • Blog
© Copyright PyXLL Ltd