Skip to content

Commit

Permalink
feat: add an option to render date-time when reading a sheet (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanix-Darker authored Dec 2, 2022
1 parent 84b003d commit 2488b8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- Feat: The connector `GoogleSheets` datasource now has an option called `Dates as Floats`, to see date time columns as strings or float when reading the sheet.

### [4.0.0] 2022-11-23

### Breaking changes
Expand Down
15 changes: 14 additions & 1 deletion toucan_connectors/google_sheets/google_sheets_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class GoogleSheetsDataSource(ToucanDataSource):
header_row: int = Field(
0, title='Header row', description='Row of the header of the spreadsheet'
)
dates_as_float: bool = Field(
False, title='Dates as floats', description='Render Date as Floats or String from the sheet'
)

class Config:
@staticmethod
Expand Down Expand Up @@ -136,6 +139,14 @@ def get_status(self) -> ConnectorStatus:
except GoogleApiClientError:
return ConnectorStatus(status=False, error="Couldn't retrieve user infos")

def _render_date_time_option_from_sheet(self, data_source: GoogleSheetsDataSource) -> str:
"""
Following the documentation, to prevent loading dates as double :
https://developers.google.com/sheets/api/reference/rest/v4/DateTimeRenderOption
We use FORMATTED_STRING to load as simple strings
"""
return 'SERIAL_NUMBER' if data_source.dates_as_float else 'FORMATTED_STRING'

def _retrieve_data(self, data_source: GoogleSheetsDataSource) -> pd.DataFrame:

if data_source.sheet is None:
Expand All @@ -150,7 +161,9 @@ def _retrieve_data(self, data_source: GoogleSheetsDataSource) -> pd.DataFrame:
.get(
spreadsheetId=data_source.spreadsheet_id,
range=f"'{data_source.sheet}'", # FIXME what will happen is the sheet name contains a single quote?
dateTimeRenderOption='SERIAL_NUMBER',
dateTimeRenderOption=self._render_date_time_option_from_sheet(
data_source=data_source
),
majorDimension='ROWS',
valueRenderOption='UNFORMATTED_VALUE',
)
Expand Down

0 comments on commit 2488b8d

Please sign in to comment.