1

I have a python script that runs 24/7 and have a google spreadsheet that contains input values for the script. I update these values regularly. On a clean start, the initial value pulls in exactly as it should. The issue is when it needs to be updated. The new value on the spreadsheet isn't recognized unless the script is stopped and restarted.

Here is the code:

import gspread
import schedule
import time
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/spreadsheets',
         'https://www.googleapis.com/auth/drive.file',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scope)

gc = gspread.authorize(credentials)
sheet = gc.open('analyze_input')
wks = sheet.worksheet("Sheet1")
atr = wks.acell("A2").numeric_value

def atr_print():
    print(atr)


schedule.every(60).seconds.do(atr_print)
while True:
    schedule.run_pending()
    time.sleep(1)

I've tried multiple variations of the code above - all with the same result. I was going to try the Cron scheduler / Cron tab next. Any ideas or suggestions would be a huge help, thanks!

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Jason
  • 13
  • 2

1 Answers1

0

In your script, how about modifying as follows?

Modified script:

import gspread
import schedule
import time
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/spreadsheets',
         'https://www.googleapis.com/auth/drive.file',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scope)

gc = gspread.authorize(credentials)
sheet = gc.open('analyze_input')
wks = sheet.worksheet("Sheet1")
# atr = wks.acell("A2").numeric_value # Modified

def atr_print():
    atr = wks.acell("A2").numeric_value # Modified
    print(atr)


schedule.every(60).seconds.do(atr_print)
while True:
    schedule.run_pending()
    time.sleep(1)
  • In this modification, when atr_print() is run, the value is retrieved from "A2" of "Sheet1" sheet in "analyze_input" Spreadsheet.
Tanaike
  • 181,128
  • 11
  • 97
  • 165