Search code examples
pythongoogle-sheets-apigspread

Python Google Sheets API Limit 429 Error with Loop


I am running a python script that is taking specific columns from one Google Sheet to another Google Sheet via the Google Sheets API and Gspread package, but I am running into a 429 Error when I am pushing a list to the new spreadsheet. This error is connected to too many requests, but I am not sure what is making my push run multiple times, rather than once. Is it possible that my worksheet.update_cells(updated_values) is being included in the loop?

Error:

APIError: {
  "error": {
    "code": 429,
    "message": "Quota exceeded for quota group 'WriteGroup' and limit 'USER-100s' of service 'sheets.googleapis.com' for consumer 'project_number:*id*'.",
    "status": "RESOURCE_EXHAUSTED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/*id*/apiui/credential"
          }
        ]
      }
    ]
  }
}

Code:

# column names
print(columns) # ['date', 'b_clicks', 'b_cpc']

# store count of column names
gs_columns = []

# count columns
for i in range(0,len(columns)):
    gs_columns.append(i+1)

print(gs_columns) # [1,2,3]

updated_values = []

for col_val, col_name in zip(gs_columns, columns):
    worksheet_range = worksheet.range(1, col_val, 500, col_val); # [row_start, col_start, row_end, col_end]
    print(type(worksheet_range))
    column_data = df_full[col_name].values.tolist();
    for cell, data in zip(worksheet_range, column_data):
        cell.value = data
        updated_values.append(cell)

worksheet.update_cells(updated_values)
print(updated_values)
print(type(updated_values))
print(len(updated_values))

Print:

['date', 'b_clicks', 'b_cpc']
[1, 2, 3]
<class 'list'>
<class 'list'>
<class 'list'>
[<Cell R1C1 1514764800000000000>, <Cell R2C1 1514851200000000000>, <Cell R3C1 1514937600000000000>....<Cell R345C3 3.21>, <Cell R346C3 3.92>]
<class 'list'>
1038

Solution

  • Encountering the same problem is there a way we can limit the program as per the following criteria such as below in the for loop that we can limit the no of requested to api and match the following criteria

    time.sleep(10)
    

    About developers.google.com :

    Google Sheets API has a limit of 500 requests per 100 seconds per project, and 100 requests per 100 seconds per user. Limits for reads and writes are tracked separately. There is no daily usage limit.