neuralib.tools.gspread

neuralib.tools.gspread.SpreadSheetName

spreadsheet name

neuralib.tools.gspread.WorkPageName

workpage name of the spreadsheet

neuralib.tools.gspread.DataIndex

data index type

alias of None | int | str | slice | list[int] | list[str] | ndarray

class neuralib.tools.gspread.GoogleSpreadSheet[source]

Gspread module wrapper to access google spreadsheet

service_account_path = ...  # The path to the service account json file
sh = GoogleSpreadSheet('test_sheet', service_account_path)

# get a worksheet from the spreadsheet
ws = sh[WORK_SHEET_NAME]
__init__(name, service_account_path, primary_key='Data')[source]
Parameters:
  • name (SpreadSheetName) – name of the spreadsheet

  • service_account_path (PathLike) – The path to the service account json file

  • primary_key (str | tuple[str, ...]) –

    Primary key of the worksheet.

    If str type, it must be one of the column name.

    If tuple str type, the primary key is join using “_” per row

property title: str

SpreadSheetName

property worksheet_list: list[str]

list of WorkPageName

has_worksheet(page)[source]

If has the worksheet, implement also in __contains__()

Parameters:

page (str)

Return type:

bool

get_worksheet(page)[source]

Get the worksheet. implement also in __get_item__()

Parameters:

page (str)

Return type:

GoogleWorkSheet

class neuralib.tools.gspread.GoogleWorkSheet[source]
__init__(worksheet, primary_key='Data')[source]
Parameters:
  • worksheet (gspread.Worksheet) – gspread.Worksheet

  • primary_key (str | tuple[str, ...]) –

    Primary key of the worksheet.

    If str type, it must be one of the column name.

    If tuple str type, the primary key is join using “_” per row

classmethod of(name, page, service_account_path, primary_key='Data')[source]

Get a worksheet from spreadsheet

Parameters:
  • name (SpreadSheetName) – SpreadSheetName

  • page (WorkPageName) – WorkPageName

  • service_account_path (PathLike) – The path to the service account json file

  • primary_key (str | tuple[str, ...]) –

    Primary key of the worksheet.

    If str type, it must be one of the column name.

    If tuple str type, the primary key is join using “_” per row

Returns:

GoogleWorkSheet

Return type:

Self

property title: str

WorkPageName

property headers: list[str]

list of worksheet header

property primary_key_list: list[str]

list of primary key of the worksheet

get_range_value(a1_range_notation)[source]

get values from range notation. i.e., B1:S1 to get the list of content.

If get values from 2D, the return order would be first column-wise, then row-wise

Parameters:

a1_range_notation (str)

Return type:

list[Any]

values(head)[source]

get list of value from header

Parameters:

head (str)

Return type:

list[Any]

get_cell(data, head, value_render_option='FORMATTED_VALUE')[source]

Get data from a cell

Parameters:
  • data (None | int | str | slice | list[int] | list[str] | ndarray) – DataIndex

  • head (str) – header name

  • value_render_option (Literal['FORMATTED_VALUE', 'UNFORMATTED_VALUE', 'FORMULA']) – VALUE_RENDER_OPT: {‘FORMATTED_VALUE’, ‘UNFORMATTED_VALUE’, ‘FORMULA’}

Returns:

update_cell(data, head, value)[source]

Update value in a cell

Parameters:
  • data (DataIndex) – DataIndex

  • head (str) – header name

  • value (list[str] | str) – value to be updated. str type if single field

Returns:

clear()[source]

Clears all cells in the worksheet

update(range_name, values=None, **kwargs)[source]

Sets values in a cell range of the sheet

to_pandas()[source]

Worksheet to pandas dataframe

Return type:

DataFrame

to_polars()[source]

Worksheet to polar dataframe

Return type:

DataFrame

neuralib.tools.gspread.upload_dataframe_to_spreadsheet(df, gspread_name, worksheet_name, service_account_path, primary_key='Data')[source]

Upload a dataframe to a gspread worksheet

Parameters:
  • df (DataFrame) – polars or pandas DataFrame

  • gspread_name (SpreadSheetName) – spreadsheet name

  • worksheet_name (WorkPageName) – worksheet name under the spreadsheet

  • service_account_path (Path | None) – The path to the service account json file.

  • primary_key (str | tuple[str, ...]) – Primary key of the worksheet. If str type, it must be one of the column name. If tuple str type, the primary key is join using “_” per row

Return type:

None