Docs
Introduction
PySheetsQL
pysheetsql is a Python package designed to simplify the management of Google Sheets data using the gspread library. With this package, you can effortlessly create, update, retrieve, delete, and export Google Sheets data.
Installation
pip install pysheetsql
SheetClient
from pysheetsql.start import SheetClient
Description:
Builds a client for accessing Google API.
Arguments:
- scopes:
list of str- An array of strings specifying the API scopes. - credentials_file_path:
str- The path to the JSON file containing your credentials.
Returns:
client: The client object for accessing Google APIs.
Raises: Exception - If an error occurs while creating the client.
Example:
client = SheetClient(["https://www.googleapis.com/auth/spreadsheets"], "path/to/credentials.json")
CreateTable
from pysheetsql.table import CreateTable
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table. - fields:
list of str- A list of field names (the first field is considered the class name). - primary_key:
bool, optional- Whether to include a primary key field. Default is True.
Returns: None
Raises: TypeError - If `table_name` is not a string or `fields` is not a list of strings.
Table already exists if `table_name` has already been used.
Example:
CreateTable(client, "StudentRecords", ["Name", "Age", "Grade"])
DeleteTable
from pysheetsql.table import DeleteTable
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table you want to delete.
Returns: None
Raises: TypeError - If `table_name` is not a string.
Table not found: If the table is not found.
Example:
DeleteTable(client, "StudentRecords")
ListTables
from pysheetsql.table import ListTables
Arguments:
- client:
SheetClient- The SheetClient client instance.
Returns: None
Example:
ListTables(client)
ExportTable
from pysheetsql.table import ExportTable
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table you want to export.
Returns: None
Raises: TypeError - If `table_name` is not a string.
gspread.exceptions.SpreadsheetNotFound - If the table does not exist.
Example:
ExportTable(client, "StudentRecords")
AddData
from pysheetsql.manage_table import AddData
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table to add data to. - data:
list- A list of values to add to the table. If the table has a primary key, data should exclude the primary key value.
Returns: None
Raises: TypeError, gspread.exceptions.SpreadsheetNotFound
Example:
AddData(client, "StudentRecords", ["John Doe", 20, "A"])
GetDataName
from pysheetsql.manage_table import GetDataName
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table to retrieve data from. - target_main_field_name:
str- The value in the main field to identify the row to retrieve.
Returns: list or None
Raises: TypeError, gspread.exceptions.SpreadsheetNotFound
Example:
GetDataName(client, "StudentRecords", "John Doe")
GetDataID
from pysheetsql.manage_table import GetDataID
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table to retrieve data from. - target_id:
int- The primary ID value to identify the row to retrieve.
Returns: list or None
Raises: TypeError, ValueError, gspread.exceptions.SpreadsheetNotFound
Example:
GetDataID(client, "StudentRecords", 1)
UpdateDataName
from pysheetsql.manage_table import UpdateDataName
Arguments:
- client:
SheetClient- The SheetClient client instance. - table_name:
str- The name of the table to update. - target_main_field_name:
str- The value in the main field to identify the row to update. - target_change_field_name:
str- The field name in which to update the value. - target_change_value:
str- The new value to set in the specified field.
Returns: None
Raises: TypeError, gspread.exceptions.SpreadsheetNotFound
Example:
UpdateDataName(client, "StudentRecords", "John Doe", "Age", "21")
UpdateDataID
from pysheetsql.manage_table import UpdateDataID
Arguments:
SheetClient - The SheetClient client instance.str - The name of the table to update.int - The primary ID value of the row to update.str - The field name in which to update the value.str - The new value to set in the specified field.Returns: None
Raises: TypeError, gspread.exceptions.SpreadsheetNotFound
Example:
UpdateDataID(client, "StudentRecords", 1, "Age", "21")
ShareTables
from pysheetsql.manage_table import UpdateDataID
Arguments:
SheetClient - The SheetClient client instance.str List of emails that you want to have access to the tablesintSpecific table you want to give the emails access too. Defualt is none so if not specificed user will get access to every table under the service account.Returns: None
Raises: TypeError, gspread.exceptions.SpreadsheetNotFound
Example:
ShareTables(client, ["xyz@gmail.com", "yxz@gmail.com"], table_name="UserData")