Skip to content

UDF with expand=table disappears on recalculation #1164

@nickderobertis

Description

@nickderobertis

OS (e.g. Windows 10 or macOS Sierra)

Windows 10 Enterprise

Versions of xlwings, Excel and Python (e.g. 0.11.8, Office 365, Python 3.7)

xlwings 0.15.10, Excel Professional Plus 2016, Python 3.7

Describe your issue (incl. Traceback!)

Thanks for including the expand functionality in the library, it seems very useful if I can get it working smoothly.

I had noticed as I was building a workbook using UDFs with expand='table' that sometimes my UDFs would disappear entirely upon recalculation. I had to do a lot of experimenting to narrow down exactly when this occurs.

This happens for UDFs with expand='table' for both @xw.ret and @xw.arg if they are referencing another UDF which has expand='table' for @xw.ret. It also only occurs if this is the last function which was defined referencing the cells of the table.

E.g. the setup looks something like this:
Plain Cells -> Reference plain cells by UDF with expand='table' for both @xw.ret and @xw.arg -> Reference first UDF output by another UDF with expand='table' for @xw.ret.

If any of these conditions are not met, the problem will not occur. Calculating any function, UDF or otherwise (=SUM works), on the full cell range of the table after defining the UDF will make the UDF call not disappear. If the same UDF is called a second time, the second occurrence will disappear, but the first will not.

If expand='table' is excluded for either @xw.ret or @xw.arg on the first UDF in the chain, the problem will not occur. If expand='table' is excluded for @xw.ret on the second UDF, the problem will not occur.

A temporary workaround on the test project is to call my UDF first in the spot where I want it, then call it in an empty spot that I don't want it. Upon recalculate, the one in the empty spot disappears, but the first one will always persist from that point on. However there may be still some conditions where this occurs that I don't understand, as I have tried this fix in my full workbook, and it works initially, but after recalculating many times, some functions will still disappear. In this workbook I have a chain of 4 UDFs using expand.

Include a minimal code sample to reproduce the issue (and attach a sample workbook if required!)

While this will happen for any UDF matching the conditions above, I have included the most simple version possible here.

@xw.func
@xw.arg('data', expand='table')
@xw.ret(expand='table', index=False)
def print_table(data):
    return data

This sample project shows the issue. Open the workbook, and recalculate with CTRL + ALT + F9, and the second UDF disappears on my machine.

Try opening the workbook again, if instead, you call print_table a second time referencing the first UDF, then only this newly created UDF will disappear on recalculation, and not ever the original second UDF again.

Try again, if you call =SUM(E4:E6), then recalculate, the second UDF will not disappear. You can even delete the =SUM call then, and recalculate as many times as you want and the second UDF will not disappear.

disappearing.zip

I appreciate any help you could give. I could also try to fix this if you point me in the right direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions