'google sheet latest data fetch

I need to automate an OTP capture for my selenium-python automation scripts, Now the otp is getting updated in the google sheet and I am fetching the same using google API using the following code. Now My script runs every hour, so in that case, I will have new OTP which is getting updated every hour. How to get the latest updated data from google sheet using python?

"otp" is my sheet name. and I am using the "range="otp!a1" for getting the value of first row and first column. So during the second run it should be "range="otp!a2"for getting the value of second row and first column and so on...

Now my goal is to capture the latest value during each execution.

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'keys.json'
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
SAMPLE_SPREADSHEET_ID = '1YYfxsX2FsdXLXqRwx0WTTlD7ru56m1D'
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,range="otp!a1").execute()
values = result.get('values', [])
print(values)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source