使用来自Python的时间戳更新Googlesheet单元格

时间:2018-09-10 15:11:15

标签: python python-3.x timestamp gspread

我正在尝试使用python 3.6.5用我的机器的当前日期/时间更新google工作表中的单元格。我正在使用gspread连接到Google工作表。

如果我执行以下操作,它将给出我要放入Google表格中的日期/时间:

import datetime
print(datetime.datetime.now())
2018-09-10 10:50:38.171795

我正在努力弄清楚如何将输出打印到Google表格的单元格中。这是我的设置,最后一行是我要弄清楚的内容:

import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials

#connect to google sheets through API
json_key = 'work.json'
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)
client = gspread.authorize(credentials)

#define googlesheet
rsheet = client.open_by_url('https://docs.google.com/spreadsheets/d/randomgooglesheetid')

#define the correct worksheet, index - 0 is the first sheet, 1 is the second...
engwsr = rsheet.get_worksheet(0)

engwsr.update_acell('O1' , print(datetime.datetime.now()))

最后一行仅将datetime打印到python中,而不更新google工作表中的任何内容。我的目标是将'2018-09-10 10:50:38.171795'打印到单元格O1中。

有更好的方法吗? datetime格式不必完全是这种格式,而可以通过日期和时间轻松读取。

1 个答案:

答案 0 :(得分:3)

代替

engwsr.update_acell('O1' , print(datetime.datetime.now()))

放入

engwsr.update_acell('O1' , datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))

print()没有返回值,这就是为什么您在工作表中看不到任何东西的原因。

相关问题