Jump to content

Custom Worksheet Function with *Args


Recommended Posts

I would like to create a custom worksheet function to return a value from a range of worksheet cells - Like vlookup() in MS Excel. For now though I will settle for returning the th value in [*args]list

To my knowledge the closest I can get to that is the following

=RUNSCRIPT(13, 'lookup.py', 2, 1, 2, 3, 4, 5)

Where:

-------------

> 13 - Is the location of 'lookup.py' in this case

AppData\Roaming\Nemetschek\Vectorworks\2016\Libraries

> 'lookup.py' is the name of my script

> 2, 1, 2, 3, 4, 5 are the arguments i'm trying to pass in

lookup.py: (current)

-------------

def lookup(i, *args):

>>>return args[i-1]

"""

Do I need to call lookup(i,*args) here as well? as the function

has only been defined and not run above

"""

Or should I change it more to something like this:

--------------

def lookup(f_i, *f_args):

>>>for a in f_args:

>>>>>>args.append(a)

>>>return args[i-1]

i = 0

args = []

lookup(i, *args)

Please Help. How do I make this work?

Edited by CarlJdP
Link to comment

Do you want get the output in your OS Terminal or somewhere out of Vectorworks?

That is to high for me, but I am very interested on this possibilities too.

As I suppose, that the args method is not loaded in an embedded python. So maybe we have to somehow initialize this first.

But as I told, this could be a very dilettantish comment.

Edited by DomC
Link to comment

Hi

I think, we have to specify the args in the worksheet scripts. Like this

arg1=vs.WSScript_GetPrmReal(0)

arg2=vs.WSScript_GetPrmReal(1)

I tried to get all args with a while loop

args=[];i=0

while vs.WSScript_GetPrmReal(i) !=None: #Endless loop, attension!

args.append(vs.WSScript_GetPrmReal(i))

Don't try this. You will never be able to open your document again. Because Worksheets script automatically actualize at open document (which seems to be a nice feature ...) but If you have an endless loop in your script you will have to wait endless :-)

Or maybe it's because the vs.WSScript_Get******* ist not very liberal, if you get wrong value type. A String instead of a float as example?

Edited by DomC
Link to comment

Hi Carl

I made an example. Attached to this post. Hope, It can help you finish your project.

Code in the Worksheet:

=RUNSCRIPT('Script-1', A2, B2, C2, D2, E2)

Code of the Script

i=0;arg_len=1;args=[]
while arg_len >0:
arg = vs.WSScript_GetPrmStr(i)
args.append(arg)
arg_len = len(arg)
i=i+1

vs.WSScript_SetResStr(str(args))

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...