Skip to content

Commit

Permalink
docs: Add datetime explanation in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe2803 committed Nov 26, 2021
1 parent 590df68 commit cb21f86
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ A Simple Example
data_range.set_background('#000000') # set every cell backgrounds to black
data_range.set_font_color('#ffffff') # set every cell font colors to white
For better security, you can also create your SpreadsheetApp object with
environment variables instead of the `secrets.json` file.
To obtain your secret json file and know more about how to initialize your ENV vars, you can refer to `the authentication tutorial`_.
You can also create your SpreadsheetApp object with environment variables
instead of the `secrets.json` file. You can refer to `the authentication tutorial`_ for more info.

.. _the authentication tutorial: https://github.com/socialpoint-labs/sheetfu/blob/master/documentation/authentication.rst

You can refer to the `sheetfu API documentation`_ for a more detailed description.
Please read the `sheetfu API documentation`_ for a more detailed description.

.. _sheetfu API documentation: https://github.com/socialpoint-labs/sheetfu/blob/master/documentation/usage.rst


The Table module
----------------

Expand Down Expand Up @@ -86,6 +86,52 @@ You can refer to the `Table API documentation`_ for a more detailed description.
.. _Table API documentation: https://github.com/socialpoint-labs/sheetfu/blob/master/documentation/table.rst


Casting
-------

An effort has been made to guide Sheetu as a Google Sheet ORM, where any values
found in a spreadsheet are casted to a matching Python object. Since version
1.5.7, Sheetfu returns `DATE` and `DATE_TIME` as Python `datetime` object.
Similarly, setting a cell with a `datetime` object will make the necessary
parsing and casting to reflect those cells as `DATE_TIME` in the sheet.

.. code-block:: python
from sheetfu import SpreadsheetApp
sa = SpreadsheetApp('path/to/secret.json')
spreadsheet = sa.open_by_id('<insert spreadsheet id here>')
sheet = spreadsheet.get_sheet_by_name('Sheet1')
# Assuming the cells are in DATE or DATE_TIME format.
cells_with_dates = sheet.get_range_from_a1("A1:A2"))
print(cells_with_dates.get_values())
# [
# [datetime.datetime(2021, 11, 26, 16, 58, 37, 737940)],
# [datetime.datetime(2021, 11, 26, 16, 58, 37, 737940)]
# ]
This means we can introduce python datetime operation in our code very
effectively.


.. code-block:: python
from sheetfu import SpreadsheetApp
from datetime import datetime
sa = SpreadsheetApp('path/to/secret.json')
spreadsheet = sa.open_by_id('<insert spreadsheet id here>')
sheet = spreadsheet.get_sheet_by_name('Sheet1')
a1 = sheet.get_range_from_a1("A1")
# The following will set today's date in the
#cell in the right google sheet format
a1.set_value(datetime.today())
Contributing
------------

Expand Down

0 comments on commit cb21f86

Please sign in to comment.