[Python] Return latest updated cell

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm fairly new to the Google sheet API.

    I have been attempting to make a bot that takes IDs and processes them through an information system.

    I have run into a few issues such as getting the last updated cell, so I can run my code.

    The main issue is running into a 'maximum request quota reached' error. Trying to build this thing on the lowest dime is challenging. Always trying to get just the last item in a list requires me to request something from the sheet and putting that in a loop is sub optimal.

    I have tried utilizing Google App Scripts, running that with a flask server. Essentially onEdit for any cell on the sheet, send my flask server a request. Didn't end up working due to google and their awesome security.

    Finally, I figured out an offshoot version of my first solution, utilizing loops, except I used time.sleep to space out the requests.

    This code is for individual rows:

    count = len(specific_sheet.col_values(1)) # a count for how many cells have filled information in a specific column, works for entire sheet as well

    while True:
    try:
    time.sleep(5) # request break
    count2 = count # a second, updated count for the sheet

    if count2 > count: # checks to see if data has been added to the sheet

    sheet = client.open_by_url(constants.sheet_id) # rechecks credentials (didn't work without it being in the loop checking every time)

    # gets correct worksheet from sheet
    month_sheet = sheet.worksheet("NAME_OF_WORKSHEET (Ex: 'Sheet1'")

    updated_id = month_sheet.col_values(1) # requests the updated column data

    print(updated_specimen_id[-1])# prints last item in list of data within specific column

    else:
    time.sleep(5) # when the sheet doesn't detect a change

    print(False)

    except Exception as e:
    pass
    print('{e} REQUEST QUOTA REACHED, ONE MOMENT...')
    time.sleep(5)

    count = len(specific_sheet.col_values(1)) # gets another updated count of current sheet, accounts for deletes and updates in a sheet.


    I'm wondering if there is a better way of doing what I have achieved. It works, but its slow. The goal of this snippet of code is for the bot to run when I am not around, making it a bot... Any way to run my code whenever someone edits my sheet on google sheets?

    Continue reading...

Compartilhe esta Página