Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets API addProtectedRange Error: No grid with id: 0

I am not sure if I am making a mistake or if possibly this is related to the same issue reported here:

Google Sheets API V4 - Autofill Error - No grid with id: 0

I am getting:

HttpError 400
"Invalid requests[0].addProtectedRange: No grid with id: 1"

Code is something like this (additional addProtectedRange objects removed)

def add_protected_ranges(spreadsheet_id):
    service = get_sheets_service()

    requests = [
    {
        "addProtectedRange": {
            'protectedRange': {
                "range": {
                    "sheetId": 1,
                    "startRowIndex": 0,
                    "endRowIndex": 0,
                    "startColumnIndex": 0
                },
                "description": "Headers must not be changed",
                "warningOnly": True
            }
        }
        }

    ]

    body = {
        'requests': requests
    }
    response = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id,
                                              body=body).execute()
like image 295
Patrick Rainsberry Avatar asked Oct 11 '17 19:10

Patrick Rainsberry


2 Answers

Had kind of the same issue. I was confusing the sheet id with sheet index.

Easiest way to find the sheet id is in the browser URL when you open the spreadsheet / sheet: https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit#gid={sheetId}

If you're looking for a more programmatic way you find that property on the SheetProperties.

like image 81
Stefan Avatar answered Oct 20 '22 13:10

Stefan


I know that's an old question but I was looking for it too. You're looking for:

res.data.sheets[].properties.sheetId

To get the sheetId (not the spreadsheetId) use:

sheets.spreadsheets.get({
  spreadsheetId
}).then(res => {
  console.log("All the sheets:");
  for(i in res.data.sheets) {
    let title = res.data.sheets[i].properties.title;
    let id = res.data.sheets[i].properties.sheetId;
    console.log(title + ': ' + id);
  }
});
like image 29
Francisco Gomes Avatar answered Oct 20 '22 15:10

Francisco Gomes