使用来自 Python 的时间戳更新 Googlesheet 单元格
Update Googlesheet cell with timestamp from Python
我正在尝试使用 python 3.6.5 使用我机器的当前 date/time 更新 google sheet 中的单元格。我正在使用 gspread 连接到 google sheet。
如果我执行以下操作,它将给出我要放入 google sheets:
中的 date/time
import datetime
print(datetime.datetime.now())
2018-09-10 10:50:38.171795
我正在努力弄清楚如何在 google sheet 秒内将输出打印到单元格中。这是我的设置,最后一行是我想弄清楚的:
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()))
最后一行只是将日期时间打印到 python 中,并没有更新 google sheet 中的任何内容。我的目标是将“2018-09-10 10:50:38.171795”打印到单元格 O1 中。
有没有更好的方法来做到这一点?日期时间格式不必完全像那样,只需使用易于阅读的日期和时间即可。
而不是
engwsr.update_acell('O1' , print(datetime.datetime.now()))
放
engwsr.update_acell('O1' , datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
print() 没有 return 值,这就是为什么您在 sheet.
中看不到任何内容的原因
你可以使用这个功能,它对我有用:
进口:
import gspread
import datetime
让变量 now 作为日期时间对象:
now = datetime.datetime.now()
然后将 now 作为字符串传递给以下函数。
wb.values_update(
'sheet1!A2',
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[str(now)]]
}
)
这样您输入的日期时间将被 google sheet 识别,就像它是由人输入的一样。
如果您想了解更多关于如何使用 gspread follow this link.
输入多个值也可以通过替换 [[str(now)]]
来完成
与:
[[str(now) ,'b','c','d']]
我正在尝试使用 python 3.6.5 使用我机器的当前 date/time 更新 google sheet 中的单元格。我正在使用 gspread 连接到 google sheet。
如果我执行以下操作,它将给出我要放入 google sheets:
中的 date/timeimport datetime
print(datetime.datetime.now())
2018-09-10 10:50:38.171795
我正在努力弄清楚如何在 google sheet 秒内将输出打印到单元格中。这是我的设置,最后一行是我想弄清楚的:
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()))
最后一行只是将日期时间打印到 python 中,并没有更新 google sheet 中的任何内容。我的目标是将“2018-09-10 10:50:38.171795”打印到单元格 O1 中。
有没有更好的方法来做到这一点?日期时间格式不必完全像那样,只需使用易于阅读的日期和时间即可。
而不是
engwsr.update_acell('O1' , print(datetime.datetime.now()))
放
engwsr.update_acell('O1' , datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
print() 没有 return 值,这就是为什么您在 sheet.
中看不到任何内容的原因你可以使用这个功能,它对我有用:
进口:
import gspread
import datetime
让变量 now 作为日期时间对象:
now = datetime.datetime.now()
然后将 now 作为字符串传递给以下函数。
wb.values_update(
'sheet1!A2',
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[str(now)]]
}
)
这样您输入的日期时间将被 google sheet 识别,就像它是由人输入的一样。 如果您想了解更多关于如何使用 gspread follow this link.
输入多个值也可以通过替换 [[str(now)]]
与:
[[str(now) ,'b','c','d']]