0
我正在尝试将传感器数据写入Google工作表。我能够在一年前写了同一张表,但我再次活跃在这个项目上,无法使其工作。我相信Oauth已经改变了,并且我已经更新了我的代码以适应这种变化。gspread数据不会出现在Google工作表中
在下面的代码中,我没有收到任何错误,但没有在GoogleSheet中输入数据。另外,如果我查看Google表格,“上次打开”日期并不反映我的程序将/应该写入该Google表格的时间。 我试过很多变化,我只是卡住了。任何建议,将不胜感激。
#!/usr/bin/python3
#-- developed with Python 3.4.2
# External Resources
import time
import sys
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import traceback
# Initialize gspread
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('MyGoogleCode.json',scope)
client = gspread.authorize(credentials)
# Start loop ________________________________________________________________
samplecount = 1
while True:
data_time = (time.strftime("%Y-%m-%d %H:%M:%S"))
row = ([samplecount,data_time])
# Append to Google sheet_
try:
if credentials is None or credentials.invalid:
credentials.refresh(httplib2.Http())
GoogleDataFile = client.open('DataLogger')
#wks = GoogleDataFile.get_worksheet(1)
wks = GoogleDataFile.get_worksheet(1)
wks.append_row([samplecount,data_time])
print("worksheets", GoogleDataFile.worksheets()) #prints ID for both sheets
except Exception as e:
traceback.print_exc()
print ("samplecount ", samplecount, row)
samplecount += 1
time.sleep(5)