1

我有一个数据列表可以说

record = ['Tuesday','2018-08-29'] 

我尝试通过它发送到谷歌表

sheet.insert_row(record,index)

我在我的 gsheet 上成功接收到它,但问题是,在 gsheet 上我得到一个日期作为字符串,我不能在上面应用任何公式。我尝试将字符串转换为日期格式

import datetime


record[1] = datetime.datetime.strptime(record[1], '%Y-%m-%d')

但随后使用 insert_row 给了我错误

TypeError: Object of type datetime is not JSON serializable
4

1 回答 1

1

As per Google Sheets API Docs, you must format your date as a float of days since the Google Sheets epoch of December 30, 1899.

For instance, in my code I am working with Pandas timestamps but you can work with another timestamp datatype as you please:

time = (pd.to_datetime(time) - pd.datetime(1899, 12, 30)) / pd.to_timedelta(1, unit='D')

Upon passing this through to API into a Google sheet with the column formatted properly, you will get a google datetime type.

于 2020-07-11T05:33:43.330 回答