我正在尝试将传感器数据写入谷歌表格。大约一年前,我可以在同一张纸上写信,但我再次活跃在这个项目上,无法让它发挥作用。我相信 Oauth 已经改变,并且我已经更新了我的代码以进行更改。
在下面的代码中,我没有收到任何错误,但是在 GoogleSheet 中没有输入任何数据。此外,如果我查看 GoogleSheets,“上次打开”日期并不能反映我的程序将/应该写入该 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)