question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to insert datetime object

See original GitHub issue

Environment info Operating System: Windows 10 Python version: 2.7.12 gspread version: 2.0.0

Steps to reproduce

  1. worksheet.insert_row([datetime.datetime.now()])

Traceback:

Traceback (most recent call last):
  File "D:/gspreadinserttest.py", line 21, in <module>
    sh.insert_row([datetime.datetime.now()])
  File "C:\Python27\lib\site-packages\gspread\v4\models.py", line 685, in insert_row
    'values': [values]
  File "C:\Python27\lib\site-packages\gspread\v4\models.py", line 112, in values_update
    r = self.client.request('put', url, params=params, json=body)
  File "C:\Python27\lib\site-packages\gspread\v4\client.py", line 67, in request
    endpoint, json=json, params=params, data=data, files=files
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 566, in put
    return self.request('PUT', url, data=data, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 494, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 437, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests\models.py", line 308, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python27\lib\site-packages\requests\models.py", line 458, in prepare_body
    body = complexjson.dumps(json)
  File "C:\Python27\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "C:\Python27\lib\json\encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2018, 3, 11, 13, 26, 34, 292000) is not JSON serializable

I know it is possible to convert to a string before inserting however in previous gspread versions I could insert a datetime object and it would format the datetime using the formatting from the google sheet. Is there anyway this kind of functionality could be replicated in 2.0.0?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
cgmortoncommented, May 4, 2018

I’m not sure how helpful this will be, but I was able to write a date to the sheet by converting it to a count of the number of days since the Google Sheets epoch date of “1899-12-30” (https://developers.google.com/sheets/api/guides/concepts).

Something like: cell.value = (datetime.datetime(2018, 3, 11) - datetime.datetime(1899, 12, 30)).days

2reactions
MAlhussainicommented, Nov 24, 2021

You could use this function, it worked for me:

Imports:

import gspread
import datetime

Lets have a variable now as datetime object:

now = datetime.datetime.now()

then pass now to the following function as string.

wb.values_update(
    'sheet1!A2',
    params={
        'valueInputOption': 'USER_ENTERED'
    },
    body={
        'values': [[str(now)]]
    }
)

This way your entered datetime will be recognized by google sheet as if it was entered by a person. If you want to know more about how to use gspread follow this link.

Entering multiple values could be done as well by replacing [[str(now)]]

with:

[[str(now) ,'b','c','d']]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to insert DateTime format into database - Stack Overflow
you try changing your date time formatDateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider); to DateTime ...
Read more >
[Solved] can't insert datetime type to sql database - CodeProject
Check whether DateTime value, which you are inserting is in correct format or not? 3 solutions. Top Rated; Most Recent.
Read more >
Cannot insert datetime value in datetime column - MSDN
Hi, I'm having a wierd problem. Tring to add datetime data to a simple table gives me the following error : Conversion failed...
Read more >
Unable to Insert Date into Data Extension via SSJS
When retrieving dates via API, most of the time I end up with an array of objects. var array = [{"Status":1,"StatusMessage":"Complete"," ...
Read more >
DateTime data insert to sql server - Studio - UiPath Forum
Hi, This is my input data: i try to insert data into sql server query… sometime date and time missing… i tried code: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found