Skip to content

Commit

Permalink
added volatile option for udfs
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Jan 22, 2017
1 parent da461ab commit 346e8f5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions xlwings/udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def should_call_in_wizard(**func_kwargs):
return True


def check_volatile(**func_kwargs):
if 'volatile' in func_kwargs:
volatile = func_kwargs.pop('volatile')
if isinstance(volatile, bool):
return volatile
raise Exception('volatile only takes boolean values ("{0}" provided).'.format(volatile))
return True


def xlfunc(f=None, **kwargs):
def inner(f):
if not hasattr(f, "__xlfunc__"):
Expand Down Expand Up @@ -112,6 +121,7 @@ def inner(f):
}
f.__xlfunc__["category"] = get_category(**kwargs)
f.__xlfunc__['call_in_wizard'] = should_call_in_wizard(**kwargs)
f.__xlfunc__['volatile'] = check_volatile(**kwargs)
return f
if f is None:
return inner
Expand Down Expand Up @@ -258,6 +268,7 @@ def generate_vba_wrapper(module_name, module, f):
xlret = xlfunc['ret']
fname = xlfunc['name']
call_in_wizard = xlfunc['call_in_wizard']
volatile = xlfunc['volatile']

ftype = 'Sub' if xlfunc['sub'] else 'Function'

Expand Down Expand Up @@ -287,6 +298,8 @@ def generate_vba_wrapper(module_name, module, f):
if ftype == 'Function':
if not call_in_wizard:
vba.writeln('If (Not Application.CommandBars("Standard").Controls(1).Enabled) Then Exit Function')
if volatile:
vba.writeln('Application.Volatile')
vba.writeln("If TypeOf Application.Caller Is Range Then On Error GoTo failed")

if vararg != '':
Expand Down

0 comments on commit 346e8f5

Please sign in to comment.