Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read value of fetched cell data as date google sheets API

I have a spreadsheet that have dates formatted as "Jan 30" etc, but have actual date values.

enter image description here

I fetch the data using python google Sheets API like so:

def get_spreadsheet_sheets(service, spreadsheetId):
    spreadsheet = service.spreadsheets().get(spreadsheetId=spreadsheetId).execute()
    dateCell = "C15"
    for sheet in spreadsheet['sheets']:
        sheetTitle = sheet['properties']['title']
        rangeName = "%s!%s" % (sheetTitle, dateCell)
        result = service.spreadsheets().values().get(
            spreadsheetId=spreadsheetId, range=rangeName).execute()
        values = result.get('values',[])

values gives me 'Jan 30', I would like to actually read it as 1/30/2017.. How can I do that?

update

Digger deeper into the spreadsheets.value.get http documentation (which corresponds to the python api client method references here I learned that I can put the option of valueRenderOption as UNFORMATTED_VALUE..

however when I run that I get this weird number for Jan 30: 42765

enter image description here

what format is that number exactly? I know it's not a unix time stamp b/c converting it returns a non-sensical date

update 2: workaround

this code works but using python date formatting (it also assumes that the year is the current year.. but obviously that won't always be the case):

def get_spreadsheet_sheets(service, spreadsheetId):
    spreadsheet = service.spreadsheets().get(spreadsheetId=spreadsheetId).execute()
    dateCell = "C15"
    for sheet in spreadsheet['sheets']:
        sheetTitle = sheet['properties']['title']
        rangeName = "%s!%s" % (sheetTitle, dateCell)
        result = service.spreadsheets().values().get(
            spreadsheetId=spreadsheetId, range=rangeName).execute()
        values = result.get('values',[])
        dateStr = "%s %s" % (values[0][0], datetime.date.today().year)
        cellDate = datetime.datetime.strptime(dateStr, '%b %d %Y')
        print(cellDate)
like image 510
abbood Avatar asked Feb 14 '17 01:02

abbood


People also ask

How do I convert a string to a date in Google Sheets?

To get dates, change the cell format to Date. If providing an explicit string input to DATEVALUE rather than a cell reference, surrounding quotation marks are required. Some date formats are not understood by Google Sheets.


1 Answers

The "weird number" it's called "serialized date-time value" or "date serial value". The 0 value displayed as date (yyyy-mm-dd) is 1899-12-30. Integers are days, fractions are hours, minutes, etc.

Related: Converting Google spreadsheet date into a JS Date object?

References:

  • TO_DATE, Google Spreadsheet built-in function
  • DateTime Serial Numbers, Article section from Google Sheets > API v4 Guide (the referred section was removed, it might be available on archive.org)
  • Date and Number Formats, Article from Google Sheets > API v4 Guide.
  • Google Sheet API V4(Java) append Date in cells , Stack Overflow Q&A

h/t Sam Berlin

like image 162
Rubén Avatar answered Sep 30 '22 05:09

Rubén