Skip to content

Can't Delete Named Ranges when they are broken or with "#REF" #1530

Description

@makubexD

Important: Please do not post usage questions here.
To get a quick response, please ask a question on Stack Overflow using gspread tag.
See existing questions: https://stackoverflow.com/questions/tagged/gspread


Describe the bug
I'm trying to delete some named ranges I already created in a sheet called "StaticDataSheet". However, I get this message

gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}

This is because I decided to delete this sheet manually. When doing so, the named ranges are not explicitly deleted and remain listed.
Here is a sample

image

So each time, I'm trying to use this:

spread_sheet_main.list_named_ranges()

When I try to display the list, I only get an empty array [], and there is no option to delete named ranges with "#REF" errors

To Reproduce
Steps to reproduce the behavior:

  1. Create a new sheet with a specific name to identify it, along with an extra sheet in addition to the default "Sheet1." Then, create some named ranges.
  2. Verify that the named ranges were created correctly by navigating to Data → Named Ranges in the menu.
  3. Delete the recently created sheet.
  4. Check Data → Named Ranges again.
  5. You should now see "#REF" for the previously created "named ranges".
  6. In your code, try using spread_sheet_main.list_named_ranges(). You will receive an empty array. With an empty result, there is nothing available to delete.
  7. In the code, assume you proceed to create these "named ranges" again.
  8. When the program reaches the code for creating named ranges, it encounters an error due to the existing #REF entries.
    gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}

Expected behavior
if we have implemented a logic to delete we should delete without any problem

Code example*

def delete_existing_named_ranges(self, spreadsheet, range_names):
    """Delete named ranges with specified names if they already exist."""
    existing_named_ranges = spreadsheet.list_named_ranges()
    update_requests = []

    for named_range in existing_named_ranges:
        if named_range['name'] in range_names:
            delete_request = {
                "deleteNamedRange": {
                    "namedRangeId": named_range['namedRangeId']
                }
            }
            update_requests.append(delete_request)
            print(f"Deleting existing named range: {named_range['name']}")

    if update_requests:
        spreadsheet.batch_update({"requests": update_requests})

For further details in implementation, I created a repo with all the logic https://github.com/makubexD/namedRangesGSBug

Screenshots
image
image
image

Environment info:

  • Operating System [Windows]:
  • Python version: python --version --> Python 3.10.8
  • gspread version: Version: 5.12.4

Stack trace or other output that would be helpful

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions