get_named_range returns 1D array, not 2D as documented

Issue #671 wontfix
Tony Middleton created an issue

In a test S/S I have a 2x2 named range. From the documentation of get_named_range I expected it to return a 2 element tuple, each element itself a 2 element tuple of cells. What I received was a 4 element tuple of cells.

Having looked at the code I thing there is an error. At the end of the method it has

for row in rows:

I believe "extend" should be replaced by "append".



Comments (5)

  1. CharlieC

    I don't really know. I think the best thing might be to remove the method altogether. I didn't write the original code but having recently worked on the definedName code, this method is pretty meaningless for worksheets. definedNames are global objects and can contain pretty much anything including multiple ranges of cells from different worksheets. It makes more sense to me to let client code do what it wants with the range.

    So, open to a PR (for 2.5) with changes otherwise it will become deprecated.

  2. CharlieC

    Okay. I've looked at this and this history in a bit more detail. At some point it was .append() and I've must have changed it, but only so that any tests would run.

    Because "ranges" can contain constants, formulae, individual cells, ranges of cells and even multiple ranges of cells (from different worksheets) —Blatt1!$F$10:$G$16,Blatt1!$I$15:$J$19+,Blatt2!$I$15:$J$19— is a valid definition then the only sensible thing to do is to deprecate this method entirely and pass the responsibility for handling defined names to client code. This is pretty easy:

    r = wb.defined_names['my_range']
    ranges = list(r.destinations)
    for title, r in ranges:
         ws = wb[title]
         cells = ws[r]

    And you can handle what comes out of this however you want. This is available in 2.4 so I don't think that at this stage it's worth worrying about exactly how get_named_range() works.

  3. CharlieC

    This method only ever provided partial functionality. Defined names are workbook objects and should be handled in client code using the full API.

  4. Log in to comment