12

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()

3 Answers3

23

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.

Stefan
  • 452
  • 4
  • 13
  • @PatrickRainsberry How did you get sheetId??? I get this error even if I omit "sheetId" from the range field! I've never ever specified a sheet id in the browser, so I don't see how that could even help figure it out, because you have to know it before you can enter it as an URL! – Michael Jul 13 '18 at 00:08
  • I think maybe you are confusing sheetId and spreadsheetId. In this case I needed the sheetId. Most operations require the spreadsheetId. You don’t need a sheetId to open the document in the browser. – Patrick Rainsberry Jul 14 '18 at 03:12
  • For more info on the `sheetId` property, see https://developers.google.com/sheets/api/guides/concepts#sheet_id – lampShadesDrifter Aug 28 '20 at 08:53
4

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);
  }
});
Francisco Gomes
  • 1,411
  • 12
  • 13
1

Make sure that there is a workbook with the id 1 in your spreadsheet. In google sheet, a spreadsheet can contain multiple worksheet(grid), there is a unique id for each of the worksheet(grid).

buqing
  • 925
  • 8
  • 25