Author Topic: int as None  (Read 1160 times)

Glauco

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
int as None
« on: November 15, 2011, 12:50:19 PM »
@xl_func("int:")
def w(v):
    print v


Hi, is not quite clear to me why an empy cell is printed as 0(zero).
I expect a NoneType, i think 0 is different from an empty cell.

Am i wrong?

thank you
Glauco

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 574
  • Karma: +13/-1
    • View Profile
Re: int as None
« Reply #1 on: November 15, 2011, 07:50:23 PM »
Hi Glauco,

you need to use the 'var' type if you want to get None for an empty cell. Excel passes 0 for an empty cell when the argument type is int, so by the time it gets to PyXLL there's no way of knowing that it was actually empty.

So, you could do something like this:

@xl_func("var:")
def w(v):
    if v is not None:
        v = int(v)
    print v

Or you could use a custom type if you're feeling adventurous:

@xl_arg_type("int_or_none", "var")
def int_or_none(x):
    if x is None:
        return None
    return int(x)

@xl_func("int_or_none:")
def w(v):
    print v

See http://www.pyxll.com/docs/udfs.html#custom-types for more details about custom types.

Best regards,
Tony


Glauco

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: int as None
« Reply #2 on: November 17, 2011, 02:28:42 PM »
Good  solutions.

Thank you for clarifications.
Gla