IndexError reading ranges

Issue #369 resolved
CharlieC
created an issue

Some files cannot be read because of IndexErrors reading ranges.

openpyxl/openpyxl/reader/workbook.py", line 160, in read_named_ranges
    named_range.scope = workbook.worksheets[int(location_id)]
builtins.IndexError: list index out of range

A workaround is possible by placing the lookup in a try: … except IndexError: pass clause

The relevant code is

        location_id = name_node.get("localSheetId")
        if location_id is not None:
            named_range.scope = workbook.worksheets[int(location_id)]

The workaround could look like this

        location_id = name_node.get("localSheetId")
        if (location_id is not None):
            location_id = int(location_id)
            if locaiton_id <= len(workbook.worksheets):
                 named_range.scope = workbook.worksheets[location_id]

Comments (3)

  1. CharlieC reporter

    According to the specification localSheetId "Specifies the sheet index in this workbook where data from an external reference is displayed." so we can just pass it through and don't really need to look it up. Resolves #369 at least temporarily. The handling of external references will probably need revisiting.

    → <<cset ad9001ca1f28>>

  2. Log in to comment