#TL;DR
- 日付型はそのまま入れられないので文字列に変換する
- 文字列の日時指定は海外のスタイルで書かないと
INVALID_ARGUMENT
で怒られ続けるので下記にする"%d-%m-%Y %H:%M:%S"
- 値のオプションに
USER_ENTERED
を指定する- append_rowとかだとあるから似たような他のでもあるんじゃないか
やりたいこと
- 日付列を持つデータが複数行ある
- そのデータをgspreadから追加していきたい
怒られたこと
No.1
皆さん御存知の通りこんな感じで使おうとする
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread-key.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('test-sheet').sheet1
date = datetime.datetime.now()
time = date.strftime('%Y-%m-%d %H:%M:%S'),
# gspread run
datas = [time, 250]
wks.append_row(datas)
INVALID_ARGUMENT
で怒られる
No.2
日付型でそのままいけんのかー?とこうする
# gspread run
datas = [date, 250]
wks.append_row(datas)
root@NAKAMIRI-DESKMINI:~# python test.py
Traceback (most recent call last):
File "test.py", line 32, in <module>
wks.append_row(datas)
File "/usr/local/lib/python2.7/dist-packages/gspread/models.py", line 1441, in append_row
table_range=table_range,
File "/usr/local/lib/python2.7/dist-packages/gspread/models.py", line 1478, in append_rows
return self.spreadsheet.values_append(range_label, params, body)
File "/usr/local/lib/python2.7/dist-packages/gspread/models.py", line 164, in values_append
r = self.client.request('post', url, params=params, json=body)
File "/usr/local/lib/python2.7/dist-packages/gspread/client.py", line 67, in request
headers=headers,
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 578, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/google/auth/transport/requests.py", line 470, in request
**kwargs
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 516, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 459, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 317, in prepare
self.prepare_body(data, files, json)
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 467, in prepare_body
body = complexjson.dumps(json)
File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2020, 10, 20, 21, 24, 12, 211250) is not JSON serializable
ですよねー
No.3
入力値のオプション足りんかったかー
# gspread run
datas = [time, 250]
wks.append_row(datas, value_input_option="USER_ENTERED")
INVALID_ARGUMENT
アッハイ...