msl.io.readers.gsheets module

Read a Google Sheets spreadsheet.

class msl.io.readers.gsheets.GSheetsReader(file, **kwargs)[source]

Bases: Spreadsheet

Read a Google Sheets spreadsheet.

This class simply provides a convenience for reading information from Google spreadsheets. It is not registered as a Reader because the information in a spreadsheet is unstructured and therefore one cannot generalize how to parse a spreadsheet to create a Root.

Parameters:
  • file (str) – The ID or path of a Google Sheets spreadsheet.

  • **kwargs – All keyword arguments are passed to GSheets.

Examples

>>> from msl.io import GSheetsReader  
>>> sheets = GSheetsReader('Google Drive/registers/equipment.gsheet')  
>>> sheets = GSheetsReader('1TI3pM-534SZ5DQTEZ-7HCI04648f8ZpLGbfHWJu9FSo')  
close()[source]

Close the connection to the GSheet API service.

New in version 0.2.

read(cell=None, sheet=None, as_datetime=True)[source]

Read values from the Google Sheets spreadsheet.

Parameters:
  • cell (str, optional) – The cell(s) to read. For example, C9 will return a single value and C9:G20 will return all values in the specified range. If not specified then returns all values in the specified sheet.

  • sheet (str, optional) – The name of the sheet to read the value(s) from. If there is only one sheet in the spreadsheet then you do not need to specify the name of the sheet.

  • as_datetime (bool, optional) – Whether dates should be returned as datetime or date objects. If False then dates are returned as a string in the format of the spreadsheet cell.

Returns:

The value(s) of the requested cell(s).

Examples

>>> sheets.read()
[('temperature', 'humidity'), (20.33, 49.82), (20.23, 46.06), (20.41, 47.06), (20.29, 48.32)]
>>> sheets.read('B2')
49.82
>>> sheets.read('A:A')
[('temperature',), (20.33,), (20.23,), (20.41,), (20.29,)]
>>> sheets.read('A1:B1')
[('temperature', 'humidity')]
>>> sheets.read('A2:B4')
[(20.33, 49.82), (20.23, 46.06), (20.41, 47.06)]
sheet_names()[source]

Get the names of all sheets in the Google Sheets spreadsheet.

Returns:

tuple of str – The names of all sheets.