#NEWLINE# def generate_vba_wrapper( module_name , module , f ) : #NEWLINE# #INDENT# vba = VBAWriter ( f ) #NEWLINE# vba . writeln ( 'Attribute VB_Name = "xlwings_udfs"' ) #NEWLINE# vba . writeln ( "'Autogenerated code by xlwings - changes will be lost with next import!" ) #NEWLINE# for svar in map ( ( lambda attr : getattr ( module , attr ) ) , dir ( module ) ) : #NEWLINE# #INDENT# if hasattr ( svar , '__xlfunc__' ) : #NEWLINE# #INDENT# xlfunc = svar . __xlfunc__ #NEWLINE# xlret = xlfunc [ 'ret' ] #NEWLINE# fname = xlfunc [ 'name' ] #NEWLINE# ftype = ( 'Sub' if xlfunc [ 'sub' ] else 'Function' ) #NEWLINE# func_sig = ( ( ( ftype + ' ' ) + fname ) + '(' ) #NEWLINE# first = True #NEWLINE# vararg = '' #NEWLINE# n_args = len ( xlfunc [ 'args' ] ) #NEWLINE# for arg in xlfunc [ 'args' ] : #NEWLINE# #INDENT# if ( not arg [ 'vba' ] ) : #NEWLINE# #INDENT# argname = arg [ 'name' ] #NEWLINE# if ( not first ) : #NEWLINE# #INDENT# func_sig += ', ' #NEWLINE# #UNINDENT# if ( 'optional' in arg ) : #NEWLINE# #INDENT# func_sig += 'Optional ' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if arg [ 'vararg' ] : #NEWLINE# #INDENT# func_sig += 'ParamArray ' #NEWLINE# vararg = argname #NEWLINE# #UNINDENT# #UNINDENT# func_sig += argname #NEWLINE# if arg [ 'vararg' ] : #NEWLINE# #INDENT# func_sig += '()' #NEWLINE# #UNINDENT# first = False #NEWLINE# #UNINDENT# #UNINDENT# func_sig += ')' #NEWLINE# with vba . block ( func_sig ) : #NEWLINE# #INDENT# if ( ftype == 'Function' ) : #NEWLINE# #INDENT# vba . write ( 'If TypeOf Application.Caller Is Range Then On Error GoTo failed\n' ) #NEWLINE# #UNINDENT# if ( vararg != '' ) : #NEWLINE# #INDENT# vba . write ( ( ( ( ( ( ( 'ReDim argsArray(1 to UBound(' + vararg ) + ') - LBound(' ) + vararg ) + ') + ' ) + str ( n_args ) ) + ')\n' ) ) #NEWLINE# #UNINDENT# j = 1 #NEWLINE# for arg in xlfunc [ 'args' ] : #NEWLINE# #INDENT# argname = arg [ 'name' ] #NEWLINE# if arg [ 'vararg' ] : #NEWLINE# #INDENT# vba . write ( ( ( ( ( 'For k = LBound(' + vararg ) + ') To UBound(' ) + vararg ) + ')\n' ) ) #NEWLINE# argname = ( vararg + '(k)' ) #NEWLINE# #UNINDENT# if arg [ 'vararg' ] : #NEWLINE# #INDENT# vba . write ( ( ( ( ( ( ( 'argsArray(' + str ( j ) ) + ' + k - LBound(' ) + vararg ) + ')) = ' ) + argname ) + '\n' ) ) #NEWLINE# vba . write ( 'Next k\n' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( vararg != '' ) : #NEWLINE# #INDENT# vba . write ( ( ( ( ( 'argsArray(' + str ( j ) ) + ') = ' ) + argname ) + '\n' ) ) #NEWLINE# j += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( vararg != '' ) : #NEWLINE# #INDENT# args_vba = 'argsArray' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# args_vba = ( ( 'Array(' + ', ' . join ( ( ( arg [ 'vba' ] or arg [ 'name' ] ) for arg in xlfunc [ 'args' ] ) ) ) + ')' ) #NEWLINE# #UNINDENT# if ( ftype == 'Sub' ) : #NEWLINE# #INDENT# vba . write ( 'Py.CallUDF "{module_name}", "{fname}", {args_vba}, ThisWorkbook\n' , module_name = module_name , fname = fname , args_vba = args_vba ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# vba . write ( '{fname} = Py.CallUDF("{module_name}", "{fname}", {args_vba}, ThisWorkbook)\n' , module_name = module_name , fname = fname , args_vba = args_vba ) #NEWLINE# #UNINDENT# if ( ftype == 'Function' ) : #NEWLINE# #INDENT# vba . write ( ( ( 'Exit ' + ftype ) + '\n' ) ) #NEWLINE# vba . write_label ( 'failed' ) #NEWLINE# vba . write ( ( fname + ' = Err.Description\n' ) ) #NEWLINE# #UNINDENT# #UNINDENT# vba . write ( ( ( 'End ' + ftype ) + '\n' ) ) #NEWLINE# vba . write ( '\n' )