Google Spreadsheets Python API - Simple interface for working with Google Sheets
Methods for modifying worksheet structure including adding/deleting rows and columns, resizing, freezing, and organizing dimension groups.
Expand worksheet dimensions by adding rows and columns.
class Worksheet:
def add_rows(rows: int) -> None:
"""
Add rows to the end of worksheet.
Parameters:
- rows (int): Number of rows to add.
Returns:
None
"""
def add_cols(cols: int) -> None:
"""
Add columns to the end of worksheet.
Parameters:
- cols (int): Number of columns to add.
Returns:
None
"""Insert new rows and columns at specific positions with optional data.
class Worksheet:
def insert_row(values: List = None, index: int = 1, value_input_option: str = "RAW",
inherit_from_before: bool = False) -> Dict:
"""
Insert single row at specified position.
Parameters:
- values (List, optional): Values for the new row.
- index (int): Position to insert row (1-indexed). Default: 1.
- value_input_option (str): "RAW" or "USER_ENTERED". Default: "RAW".
- inherit_from_before (bool): Inherit formatting from previous row. Default: False.
Returns:
Dict: Response from insert operation.
"""
def insert_rows(values: List[List] = None, row: int = 1, value_input_option: str = "RAW",
inherit_from_before: bool = False) -> Dict:
"""
Insert multiple rows at specified position.
Parameters:
- values (List[List], optional): 2D array of values for new rows.
- row (int): Position to insert rows (1-indexed). Default: 1.
- value_input_option (str): "RAW" or "USER_ENTERED". Default: "RAW".
- inherit_from_before (bool): Inherit formatting from previous row. Default: False.
Returns:
Dict: Response from insert operation.
"""
def insert_cols(values: List[List], col: int = 1, value_input_option: str = "RAW",
inherit_from_before: bool = False) -> Dict:
"""
Insert columns at specified position with values.
Parameters:
- values (List[List]): 2D array of values for new columns.
- col (int): Position to insert columns (1-indexed). Default: 1.
- value_input_option (str): "RAW" or "USER_ENTERED". Default: "RAW".
- inherit_from_before (bool): Inherit formatting from previous column. Default: False.
Returns:
Dict: Response from insert operation.
"""Remove rows and columns from worksheet.
class Worksheet:
def delete_rows(start_index: int, end_index: int = None) -> Dict:
"""
Delete rows from worksheet.
Parameters:
- start_index (int): Starting row index (1-indexed).
- end_index (int, optional): Ending row index (1-indexed, exclusive). If None, deletes single row.
Returns:
Dict: Response from delete operation.
"""
def delete_columns(start_index: int, end_index: int = None) -> Dict:
"""
Delete columns from worksheet.
Parameters:
- start_index (int): Starting column index (1-indexed).
- end_index (int, optional): Ending column index (1-indexed, exclusive). If None, deletes single column.
Returns:
Dict: Response from delete operation.
"""Change worksheet dimensions.
class Worksheet:
def resize(rows: int = None, cols: int = None) -> Dict:
"""
Resize worksheet to specified dimensions.
Parameters:
- rows (int, optional): New number of rows. If None, keeps current row count.
- cols (int, optional): New number of columns. If None, keeps current column count.
Returns:
Dict: Response from resize operation.
"""Automatically adjust row heights and column widths.
class Worksheet:
def rows_auto_resize(start_row_index: int, end_row_index: int = None) -> Dict:
"""
Auto-resize row heights to fit content.
Parameters:
- start_row_index (int): Starting row index (0-indexed).
- end_row_index (int, optional): Ending row index (0-indexed, exclusive). If None, resizes single row.
Returns:
Dict: Response from auto-resize operation.
"""
def columns_auto_resize(start_column_index: int, end_column_index: int = None) -> Dict:
"""
Auto-resize column widths to fit content.
Parameters:
- start_column_index (int): Starting column index (0-indexed).
- end_column_index (int, optional): Ending column index (0-indexed, exclusive). If None, resizes single column.
Returns:
Dict: Response from auto-resize operation.
"""Freeze rows and columns for scrolling.
class Worksheet:
def freeze(rows: int = None, cols: int = None) -> Dict:
"""
Freeze rows and/or columns.
Parameters:
- rows (int, optional): Number of rows to freeze from top.
- cols (int, optional): Number of columns to freeze from left.
Returns:
Dict: Response from freeze operation.
"""Control visibility of rows and columns.
class Worksheet:
def hide_rows(start_index: int, end_index: int) -> Dict:
"""
Hide rows.
Parameters:
- start_index (int): Starting row index (0-indexed).
- end_index (int): Ending row index (0-indexed, exclusive).
Returns:
Dict: Response from hide operation.
"""
def hide_columns(start_index: int, end_index: int) -> Dict:
"""
Hide columns.
Parameters:
- start_index (int): Starting column index (0-indexed).
- end_index (int): Ending column index (0-indexed, exclusive).
Returns:
Dict: Response from hide operation.
"""
def unhide_rows(start_index: int, end_index: int) -> Dict:
"""
Show previously hidden rows.
Parameters:
- start_index (int): Starting row index (0-indexed).
- end_index (int): Ending row index (0-indexed, exclusive).
Returns:
Dict: Response from unhide operation.
"""
def unhide_columns(start_index: int, end_index: int) -> Dict:
"""
Show previously hidden columns.
Parameters:
- start_index (int): Starting column index (0-indexed).
- end_index (int): Ending column index (0-indexed, exclusive).
Returns:
Dict: Response from unhide operation.
"""Create and manage groups of rows or columns.
class Worksheet:
def add_dimension_group(dimension: str, start_index: int, end_index: int) -> Dict:
"""
Add dimension group (for collapsible sections).
Parameters:
- dimension (str): "ROWS" or "COLUMNS".
- start_index (int): Starting index (0-indexed).
- end_index (int): Ending index (0-indexed, exclusive).
Returns:
Dict: Response from group creation.
"""
def add_dimension_group_columns(start: int, end: int) -> Dict:
"""
Group columns in order to hide them in the UI.
Parameters:
- start (int): The start (inclusive) of the group.
- end (int): The end (exclusive) of the group.
Returns:
Dict: The response body.
"""
def add_dimension_group_rows(start: int, end: int) -> Dict:
"""
Group rows in order to hide them in the UI.
Parameters:
- start (int): The start (inclusive) of the group.
- end (int): The end (exclusive) of the group.
Returns:
Dict: The response body.
"""
def list_dimension_group_columns() -> List[Dict]:
"""
List all the grouped columns in this worksheet.
Returns:
List[Dict]: List of the grouped columns.
"""
def list_dimension_group_rows() -> List[Dict]:
"""
List all the grouped rows in this worksheet.
Returns:
List[Dict]: List of the grouped rows.
"""
def delete_dimension_group_rows(start_index: int, end_index: int) -> Dict:
"""
Delete row dimension group.
Parameters:
- start_index (int): Starting row index (0-indexed).
- end_index (int): Ending row index (0-indexed, exclusive).
Returns:
Dict: Response from group deletion.
"""
def delete_dimension_group_columns(start_index: int, end_index: int) -> Dict:
"""
Delete column dimension group.
Parameters:
- start_index (int): Starting column index (0-indexed).
- end_index (int): Ending column index (0-indexed, exclusive).
Returns:
Dict: Response from group deletion.
"""Update worksheet properties and manage worksheet lifecycle.
class Worksheet:
def update_title(title: str) -> Dict:
"""
Update worksheet title.
Parameters:
- title (str): New title for the worksheet.
Returns:
Dict: Response from title update.
"""
def update_index(index: int) -> Dict:
"""
Update worksheet position/index.
Parameters:
- index (int): New index position (0-indexed).
Returns:
Dict: Response from index update.
"""
def copy_to(destination_spreadsheet_id: str) -> Worksheet:
"""
Copy worksheet to another spreadsheet.
Parameters:
- destination_spreadsheet_id (str): ID of destination spreadsheet.
Returns:
Worksheet: New worksheet instance in destination spreadsheet.
"""
def duplicate(insert_sheet_index: int = None, new_sheet_id: int = None, new_sheet_name: str = None) -> Worksheet:
"""
Duplicate this worksheet within same spreadsheet.
Parameters:
- insert_sheet_index (int, optional): Position to insert duplicated sheet.
- new_sheet_id (int, optional): ID for new sheet.
- new_sheet_name (str, optional): Name for new sheet.
Returns:
Worksheet: Duplicated worksheet instance.
"""Add validation rules to cells and ranges to control data entry.
class Worksheet:
def add_validation(range: str, condition_type: str, values: List[Any],
inputMessage: str = None, strict: bool = False, showCustomUi: bool = False) -> Any:
"""
Add a data validation rule to any given range.
Parameters:
- range (str): The A1 notation of the range to validate.
- condition_type (str): The sort of condition to apply.
- values (List[Any]): List of condition values.
- inputMessage (str, optional): Message to show for the validation.
- strict (bool): Whether to reject invalid data or not. Default: False.
- showCustomUi (bool): Whether to show a custom UI (Dropdown) for list values. Default: False.
Returns:
Any: The validation response.
"""class Worksheet:
@property
def row_count -> int:
"""Number of rows in worksheet."""
@property
def col_count -> int:
"""Number of columns in worksheet."""
@property
def frozen_row_count -> int:
"""Number of frozen rows."""
@property
def frozen_col_count -> int:
"""Number of frozen columns."""Usage examples:
# Add rows and columns
worksheet.add_rows(10)
worksheet.add_cols(5)
# Insert rows with data
worksheet.insert_rows([
['Name', 'Age'],
['Alice', 25]
], row=1)
# Delete rows
worksheet.delete_rows(5, 10) # Delete rows 5-9
# Resize worksheet
worksheet.resize(rows=100, cols=20)
# Auto-resize columns to fit content
worksheet.columns_auto_resize(0, 5) # Columns A-E
# Freeze first row and column
worksheet.freeze(rows=1, cols=1)
# Hide columns B and C
worksheet.hide_columns(1, 3) # 0-indexed: columns 1-2
# Create dimension group for rows
worksheet.add_dimension_group('ROWS', 5, 10)
# Update worksheet title
worksheet.update_title('Data Sheet')Install with Tessl CLI
npx tessl i tessl/pypi-gspread